Day 5 of #90daysofdevops Advanced Linux Shell Scripting for DevOps Engineers with User Management
Tags: shellscripting, linux, cloudcomputing, devops
Published: 2024-06-05 12:59:41

**1. Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name and the second is the start number of directories and the third is the end number of directories ) it creates specified number of directories with a dynamic directory name.**
Write a shell script to create 10 directory day{1..10}
create file` createDirectories.sh`

```
 #!/bin/bash
 directory_name=$1
 start=$2
 end=$3

 for ((i=start; i<=end; i++))
 do
         mkdir "$directory_name$i"
 done
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lml7jq6ftuyblpz2xve1.png)

**2. create a Script to backup all your work done till now**
Creating a daily backup script involves copying or archiving important files to a backup location. Here's a simple example of a backup script using the rsync command, which is commonly used for file synchronization and backup:
 

```
#!/bin/bash

 # Source directory (the directory you want to back up)
 source_dir="/home/Priyanka"

 # Destination directory (the backup location)
 backup_dir="/home/backup"

 # Create a backup directory with the current date
 backup_subdir="$backup_dir/backup_$(date +\%Y-\%m-\%d)"
 mkdir -p "$backup_subdir"

 # Perform the backup using rsync
 rsync -av "$source_dir/" "$backup_subdir/"

 echo "Backup completed successfully to $backup_subdir."
```

Make the script executable:

chmod +x backup_script.sh

You can run this script daily using a scheduling tool like cron. For example, to run it every day at a specific time, you can add an entry to your crontab:

```
 crontab -e
```
The string 0 2 * is a cron expression that specifies when to run the command. In this case, it means "run the command at 2:00 AM every day".

```
 0 2 * * * /path/to/backup_script.sh
```

**3. Read About Cron and Crontab, to automate the backup Script**

**What is cron and crontab?**
cron is a time-based job scheduler in Linux and Unix-like operating systems. It runs in the background and is responsible for scheduling tasks or jobs (also called "cron jobs") to run at specific times and intervals.

crontab is the configuration file used to specify the cron jobs to be run. The name "crontab" comes from the combination of "cron" (the scheduler) and "table" (the configuration file).

The crontab file is a simple text file that contains a list of commands meant to be run at specified times. Each line of the crontab file represents a single cron job, and has six fields separated by spaces or tabs. These fields specify the minute, hour, day of the month, month, day of the week, and the command to be run

**4. Read about User Management**

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.

Simplifying User Management: 👨‍💼👩‍💼 User management in Linux involves creating and managing user accounts. To create two users and display their usernames:
**5. Create 2 users and just display their Usernames**

```
sudo useradd user1
sudo useradd user2

echo "User A: $(id -un user1)"
echo "User B: $(id -un user2)"
```