Shell Script To Show Names of All Sub-Directories Present in Current Directory
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:
- Go to the root directory or any target directory where you want to get its sub-directories.
- Type in the following program.
- We are using ‘echo’ so that we get a simplified output.
- ‘ls’ command in Linux lists all the files and directories.
- ‘-d’ option in ls command specifies that we need to list only the directories.
- 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:
Contact Us