Advanced Linux Shell Scripting for DevOps Engineers with User management-Day 5
Table of contents
- 1.Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.
- Example 1: When the script is executed as
- ./createDirectories.sh day 1 90
- then it creates 90 directories as day1 day2 day3 .... day90
- Example 2: When the script is executed as
- 2.Create a Script to backup all your work done till now.
- 3.About Cron and Crontab, to automate the backup Script
- 4.About User Management
- 5.Create 2 users and just display their Usernames
1.Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.
Example 1: When the script is executed as
./
createDirectories.sh
day 1 90
then it creates 90 directories as day1 day2 day3 .... day90
Example 2: When the script is executed as
Here's an example of a bash script createDirectories.sh
that creates a specified number of directories with a dynamic directory name based on the arguments passed to the script:
#!/bin/bash
# Check if 3 arguments are passed to the script
if [ $# -ne 3 ]; then
echo "Usage: createDirectories.sh <directory_name> <start_number> <end_number>"
exit 1
fi
# Get the arguments
dir_name=$1
start_num=$2
end_num=$3
# Loop through the specified number of directories and create them
for (( i=$start_num; i<=$end_num; i++ ))
do
mkdir "${dir_name}${i}"
done
echo "Directories created successfully."
To run this script, open a terminal and navigate to the directory where the script is saved. Then, execute the following command:
bash createDirectories.sh day 1 90
This will create 90 directories named day1
through day90
in the current directory. You can change the directory name and the start and end numbers as per your requirements.
2.Create a Script to backup all your work done till now.
Here's an example of a bash script that can backup all your work done till now:
#!/bin/bash
# Set the backup directory
backup_dir="/path/to/backup/directory"
# Set the current date and time as the backup file name
backup_file_name="backup_$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
# Create the backup directory if it doesn't exist
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
# Create the backup archive
tar -czvf "$backup_dir/$backup_file_name" /path/to/your/work
# Check if the backup archive was created successfully
if [ $? -eq 0 ]; then
echo "Backup created successfully."
else
echo "Backup creation failed."
fi
To use this script, replace /path/to/backup/directory
with the actual path to the directory where you want to store your backups, and replace /path/to/your/work
with the actual path to the directory or file(s) you want to backup.
Save the script in a file, for example backup_script.sh
, and make it executable with the command:
chmod +x backup_script.sh
Then, run the script with the command:
./backup_script.sh
This will create a backup archive of your work in the specified backup directory with the current date and time as the file name. You can run this script periodically to ensure that your work is backed up regularly.
Here's an example of a bash script that can backup all your work done till now:
bashCopy code#!/bin/bash
# Set the backup directory
backup_dir="/path/to/backup/directory"
# Set the current date and time as the backup file name
backup_file_name="backup_$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
# Create the backup directory if it doesn't exist
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
# Create the backup archive
tar -czvf "$backup_dir/$backup_file_name" /path/to/your/work
# Check if the backup archive was created successfully
if [ $? -eq 0 ]; then
echo "Backup created successfully."
else
echo "Backup creation failed."
fi
To use this script, replace /path/to/backup/directory
with the actual path to the directory where you want to store your backups, and replace /path/to/your/work
with the actual path to the directory or file(s) you want to backup.
Save the script in a file, for example backup_script.sh
, and make it executable with the command:
chmod +x backup_script.sh
Then, run the script with the command:
./backup_script.sh
This will create a backup archive of your work in the specified backup directory with the current date and time as the file name. You can run this script periodically to ensure that your work is backed up regularly.
3.About Cron and Crontab, to automate the backup Script
Cron is a time-based job scheduler in Linux and other Unix-like operating systems. It allows you to schedule a command or a script to run automatically at a specific time or on a specific interval. Cron is often used for automating system maintenance or administration tasks, such as backups, updates, and monitoring.
Crontab (short for "cron table") is a configuration file that specifies the commands or scripts to be executed by cron. Each user has their own crontab file, which can be edited using the crontab
command.
To automate the backup script using cron and crontab, you can follow these steps:
Open the crontab file for the user that will be running the backup script. You can do this by typing
crontab -e
in the terminal.Add a new line at the end of the file with the schedule for running the backup script and the command to run it. For example, to run the backup script every day at 2 AM, you can add the following line:
0 2 * * * /path/to/backup/script.sh
This line specifies the minute (0), hour (2), and day of the month, month, and day of the week (the *
means "any value") when the script should be run.
- Save the crontab file and exit.
Now the backup script will be run automatically according to the schedule specified in the crontab file. You can modify the schedule or the command to run the script by editing the crontab file again using the crontab -e
command.
4.About User Management
User management in Linux involves creating, modifying, and deleting user accounts, as well as managing user permissions and privileges.
In Linux, each user has a unique user ID (UID) and a group ID (GID) that determine their permissions to access files and directories. User management commands can be used to add, modify, or delete users, set passwords, and modify user account settings.
Some common user management commands in Linux are:
useradd
: Used to create a new user accountpasswd
: Used to set or change a user's passwordusermod
: Used to modify user account settingsuserdel
: Used to delete a user accountgroupadd
: Used to create a new user groupgroupmod
: Used to modify user group settingsgroupdel
: Used to delete a user group
User management is an important aspect of system administration and is necessary for maintaining the security and integrity of a Linux system.
5.Create 2 users and just display their Usernames
To create two users in Linux and display their usernames, follow these steps:
Open a terminal window.
Use the
sudo
command to switch to the root user account:sudo su
Use the
useradd
command to create the first user:useradd -m user1
Set a password for the first user with the
passwd
command:passwd user1
Repeat steps 3 and 4 to create and set a password for the second user:
useradd -m user2
andpasswd user2
To display the usernames of the two users, use the
cut
command to extract the first field of the/etc/passwd
file, which contains the usernames:cut -d: -f1 /etc/passwd
The output of the command will display the usernames of the two users:
Copy codeuser1
user2