Skip to main content

Command Palette

Search for a command to run...

Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management.

Published
3 min read
Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management.
  • Example 1: When the script is executed as

./createDirectories.sh day 1 90

then it creates 90 directories as day1 day2 day3 .... day90

./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

#!/bin/bash

Folder_name=$1
start=$2
end=$3

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


  • Create a Script to Backup all your work done till now.

#!/bin/bash

source=/home/ubuntu/my_scripts
target=/home/ubuntu/devops/backup

tar -czvf $target/$1tar.gz $source

echo "backup completed"


  • Read About Cron and Crontab, to automate the backup Script

Cron is a utility in Unix-like operating systems that allows users to schedule and automate tasks to run at specified intervals. Crontab is a file that contains the commands or scripts to be executed by cron.

To automate a backup script using cron and crontab, you can follow these steps:

  1. Create a backup script: Write a shell script that performs the backup of your data.

  2. Set the file permissions: Ensure the script is executable by running the command chmod +x backup_script.sh.

  3. Open the crontab file: Run the command crontab -e to open the crontab file.

  4. Schedule the backup script: In the crontab file, specify the time and frequency at which you want the backup script to run. For example, to run the script daily at 2 am, add the following line to the crontab file:

0 2 * * * /path/to/backup_script.sh
  1. Save the crontab file: Save and close the crontab file.

Now, the backup script will run automatically at the scheduled time and frequency specified in the crontab file. You can check the log files to ensure that the backup script is running as expected.


  • User Management

In Linux, user management involves creating, modifying, and deleting user accounts. Here are some of the basic commands that you can use to manage users in Linux:

  1. Creating a user: You can create a user account using the useradd command. For example, to create a user named "john", run the command:
sudo useradd john
  1. Setting a password: To set a password for the new user, use the passwd command. For example:
sudo passwd john
  1. Modifying a user: You can modify a user's account details using the usermod command. For example, to add the user "john" to the "sudo" group, run the command:
sudo usermod -aG sudo john
  1. Deleting a user: To delete a user account, use the userdel command. For example, to delete the user "john", run the command:
sudo userdel john
  1. Listing users: To list all users on the system, use the cat command to read the /etc/passwd file. For example:
cat /etc/passwd

These are some of the basic commands for managing user accounts in Linux. It's important to note that user management can vary depending on the Linux distribution you're using.


  • Create 2 users and just display their Usernames

sudo useradd user1 (create user1)
sudo useradd user2 (create user2)
cat /etc/passwd | grep "username" (to find usernames)
cut -d: -f1 /etc/passwd | grep "^user" 
(to display:This command uses the cut command to extract the first field (i.e., the username) from the /etc/passwd file, and the grep command to filter out any usernames that don't start with the prefix "user")