Anyone who has used command line on a terminal must have used the linux command “ls” to list the files and directories in a directory. Simple use of “ls” command will list directory and files in a given directory.
Often you might want to know only about the directories in a directory.
One can use the command “ls” with “-d” option to get directories within a directory. Here is how to get the list of directories
ls -d */
Remember that the above command will not function recursively. This will just list all the directories in the current directory. For example, if a current directory contains both files and directories, we can use
ls -lt
to get the list of files and directories like this
Here we have two files and three directories (highlighted in red). To list only the directories, if we use
ls -d */
we get the list of directories as below
Using “ls -d */” only lists the names of the directories. However, if you need more information about each of the directories, we can use
ls -ld */
And we will get the following results