How to Display Directories Only Under Linux Shells?


We know that the ls command under Linux displays the list of files and directories under the current directory. If you want to display the directories by excluding the files, you can do it via the following methods.

Suppose, we have a few files and directories under the current directory:

1
2
root@uploadbeta:/var/www/test# ls
a  b  c  Makefile  test.c
root@uploadbeta:/var/www/test# ls
a  b  c  Makefile  test.c

ls -d */

the -d option list directory entries instead of files, and it does not dereference the symbolic links.

1
2
root@uploadbeta:/var/www/test# ls -d */
a/  b/  c/
root@uploadbeta:/var/www/test# ls -d */
a/  b/  c/

You could show the hidden directories by (hidden files/directories start with dot):

1
ls -d .*/
ls -d .*/

ls and grep

The option -F (or –classify) appends the indicator to the entries, so the directories are appended with forward slashes and files are appended with the asterisk. We justneed to grep and match it.

1
2
3
4
5
6
root@uploadbeta:/var/www/test# ls -F
a/  b/  c/  Makefile*  test.c*
root@uploadbeta:/var/www/test# ls -F | grep /
a/
b/
c/
root@uploadbeta:/var/www/test# ls -F
a/  b/  c/  Makefile*  test.c*
root@uploadbeta:/var/www/test# ls -F | grep /
a/
b/
c/

THe alternative is to do ls -l which lists one entry per line, and the directories will be printed with a leading ‘d’ attribute, so we can grep this using regular expression:

1
2
3
4
5
6
7
8
9
10
11
12
root@uploadbeta:/var/www/test# ls -l
total 20
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 a
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 b
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 c
-rwxrwxrwx 1 lychee lychee   71 Jun 23 20:45 Makefile
-rwxrwxrwx 1 lychee lychee   69 Jun 23 20:35 test.c
 
root@uploadbeta:/var/www/test# ls -l | grep ^d
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 a
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 b
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 c
root@uploadbeta:/var/www/test# ls -l
total 20
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 a
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 b
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 c
-rwxrwxrwx 1 lychee lychee   71 Jun 23 20:45 Makefile
-rwxrwxrwx 1 lychee lychee   69 Jun 23 20:35 test.c

root@uploadbeta:/var/www/test# ls -l | grep ^d
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 a
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 b
drwxr-sr-x 2 root   lychee 4096 Aug 30 10:15 c

Using tree utility

The tree is not installed by default, so you can install it using sudo apt-get install tree or sudo yum install tree

1
2
3
4
5
root@uploadbeta:/var/www/test# tree -dL 1
.
├── a
├── b
└── c
root@uploadbeta:/var/www/test# tree -dL 1
.
├── a
├── b
└── c

The -d displays the directories only while the -L 1 shows only current level (not the sub folders)

Using find

The find command is powerful, and you can show directories only by:

1
2
3
4
5
root@uploadbeta:/var/www/test# find -maxdepth 1 -type d
.
./a
./c
./b
root@uploadbeta:/var/www/test# find -maxdepth 1 -type d
.
./a
./c
./b

-maxdepth 1 ignores sub folders and -type d only finds directories (ignores files and other types e.g. links).

bash-shellshock How to Display Directories Only Under Linux Shells? BASH Shell linux linux shell tricks

bash-shellshock

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
509 words
Last Post: How to Define Lambda Functions in C++11?
Next Post: C/C++ Find the Difference of Two Lowercase Strings

The Permanent URL is: How to Display Directories Only Under Linux Shells?

Leave a Reply