Day 5 Task: Advanced Linux Shell Scripting

1.Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name and second is start number of directories and third is the end number of directories ) it creates a 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

#!/bin/bash

# Check if all 3 arguments are given
if [ $# -ne 3 ]
then
  echo "Error: Missing arguments"
  echo "Usage: createDirectories.sh <directory_name> <start_number> <end_number>"
  exit 1
fi

# Get the arguments
directory_name=$1
start=$2
end=$3

# Create directories with dynamic name
for (( i=$start; i<=$end; i++ ))
do
  dir_name="$directory_name$i"
  mkdir $dir_name
  echo "Directory created: $dir_name"
done

echo "Done"

2.Create a Script to backup all your work done till now.

Creating a script to backup all your work done till now is a good practice to ensure that your work is saved in case of any unforeseen circumstances.

To create a backup script, you can use Linux commands like cp, tar, or rsync to copy the necessary files and directories to a backup location. You can create a script using any text editor such as vi or vim.

Here is an example script to backup your work done till now:

#!/bin/bash

# Set the backup location and file name
backup_dir=/home/user/backup
backup_file=backup_$(date +%Y-%m-%d_%H-%M-%S).tar.gz

# List of directories to backup
backup_list=(
    /home/user/Documents
    /home/user/Projects
)

# Create the backup directory if it doesn't exist
mkdir -p $backup_dir

# Create the backup
tar -czvf $backup_dir/$backup_file ${backup_list[*]}

# Print the backup status
echo "Backup completed successfully!"
echo "Backup file: $backup_dir/$backup_file"

In this script, you can specify the backup location, file name, and the list of directories to backup. The script creates the backup directory if it doesn’t exist and uses the tar command to create a compressed archive of the specified directories.

You can run this script using the command ./backup_script.sh and it will create a backup file in the specified backup location with the current date and time in the file name.

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

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run at specified times or intervals. The cron daemon (crond) runs constantly in the background and checks the system’s crontab files at predefined intervals to find and execute scheduled commands.

Crontab is a configuration file that specifies the schedule for cron jobs. It contains instructions for the cron daemon about when and how often to execute commands. Each user on a Unix-like system can have their own crontab file, which can be edited using the crontab command.

To automate the backup script, we can add an entry to the crontab file specifying the schedule for the backup script to run. The syntax for a crontab entry consists of six fields that specify the minute, hour, day of the month, month, day of the week, and command to run. For example, the following crontab entry will run the backup script every day at midnight:

0 0 * * * /path/to/backup/script.sh

This entry specifies that the command (/path/to/backup/script.sh) should be run at 12:00am (midnight) every day (0 0 * * *).

4. what is user management on Linux.

User management on Linux is the process of creating, modifying, and deleting user accounts. As a multi-user operating system, Linux allows multiple users to access the system resources simultaneously. Each user has a unique username and password, which grants them access to their own files and system settings.

The most common user management tasks include:

  1. Creating a new user account: This involves setting up a new user account with a unique username and password. The user may also be assigned to a specific group or given special privileges.

  2. Modifying an existing user account: This can involve changing a user’s password, modifying their permissions or adding them to a new group.

  3. Deleting a user account: This involves removing a user account and all associated files and settings.

  4. Managing user groups: User groups are used to group together users with similar access requirements. Group management involves creating new groups, adding or removing users from groups, and modifying group permissions.

User management can be performed through the command line or through graphical user interface tools such as GNOME Users and Groups.

5.Create 2 users and just display their Usernames

To create a new user on Linux, you can use the adduser command followed by the username you want to create:

sudo adduser username

This will prompt you to enter the user’s password and some additional information such as full name, room number, etc.

To display a list of all users on the system, you can use the cat command to view the /etc/passwd file:

cat /etc/passwd

This will show you a list of all users on the system, along with their home directory, default shell, and other information. To display only the usernames, you can use the cut command to extract the first field (username) from each line:

cat /etc/passwd | cut -d: -f1