π§ Advanced Linux Shell Scripting for DevOps Engineers
Learn how to automate Linux tasks effortlessly with advanced shell scripting!
π‘ 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!