Mastering DevOps: Advanced Linux Shell Scripting and User Management for Efficient Operations
Why this matters today: Linux fluency still compounds. Clear command-line habits and safe automation make incident response, troubleshooting, and platform work materially faster.
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:
#!/bin/bash
# Check if the number of arguments is correct
if [ $# -ne 3 ]
then
echo "Usage: $0 directory_name start_number end_number"
exit 1
fi
# Assign variables to the input arguments
dir_name=$1
start=$2
end=$3
# Loop through the range of numbers and create directories
for (( i=start; i<=end; i++ ))
do
mkdir "${dir_name}${i}"
done
echo "Directories created successfully!"
To use this script, save it in a file named createDirectories.sh, and give it execute permissions using the chmod +x createDirectories.sh command. Then, run the script with the desired input arguments:
./createDirectories.sh 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:
#!/bin/bash
# Set the backup destination directory
backup_dir="/path/to/backup/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
backup_file="${backup_dir}/backup_${date_format}.tar.gz"
# 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