Open In App

Shell Script To Show Names of All Sub-Directories Present in Current Directory

Improve
Improve
Like Article
Like
Save
Share
Report

In this given program, we are required to write a shell script to print the list of all the sub-directories present in the current directory. Currently, when we are running this program, the terminal is opened at the root directory location and hence, we are getting a list of sub-directories present at the root level. Moreover, this script comes in handy when we only want to list the sub-directories and not any other files present at the same level of hierarchy.

Approach:

  1. Go to the root directory or any target directory where you want to get its sub-directories.
  2. Type in the following program.
  3. We are using ‘echo’ so that we get a simplified output.
  4. ‘ls’ command in Linux lists all the files and directories.
  5. ‘-d’ option in ls command specifies that we need to list only the directories.
  6. We are using the expression ‘*/’ so that we get only the directories under the current directory level and not going any level in depth further.

Program:

# Shell Script to list all sub-directories
# present in a current folder

# echo prints a message for user on screen
echo "List of sub-directories present in this Folder - "

# Following command lists all sub directories
# '*/' will only match directories under the current directory
    
ls -d */

Output:

Listing all sub-directories at root level


Last Updated : 09 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads