Day 5: 90DaysOfChallenge
Advanced Linux Shell Scripting for DevOps Engineers with User management
We are going to learn how to create dynamic subdirectories within seconds using for loop or commands, create a script to backup all our scripts till now, learn about Cron and Crontab, automate the backup scripts using crontab, learn about user management, create users and display their Usernames. So, let's get started:
Write a Shell Script to create multiple directories using for loop or command
Using for-loop: Example 1: When the script is executed as
./
createDirectories.sh
day 1 90
then it creates 90 directories as day1 day2 day3 .... day90
We can see the below shell script where in the first ifelse statement it checks whether the number of arguments provided is three or not. If it is not three, it prints the error message and terminates the execution. Ex - if two input is provided, it throws the error message and doesn't proceed further. In the second part, the code to create multiple directories is written using for loop.
Using command: Also, we can do the same process using command. The syntax is mkdir movie{20..50}. The number of folders to be created is written inside curly braces with the starting number followed by two dots and the closing number. Ex-
Create a Script to back up all your work done till now.
Below is the script for backing up data. Firstly, we will be creating a folder where all our backups will be stored. Now, in shell editor, we will give the source and destination file path. We will be saving the name of the file with the help of the date command to maintain the uniqueness of the file. Now, we will concatenate the file name with the target directory path and the date using .tgz extension. A TGZ file is a compressed archive created using GZIP, an open-source data compression program. It's popular among UNIX and Linux users for combining several files to make them easier to share. And at last, we will backup the data using tar and czf command.
In Linux, the
tar
command is used to create, view, extract, and manipulate tar archives. A tar archive is a collection of files and directories that are combined into a single file. Thetar
command has various options and arguments that allow you to perform different operations on tar archives.The specific command
tar czf
is a combination of options used to create a compressed tar archive. Let's break down the components:c
: This option stands for "create." It tellstar
to create a new archive.z
: This option enables compression using gzip. Gzip is a popular compression algorithm used to reduce the size of the archive, making it more efficient to store and transfer.f
: This option is used to specify the filename of the resulting archive. Thef
option must be followed by the name of the archive file.
So, when we use the tar czf
command, we are telling tar
to create a new tar archive, compress it using gzip, and save it to the specified filename.
Hence, after executing the below script, a new compressed tgz file will be created in the current directory, containing the contents of the source directory and all its subdirectories.
What is Cron and Crontab? Write a Shell Script to automate the backup.
"Cron" is a service that allows users to schedule and automate the execution of tasks or commands at specific intervals, such as hourly, daily, weekly, or even on a customized schedule. These scheduled tasks are often referred to as "cron jobs". The cron
service runs in the background and checks the system's crontab
files regularly to determine if any tasks need to be executed at the current time.
"Crontab" refers to the configuration file that contains the list of scheduled tasks for a specific user. Each user can have their own crontab file, and the tasks defined in the crontab will be executed under that user's context.
To edit a user's crontab, the crontab
command is used. Here are some common crontab
commands:
crontab -e
: This command opens the user's crontab in the default text editor, allowing you to add or modify scheduled tasks.crontab -l
: This command lists the contents of the user's crontab without editing it.crontab -r
: This command removes the user's crontab, effectively cancelling all scheduled tasks for that user.The syntax for scheduling tasks in a crontab is as follows:
Shell script to automate the backup-
First, we will give crontab -e to open the editor and give the below command i.e. b*ash <BackUpFileName> >> <FilePathName>
We can see that backup is automatically created once the time crosses the given time.
User Management in Linux
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards.
Getting userid: Using id command, one can get the ID of any username.
For example. to get the user id, you would use the following command:
id username.
Creating Users: New user accounts can be created using the
useradd
command. For example, to create a new user named "john," you would use the following command:useradd john
Password Management: User passwords are stored in the
/etc/shadow
file, which is only accessible by the root user. To set a password for a user, you can use thepasswd
command. For example, to set a password for "john," you would use:sudo passwd john
Modifying Users: To modify user account properties, you can use the
usermod
command. For example, to change the user's default shell:sudo usermod -s /bin/zsh john
.Deleting Users: To remove a user account, you can use the
userdel
command:sudo userdel john
.Create 2 users and just display their Usernames.
We can add the user using the below command -
-m is used so that the user is created with the directory. If -m is not used, the user will be created but without a directory. The user information is stored in the
/etc/passwd
file. Hence, by giving the command cat /etc/passwd, one can check whether the user is added or not means it would return all the user present in the directory. Ex-Thank you!
Shilpi~