DEV Community

Cover image for Day 5. Advanced Linux Shell Scripting for DevOps Engineers with User Management
Udoh Deborah
Udoh Deborah

Posted on

Day 5. Advanced Linux Shell Scripting for DevOps Engineers with User Management

Welcome to day 5 of the #90daysDevOpschallenge, our focus today is diving into advanced Linux shell scripting, focusing on advanced user management an integral part for anyone who's a DevOps Engineer.

On day 5, we will work on user management tasks like,

  • Create Directories using Shell Script (bulk user creation)
  • Create a Script to Backup All Your Work
  1. Create Directory

**

!/bin/bash

# Check if exactly 3 arguments are provided
if [ $# -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

# Assign arguments to variables
base_name=$1
start=$2
end=$3

# Loop through the range and create directories
for ((i=start; i<=end; i++)); do
  dir="${base_name}${i}"
  mkdir -p "$dir"
  echo "Created directory: $dir"
done

Enter fullscreen mode Exit fullscreen mode
./createDirectories.sh day 1 90

Enter fullscreen mode Exit fullscreen mode
./createDirectories.sh Movie 20 50

Enter fullscreen mode Exit fullscreen mode

Output

Created directory: day1
Created directory: day2
...
Created directory: day90
Enter fullscreen mode Exit fullscreen mode

The script automatically creates a series of directories using a base name and a range of numbers.

  1. Create Script to back up your work
#!/bin/bash
# Set variables
SOURCE_DIR="$HOME/work"                 # Change 'work' to your actual folder name
BACKUP_DIR="$HOME/backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="backup_$TIMESTAMP.tar.gz"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Create a compressed archive of the source directory
tar -czvf "$BACKUP_DIR/$BACKUP_FILE" "$SOURCE_DIR"

# Notify user
echo "✅ Backup complete: $BACKUP_DIR/$BACKUP_FILE"


Enter fullscreen mode Exit fullscreen mode

What This Script Does:
Looks for a directory named work in your home folder.

Compresses its contents into a .tar.gz archive.

Stores the backup in a backups folder (also in your home directory).

Names the backup file using the current date and time for easy identification.

  1. Create Users and Display their names
#!/bin/bash

# Create two users
sudo adduser user1
sudo adduser user2

# Display the created usernames
echo "Created Users:"
cut -d: -f1 /etc/passwd | grep -E 'user1|user2'

Enter fullscreen mode Exit fullscreen mode

Explanation

adduser creates a new user account.

/etc/passwd stores user account info.

cut -d: -f1 extracts just the usernames from each line.

grep -E 'user1|user2' filters to show only user1 and user2

  1. What is Cron? Cron is a time-based job scheduler in Unix/Linux systems. It lets you schedule tasks (called cron jobs) to run automatically at specified intervals — like daily, weekly, or even every minute.

What is Crontab?
Crontab (short for "cron table") is the file where you define these scheduled tasks. Each line in a crontab file represents a separate job and tells the system:

“Run this command at this time.”

Why is this Important in DevOps?
DevOps engineers use cron to automate repetitive tasks like:

  • Running backups

  • Cleaning up logs

  • Running health checks

  • Deploying scripts

* * * * * command_to_run
│ │ │ │ │
│ │ │ │ └─ Day of week (0 - 7) (Sunday = 0 or 7)
│ │ │ └─── Month (1 - 12)
│ │ └───── Day of month (1 - 31)
│ └─────── Hour (0 - 23)
└───────── Minute (0 - 59)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)