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.
- 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.
# 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 */
Listing all sub-directories at root level
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!