Day 5: Advanced Linux Shell Scripting for DevOps Engineers with User management
In this blog, we will go deep dive into Bash Scripting
Creating Dynamic Directories with Bash Scripting using Loops and Arguments
We will be creating a bash script createDirectories.sh that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
Example: Create Directories from 1 to 90
Let's assume we want to create 90 directories with names like "day1," "day2," and so on, up to "day90." We will execute the script as follows:
Output:
Now let's understand the script first before we go further into Bash Script
. It takes the input arguments and uses them to create directories with a dynamic name (e.g., "day_1," day_2," ...).
. The -p
option in mkdir
ensures that the script creates parent directories if they do not exist.
. "if [ $# -ne 3 ]; then ":
$#
: This special variable holds the number of arguments passed to the script.-ne
: This is a comparison operator in bash and stands for "not equal."3
: This is the value that we are comparing against the number of arguments. In this case, it is the expected number of arguments needed for the script.
Example 2: When the script is executed it will create a Movie Directories Movie1 till Movie50
./
createDirectories.sh
Movie 20 50
then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50
Script to backup all your work done till now
source_dir
: Here you can also define your source directory
backup_dir
: Here you have to define the path where you want to keep your backup
timestamp
: This variable uses the date
command to generate the current timestamp in the format YYYY-MM-DD-HH-MM-SS
backup_dir
: This variable stores the path of the backup file with the name in the destination folder
Automate Backup with Cron and Crontab
Cron is a time-based job scheduling daemon in Unix-like operating systems. It runs in the background and executes commands or scripts based on a predefined schedule. These scheduled tasks are referred to as "cron jobs."
The crontab
command is used to create, edit, and manage the user-specific cron tables. Users can use the crontab
command with various options to schedule their tasks.
When you edit the crontab using the crontab -e
command, it opens the crontab file in the default text editor for you to make changes.
The crontab file format consists of five time and date fields (minute, hour, day of the month, month, day of the week) and the command to be executed. It follows the syntax:
* * * * * command_to_execute
Each asterisk represents a wildcard, meaning it matches any value. The five fields allow you to specify when the command should be executed. For example, 0 1 * * * /path/to/script.sh
means the command /path/to/script.sh
will be executed at 1:00 AM every day.
Here's a brief explanation of the five fields:
Minute (0-59)
Hour (0-23)
Day of the month (1-31)
Month (1-12)
Day of the week (0-6, where 0 represents Sunday)
User Management
User management refers to the process of creating, configuring, and maintaining user accounts on a computer system. It is an essential aspect of system administration, as it allows administrators to control access, permissions, and privileges for individual users on the system.
Create 2 users and just display their Usernames
useradd username
# Display detailed user information
id ansible
This will provide output similar to the following:
uid=1000(ansible) gid=1000(ansible) groups=1000(ansible)
Subscribe to my newsletter
Read articles from Moiz Asif directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by