Table of contents
1.
Introduction
2.
Shell
2.1.
Shell Scripting
2.2.
Directory Management
3.
Implementation
4.
Frequently Asked Questions
4.1.
What is the extension in which the scripts are stored?
4.2.
Name some of the directory management functions.
4.3.
Are there any disadvantages of using shell scripts?
5.
Conclusion
Last Updated: Mar 27, 2024

Implementing Directory Management Using Shell Script

Author Ankit Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

In a world full of windows/mac operating systems, we still have many users working on Linux/Unix operating systems. There could be various reasons as Linux is comparatively more secure, fast, and highly customizable. This article will quickly understand how to implement the shell script's directory management functions. But, Before learning how to implement directory management using shell script, we must get familiar with the critical concepts of shell and shell scripting.

Let us begin by understanding what a shell is.

Also see, Multiprogramming vs Multitasking and Open Source Operating System

Shell

In the windows operating system, the system services are not accessible for the user, but in Linux/Unix, we get an option to do it. The Linux Shell is a particular user program that acts as an interface between the operating system and the user to use the system services.

It is an interpreter that inputs human-readable commands from the user and converts them into something that the operating system's kernel can understand.

The shell can be divided into two parts.

  1. Graphical shell
  2. Command-line shell

Let us now understand the shell scripts and shell scripting language.

Shell Scripting

We learned that a shell is a kind of interpreter that inputs command from the user. Sometimes, We may need to write so much of repetitive codes to execute the desired program. To avoid writing redundant codes, we create scripts, store the required code, and implement it in the shell according to our requirements. This method of using scripts in the shell is known as shell scripting.

There are many benefits of using shell scripting. Some of them are mentioned below.

  1. To avoid writing redundant code
  2. To monitor the system
  3. To add new functionality in the shell,

shell scripting can be understood as any other programming language, which helps in saving our time and many other things.

The syntax of shell scripts is similar to other programming languages and involves keywords, conditional statements, and functions.

The shell script files are stored in .sh extensions.

Let us now discuss the directory management functions.

Directory Management

The Directory is where the files and their details are stored. We have two types of directories, the first is the root directory, and then another one is the subdirectory. Manipulating directories like navigating, creating, and deleting a directory constitutes the management functions.

Let us look at the directory management functions given below.

  1. Navigation
  2. Listing directories
  3. Creating directories
  4. Modifying directories
  5. Absolute/Relative pathnames

Let us see a shell script that implements the directory management functions.

You can also read about the Multilevel Feedback Queue Scheduling.

Implementation

We can execute the below script to implement the directory management functions using the Linux commands.

#!/bin/sh 

# stores the value input by the user. So that user has the ability to navigate 

choice=0   

# between different options

while [ $choice -lt 4 ] # if user inputs any number greater than 3 we’ll exit.

do

    clear # clears the console

    echo "***** Directory Management *****"

    echo " "

    echo "Select one option: "

    echo "1. Modification."

    echo "2. Listing."

    echo "3. Exit."

    read choice # user input

    case $choice in 

    1)

        clear

        echo "****** Modification menu ******"

        echo ""

        echo "Select one option: "

        echo "1. Create."

        echo "2. Delete."

        echo "3. Rename."

        echo "4. Move."

        echo "5. Copy."

        echo "6. Exit from current mode..."

        read mdChoice

        case $mdChoice in

        1) echo ""

            echo "Name for new Directory: " 

            #Stores new directory name

            read dirName 

            #To create new directory with the name specified by the user

            mkdir $dirName 

        ;;

        2) echo ""

            echo "Enter name of directory to delete: "

            #gets name of the directory which need to be deleted

            read dirName 

            #delete directory

            rmdir $dirName 

        ;;

        3) echo ""

            echo "Enter name of old directory: " 

            #gets old directory name from user

            read oldDirName 

            echo "Enter new name: "

            #new directory name

            read newDirName 

            mv $oldDirName $newDirName #rename old directory with the new name given by user

        ;;

        4) echo ""

            echo "Enter name of directory to move: " 

            read dirName

            echo "Enter target: "

            read target

            # create new directory

            mkdir $target 

            #moves the directory to above created folder.

            mv $dirName $target 

        ;;

        5) echo ""

            echo "Enter name of directory to copy: " 

            read dirName

            echo "Enter target: "

            read target

            # create new directory

            mkdir $target 

            #copies the directory to above created folder.

            cp $dirName $target 

        ;;

        6) echo "Exiting from modification mode..."

            # don’t do anything if user selects this option

        ;;

        esac

    ;;

    2) clear

        echo ""

        echo "****** Listing menu ******"

        echo "Select one option: " 

        echo "1. List directories"

        echo "2. List directories with details."

        echo "3. Exit."

        read lsChoice

        case $lsChoice in

        1) echo ""

        #list all directories in the current path

        ls

        echo "Press Enter to continue.."

        # Waits for user to press enter

        read wait 

        ;;

        2) echo ""

            #list all directories with additional information

            ls -la 

            echo "Press Enter to continue.."

            read wait

        ;;

        3) echo ""

            echo "Exiting..."  

        ;;

        esac

    ;;

    3) echo "Exiting..."

        exit

    ;;

    esac

done

Output

Output

It is the first screen that appears when the script is executed.

User can select different options from the list.

Output

The above screen will appear if user selects option 1 (Modification)

Output
Output

This screen will appear if the user selects the option2 (listing)

Users can interact with different options and when the user is done interacting

Output

They can choose the last option (Exit) which will take the user back to the main menu

You need to execute the scripts with the help of output screenshots.

Let us now see some of the frequently asked questions.

Must Read Evolution of Operating System

Frequently Asked Questions

What is the extension in which the scripts are stored?

The scripts are stored in the .sh extension.

Name some of the directory management functions.

Some of the functions are listed below:
Navigation
Modification
Creating

Are there any disadvantages of using shell scripts?

The disadvantages of using shell scripts are given below:
Slower the execution of commands
Vulnerability to errors increases.

Let us now summarize this blog.

Conclusion

In This article, we discussed what a shell is, what a script is, and what shell scripting is in the Linux environment. It was then followed by discussing some of the advantages of shell scripting and its implementation. Then we learned about the directory management functions, and we also saw how with the help of shell scripts, we could implement directory management functions in the Linux environment. To better understand, we also attached outputs of the scripts when We executed them.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass