Advanced Linux Shell Scripting

Advanced Linux Shell Scripting

  1. Create Directories Using Shell Script

    Q. Write a bash script createDirectories.sh that, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.

    Example 1: When executed as ./createDirectories.sh day 1 90, it creates 90 directories as day1 day2 day3 ... day90.

    Example 2: When executed as ./createDirectories.sh Movie 20 50, it creates 31 directories as Movie20 Movie21 Movie22 ... Movie50.

#!/bin/bash

# Taking input from user 
read -p "Enter Directory name: " dirName
read -p "Enter number from directory starts: " sNum
read -p "Enter number of directory end: " eNum

# for loop to create directories
for ((i=sNum; i<=eNum; i++))
do
        mkdir "${dirName}${i}"
done

echo "Directories created successfully."
#Output after running script
Enter Directory name: dir
Enter number from directory starts: 1
Enter number of directory end: 10
Directories created successfully.
# 10 Directories created 
$ ls -l
total 44
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir1
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir10
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir2
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir3
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir4
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir5
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir6
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir7
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir8
drwxr-xr-x 2 shuhari shuhari 4096 Jul  3 10:10 dir9

  1. Create a Script to Backup All Your Work

#!/bin/bash

sDir=/home/shuhari/linux/demo/
dDir=/home/shuhari/backup

rsync -av --delete $sDir $dDir

echo "Backup successfully done."
#Output

$ ./backupScript.sh
sending incremental file list
created directory /home/shuhari/backup
./
file11.gif
file110.gif
file12.gif
file13.gif
file14.gif
file15.gif
file16.gif
file17.gif
file18.gif
file19.gif

sent 634 bytes  received 252 bytes  1,772.00 bytes/sec
Backup successfully done.
total size is 0  speedup is 0.00

  1. What is Cron?

  • In simple terms, cron is like an automated to-do list for your computer.

  • It allows you to schedule tasks to run automatically at specific times or intervals.

  • e.g. Think of it as setting an alarm or reminder for your computer to perform certain actions without you having to remember to do them manually.


  1. What is Crontab?

  • Crontab stands for "cron table" and refers to a file that contains a list of commands meant to be run at specified times.

  • These files are read by the cron daemon to determine which tasks to execute and when to execute them.

  • Each user on a Unix-like system can have their own crontab file, which they can edit to schedule their own tasks.

To configure cron job

$ crontab -e

To list down all crontab for current user

$ crontab -l

  1. User management in Linux

  • User management in Linux involves creating, modifying, and deleting user accounts, as well as managing user permissions and groups.

  • Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.

Step 1 : Create two users

$sudo useradd user1
$sudo useradd user2

Step 2 : Display usernames of users

$cut -d: -f1 /etc/passwd | grep "^user"

#Output
user1
user2

This command uses cut to extract the first field (username) from each line in /etc/passwd, then filters (grep) lines starting with "user". Adjust the grep pattern as needed based on your usernames or naming convention.