Advanced Linux Shell Scripting for DevOps Engineers with User management
Shell script for creating n number for creating n no of directories in a given range
if [ $# -ne 3 ]; then
echo "Usage: $0 <directory name> <start number> <end number>"
exit 1
fi
dir_name="$1"
start="$2"
end="$3"
for i in $(seq $start $end); do
dir="${dir_name}_${i}"
if [ ! -d "$dir" ]; then
mkdir "$dir"
echo "Created directory: $dir"
fi
done
The script takes three command-line arguments: the base directory name, the starting number of directories, and the ending number of directories to create.
If the number of arguments is not three, the script prints an error message and exits with a non-zero status.
The script uses a
for
loop with theseq
command to iterate over the numbers from the starting to the ending number.For each number, the script constructs a directory name by appending the number to the base directory name with an underscore separator.
The script checks if the directory already exists, and if not, creates it with the
mkdir
command and prints a message to indicate that the directory was created.
To use the script, save it to a file (e.g., create_directories.sh
), make it executable with the chmod +x create_directories.sh
command, and then run it with the desired directory name, start number, and end number as arguments:
bashCopy code./create_directories.sh mydir 1 5
This will create five directories named mydir_1
, mydir_2
, mydir_3
, mydir_4
, and mydir_5
.
Create a Script to backup all your work done till now
#!/bin/bash
# Set backup file name and path
backup_dir=~/my_backups
backup_file=$(date +%Y%m%d_%H%M%S)_backup.tar.gz
backup_path=$backup_dir/$backup_file
# Create backup directory if it doesn't exist
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
# Create backup archive
tar czf "$backup_path" ~/Documents ~/Projects
echo "Backup successful: $backup_path"
The script sets up the backup directory, backup file name, and backup path.
It creates the backup directory if it doesn't already exist.
It uses the
tar
command to create a compressed archive of the~/Documents
and~/Projects
directories, and saves it to the backup path.It skips the verification step and prints a message indicating that the backup was successful after creating the backup archive.
To use the script, save it to a file, make it executable, and run it with the appropriate command.
To automate the backup script using cron, you can follow these steps:
Open the crontab file by executing the command
crontab -e
.crontab -e
This will open the crontab file in the default text editor for your system.
Add a new line at the end of the file in the format
* * * * * /path/to/backup.sh
to schedule the script to run every minute. The five fields in the line represent the minute, hour, day of the month, month, and day of the week, respectively.* * * * * /path/to/backup.sh
Customize the schedule by modifying the five fields in the line. For instance, to run the script every day at 6:00 AM, use the line
0 6 * * * /path/to/backup.sh
.0 6 * * * /path/to/backup.sh
Save and exit the crontab file.
After adding the line to your crontab file, the cron job will run the backup script at the scheduled times. You can check the system logs to ensure that the backups are being created successfully. However, ensure that the backup script is executable and has the correct path specified in the crontab file. It's also crucial to test the backup script and the cron job to verify that they're working correctly and the backups are being created as expected.
User Management
User management is an essential part of system administration. It involves adding, modifying, and deleting users from the system. In this blog, we will explore user management in Linux using bash commands.
Adding a user To add a new user to the system, you can use the useradd
command. Here is an example:
sudo useradd john
This command will create a new user with the username john
. If you want to set a password for the new user, you can use the passwd
command:
sudo passwd john
This command will prompt you to enter and confirm the new password for the user.
Modifying user details If you want to modify the details of an existing user, you can use the usermod
command. For example, if you want to change the home directory of the user john
, you can use the following command:
sudo usermod -d /new/home/directory john
This command will change the home directory of the user john
to /new/home/directory
.
Deleting a user To delete a user from the system, you can use the userdel
command. For example, if you want to delete the user john
, you can use the following command:
sudo userdel john
This command will delete the user john
from the system.
Conclusion In this blog, we have explored the basics of user management in Linux using bash commands. We have covered adding a user, modifying user details, and deleting a user. These commands are essential for system administrators to manage users on a Linux system.