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

Nikunj VaishnavNikunj Vaishnav
3 min read

All 90 directories were created within seconds using a simple command:

mkdir 2025
cd 2025
ls -lrth
mkdir Day{1..90}
ls

Result:

Create Directories Using Shell Script:

Write a bash script foldercreate1.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 ./foldercreate1.sh day 1 90, it creates 90 directories as day1 day2 day3 ... day90.

# Ask to user for first value
echo "Please provide starting number:" $1
echo "Please provide last number:" $2
echo "Please provide name for folder:" $3

numfirst=$1
numlast=$2
name=$3

cd ..
mkdir 2028
cd 2028
echo "$(pwd)"

for ((i=numfirst; i<=numlast; i++));
do
        mkdir "$name_$i"
done

Result:

Write a bash script foldercreate.sh that, ask value from user (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.

Example 2: When executed as ./foldercreate.sh, it is ask Start number end number and directory name from user and it creates directories as day1 day2 day3 ... day90.

#!/bin/bash

# Ask to user for first value
read -p "Please provide starting number:" numfirst
read -p "Please provide last number:" numlast
read -p "Please provide name for folder:" name

cd ..
mkdir 2027
cd 2027
echo "$(pwd)"

for ((i=numfirst; i<=numlast; i++));
do
        mkdir "$name$i"
done

Result:

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

#!/bin/bash

echo "Please provide starting number:" $1
echo "Please provide last number:" $2
echo "Please provide name for folder:" $3

numfirst=$1
numlast=$2
name=$3

cd ..
mkdir Movies2028
cd Movies2028
echo "$(pwd)"

for ((i=numfirst; i<=numlast; i++));
do
        mkdir "$name$i"
done

Result:

Create a Script to Backup All Your Work:

#!/bin/bash

#This Script is for Backup folder of my Generated Scripts Files.

echo "$(pwd)"

cd ..

echo "$(pwd)" && ls

backup_dir="backup_$(date +%Y%m%d)"
mkdir "$backup_dir"

cp myscript/*.sh "$(pwd)/$backup_dir"

echo "Backup complete. Files copied to $backup_dir"

la $(pwd)/$backup_dir/

Result:

Crontab Refer this my blog for more details.

User Administration Refer this my blog for more details.

Conclusion

Mastering advanced Linux shell scripting is an essential skill for DevOps engineers, particularly in the realm of user management and automation. By leveraging scripts to create directories dynamically and automate backups, engineers can significantly enhance their productivity and ensure efficient system management. The examples provided demonstrate the power and flexibility of shell scripting in handling repetitive tasks, ultimately contributing to a more streamlined and effective DevOps workflow.

Connect and Follow Me On Socials :

LinkedIn|Twitter|GitHub

Like👍 | Share📲 | Comment💭

0
Subscribe to my newsletter

Read articles from Nikunj Vaishnav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nikunj Vaishnav
Nikunj Vaishnav

👋 Hi there! I'm Nikunj Vaishnav, a passionate QA engineer Cloud, and DevOps. I thrive on exploring new technologies and sharing my journey through code. From designing cloud infrastructures to ensuring software quality, I'm deeply involved in CI/CD pipelines, automated testing, and containerization with Docker. I'm always eager to grow in the ever-evolving fields of Software Testing, Cloud and DevOps. My goal is to simplify complex concepts, offer practical tips on automation and testing, and inspire others in the tech community. Let's connect, learn, and build high-quality software together! 📝 Check out my blog for tutorials and insights on cloud infrastructure, QA best practices, and DevOps. Feel free to reach out – I’m always open to discussions, collaborations, and feedback!