The Simplified Path to Learning Linux Shell Scripting : Advanced - Easy Guide
💠Introduction 💠
👋 Welcome to the world of Linux directory management, backups, and user management! In this article, we'll solve the problem of manually and automatically creating directories.
I am going even teach you how to create your own directories from scratch using only a script. In addition, we'll discuss why backups are a DevOps action figure and demonstrate you to the wonders of Cron scheduling. Don't worry, I'll keep it basic and easy to use! 🚀💻
Directories Creation 📁
How Were the Directories Created?
We can create it manually and through Automation. But if we have to create multiple directories then it will be time-consuming.
Automating file creation is better as it reduces the risk of human error, and ensures consistency in naming and structure, ultimately leading to increased efficiency and accuracy in managing and organizing files.
Automation script by the following way for creating directories with sequential names:
Create a createDirectories.sh and make it executable
vim createDirectories.sh
#!/bin/bash
# Check if there are exactly three arguments
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Assign the arguments to variables
directory_name="$1"
start_number="$2"
end_number="$3"
# Check if the start number is less than or equal to the end number
if [ "$start_number" -gt "$end_number" ]; then
echo "Error: Start number must be less than or equal to end number."
exit 1
fi
# Create directories with dynamic names
for ((i = start_number; i <= end_number; i++)); do
dir="${directory_name}_${i}"
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
echo "Created directory: $dir"
else
echo "Directory $dir already exists."
fi
done
echo "Directories created successfully."
Insert the code by hitting "i" on the keyboard
above the script saved by wq!
#!/bin/bash to instruct the operating system to use bash as a command interpreter
$# represents the number of arguments passed to the script.
-ne is a comparison operator that stands for "not equal."
3 is the value we're comparing the number of arguments to
-gt is greater than used for comparison
chmod +x createDirectories.sh
Run the script by following the way to create directories from day1 to day90
./createDirectories.sh day 1 90
Check the output :
Congratulations! 🎉You created day1 to day90 directories in a single click.
In the same way, taking another example to create a directory movie20 movie50
./createDirectories.sh movie 20 50
Output :
Taking Backup by Shell Script
🌠Importance of Backups in DevOps
To protect important data and setups in DevOps, backups are essential. They guarantee business continuity in the event of unexpected situations by acting as an extra layer of protection against data loss.
Backups ease quick system recovery, enabling teams to keep working and cut down on downtime. They also support environment replication and version control, which helps with software development, testing, and deployment. In short, backups are an important element of DevOps, ensuring data integrity, resilience, and the overall success of the project in its entirety.
🌠Creating a Backup Script and automating Backups
Cron and Crontab
Cron is similar to your trusty alarm clock. It is a scheduling utility used in operating systems that operate similarly to Unix (like Linux) to automate operations at predefined intervals. Running backups to updating your social media posts are examples of these responsibilities.
As a DevOps engineer, you have to take the backups of scripts on a daily basis. All the scripts that I had stored inside the scripts directory.
Now, make a backup folder to take all the backups with their timestamp & Inside that we will write one script so that we will take a backup.
mkdir backups/
cd backups
vim backups.sh
#!/bin/bash
src_dir="/home/ubuntu/scripts"
tgt_dir="/home/ubuntu/backups"
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
# Check if the source directory exists
if [ -d "$src_dir" ]; then
# Create the target directory if it doesn't exist
mkdir -p "$tgt_dir/$curr_timestamp"
# Copy the files from the source directory to the timestamped directory
cp -r "$src_dir"/* "$tgt_dir/$curr_timestamp/"
echo "Backup completed and saved in $tgt_dir/$curr_timestamp"
else
echo "Source directory $src_dir does not exist. Backup aborted."
fi
Insert the code by hitting "i" on the keyboard
above the script saved by wq!
👇Timestamp breakdown 👇
%Y: Represents the year with century as a decimal number (e.g., 2023).
%m: Represents the month as a zero-padded decimal number (e.g., 09 for September).
%d: Represents the day of the month as a zero-padded decimal number (e.g., 05).
%H: Represents the hour (24-hour clock) as a zero-padded decimal number (e.g., 15 for 3 PM).
%M: Represents the minute as a zero-padded decimal number (e.g., 07).
%S: Represents the second as a zero-padded decimal number (e.g., 23).
✨Make the scripts executable
chmod +x backups.sh
Run the scripts :
./backups.sh
As we had taken the backup, we will check it out is that by is the backup is present or not :
ls
cd 2023-09-10-10-31-14/cd
ls
Bravo! 🎉 , You completed the backup.
🌠Understanding User Management in Linux User IDs in Linux 😮
User management is required in Linux since it serves various functions. It protects the system by enforcing access controls, preventing illegal access, and protecting sensitive data. It enables proper resource allocation and usage, hence avoiding conflicts and resource hogging.
Effective user management also makes system administration easier, increases accountability, and makes auditing and troubleshooting easier. Overall, it is an important factor in maintaining a secure, set up and productive Linux environment.
The root user is assigned the ID 0 and the system users are assigned the IDs 1 to 999 (both inclusive), hence the IDs for local users begin at 1000.
You can check your ID by following way :
Syntax: id username
For my system the username is ubuntu so consider it in the following way.
id ubuntu
If you want to display a list of all Linux users :
cat /etc/passwd
Now, we will create a users
Syntax: sudo useradd username
creating a user having the name "Vipul" and "Anshul"
sudo useradd Vipul
sudo useradd Anshul
Congratulations! 🎉You created users, you are one step ahead.
⭐Bonus Tip
1)If you want to remove the files at once in a single shot you can remove them by following the way
rm -r day_{1..90}
2)You can change the password of this user by following the way :
Syntax: sudo passwd username
sudo passwd Anshul
(Here as you type the password it will not visible to you , that's the security feature of Linux)
I hope you enjoy the blog post!
If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊