Mastering DevOps: Advanced Linux Shell Scripting and User Management for Efficient Operations

As a DevOps engineer, you need to be proficient in Linux shell scripting to automate tasks and manage systems efficiently. In this article, we will discuss a total of three tasks related to Linux shell scripting and user management.

Task 1: Create Directories Using a Bash Script

In this task, we are required to create a bash script that creates directories with dynamic names based on the input arguments. Here is the script:


# Check if the number of arguments is correct
if [ $# -ne 3 ]
  echo "Usage: $0 directory_name start_number end_number"
  exit 1

# Assign variables to the input arguments

# Loop through the range of numbers and create directories
for (( i=start; i<=end; i++ ))
  mkdir "${dir_name}${i}"

echo "Directories created successfully!"

To use this script, save it in a file named, and give it execute permissions using the chmod +x command. Then, run the script with the desired input arguments:

./ day 1 90

This will create 90 directories with names day1, day2, day3, and so on.

Task 2: Create a Backup Script

In this task, we are required to create a script to back up all the work done so far. Backups are essential to ensure that your work is safe and can be recovered in case of any failure. Here is a simple backup script:


# Set the backup destination directory

# Set the date format for the backup filename
date_format=$(date +%Y-%m-%d_%H-%M-%S)

# Create a backup file with the current date and time in the filename

# Create the backup archive
tar -czf "${backup_file}" /path/to/source/directory

echo "Backup created successfully at ${backup_file}"

In this script, we set the backup destination directory and the date format for the backup filename. We then create a backup file with the current date and time in the filename and create a backup archive using the tar command.

To automate this backup script, we can use the Cron scheduler. Cron is a Linux utility that allows you to schedule tasks to run automatically at specified intervals. To schedule our backup script, we need to add a Cron job to the Crontab file. Here is an example Cron job that runs the backup script every day at 2 AM:

0 2 * * * /path/to/backup/

To add this Cron job, run the crontab -e command and add the line above to the Crontab file. This will schedule the backup script to run every day at 2 AM.

Task 3: User Management

In this task, we are required to create two users and display their usernames. User management is an essential task for DevOps engineers, as it allows them to manage access to systems and resources. Here is how you can create users and display their usernames:

# Create a user with username john
sudo useradd john

# Create a user with username jane
sudo useradd jane

# Display the usernames of all users on the system
cut -d: -f1 /etc/passwd

After creating the two users, we can display their usernames using the following command:

cut -d: -f1 /etc/passwd

This command extracts the first field (username) from the /etc/passwd file, which is the system's user database file that stores information about all the users on the system.

The output of this command will be a list of all the usernames on the system, including the two users we just created. We can filter the output to only show the two new users by piping the output to the grep command:

cut -d: -f1 /etc/passwd | grep -e "^user1$" -e "^user2$"

This command uses the grep command to search for the usernames "user1" and "user2" in the output of the cut command. The -e option allows us to specify multiple search patterns, and the "^" and "$" characters ensure that we match the exact usernames and not partial matches.

The output of this command will be:


which confirms that we have successfully created the two users and displayed their usernames.

User management is an important aspect of DevOps, as managing users and permissions is crucial for ensuring the security and reliability of systems and applications. By learning about user management in Linux, DevOps engineers can effectively manage user accounts, access control, and resource allocation in their environments.

The above information is up to my understanding. Suggestions are always welcome.

~Abhiraj kharbade

#DevOps, #Day5_Blog, #Linux, #shell, #shell script .

Shubham Londhe