🐧 Advanced Linux Shell Scripting for DevOps Engineers

🐧 Advanced Linux Shell Scripting for DevOps Engineers

Learn how to automate Linux tasks effortlessly with advanced shell scripting!

Β·

4 min read

πŸ’‘ Ever wondered how to create 90 directories in seconds? Instead of manually making each folder (mkdir day1, mkdir day2... 😩), we use automation!


1️⃣ Creating Multiple Directories in Seconds

Let’s say we need 90 directories named day1, day2, ..., day90. Instead of wasting time, a simple one-liner does the job:

mkdir day{1..90}

πŸ”₯ BOOM! 90 directories created instantly!

πŸ“Œ But what if we need flexibility?

We can write a Bash script to create any range of directories dynamically!

πŸ“œ createDirectories.sh (Bash Script to Create Directories)

#!/bin/bash

# Checking if the correct number of arguments are passed
if [ $# -ne 3 ]; then
    echo "Usage: $0 <directory_name> <start_number> <end_number>"
    exit 1
fi

# Assigning arguments to variables
dir_name=$1      # First argument (Directory name prefix)
start_num=$2     # Second argument (Start number)
end_num=$3       # Third argument (End number)

# Loop to create directories
for ((i=start_num; i<=end_num; i++)); do
    mkdir "${dir_name}${i}"  # Creates directories like day1, day2...day90
done

echo "βœ… Created directories from ${dir_name}${start_num} to ${dir_name}${end_num}"

πŸ“Œ Explanation:
1️⃣ Checks if the correct number of arguments (3) are provided. If not, it shows the correct usage and exits.
2️⃣ Takes directory name, start number, and end number as input.
3️⃣ Loops from the start number to the end number and creates directories dynamically using mkdir.
4️⃣ Prints a success message after completion.

πŸ“Œ How to run this script?

chmod +x createDirectories.sh       # Makes the script executable
./createDirectories.sh day 1 90     # Creates day1 to day90
./createDirectories.sh Movie 20 50  # Creates Movie20 to Movie50

πŸš€ Now, you can generate any number of directories dynamically!


2️⃣ Automating Backups in Linux πŸ› οΈ

As a DevOps Engineer, backup is life-saving!
Imagine accidentally deleting files... 😱 A backup script can save your day!

πŸ“œ backup.sh (Bash Script for Backup)

#!/bin/bash

backup_dir="/home/$(whoami)/backup"  # Define backup directory based on the logged-in user
mkdir -p $backup_dir  # Create the backup directory if it doesn’t exist

# Create a compressed backup of the 'work' directory
tar -czf "$backup_dir/backup_$(date +%Y%m%d_%H%M%S).tar.gz" /home/$(whoami)/work

echo "βœ… Backup completed! Stored in $backup_dir"

πŸ“Œ Explanation:
1️⃣ Defines a backup directory inside the user's home folder (/home/username/backup).
2️⃣ Creates the backup directory if it doesn’t exist using mkdir -p.
3️⃣ Compresses the work directory using tar -czf, saving it with a timestamp (YYYYMMDD_HHMMSS) to avoid overwriting.
4️⃣ Displays a success message after completing the backup.

πŸ€– Automate Backups using Cron!

Instead of running the script manually, schedule it with cron:

crontab -e

Add this line to run the backup every night at 2 AM:

0 2 * * * /path/to/backup.sh

πŸ“Œ Explanation:

πŸ”Ή 0 β†’ The minute when the script runs (0th minute).
πŸ”Ή 2 β†’ The hour when the script runs (2 AM).
πŸ”Ή Scripting Secrets: Automate Everything in Linux!**β†’ The **day of the month (runs every day*).
πŸ”Ή
* β†’ The month (runs every month).
πŸ”Ή
* β†’ The day of the week (runs on all days**).

πŸ”₯ Now backups happen automatically! No worries about data loss!


3️⃣ User Management in Linux πŸ‘₯

Users in Linux = Separate Accounts for Different Tasks
πŸ’‘ Linux assigns unique IDs to each user.
πŸ”Ή System Users: IDs 0-999
πŸ”Ή Local Users: IDs 1000+

πŸ“Œ Create 2 Users and Display Their Names

sudo useradd devops1  # Creates a new user named 'devops1'
sudo useradd devops2  # Creates another user named 'devops2'

# Display user list
cut -d: -f1 /etc/passwd | tail -n 5

πŸ“Œ Explanation:
1️⃣ useradd devops1 & useradd devops2 β†’ Creates new users named devops1 and devops2.
2️⃣ cut -d: -f1 /etc/passwd β†’ Extracts usernames from /etc/passwd (Linux user database).
3️⃣ tail -n 5 β†’ Displays the last 5 users (including the new ones).

πŸš€ Now, you’ve created users like a pro!


πŸ”š Final Thoughts

  • πŸ“ Automated Directory Creation β†’ Saves Time!

  • πŸ’Ύ Backup Script + Cron Job β†’ Keeps Data Safe!

  • πŸ‘€ User Management β†’ Controls Access in Linux!

πŸ’‘ DevOps is all about automation! Start scripting, and make Linux work for you!

Β