How to list only directories in Unix or Linux

We were asked recently by a customer how to list only the directories when doing an ls command

Firstly ls lists all files in a directory so an ls would produce something like this

ls /tmp

storage.log ifcfg.log packaging.log

if we now wanted to list only directories we would issue the command

ls -d */

this would give something like this

systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC/

systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH/

systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd/

if we now wanted to see permissions,ownerships and dates we would do the following

for files we would issue the command

ls -la

this would give something similar to the following

-rw-r–r–. 1 root mousse 0 Sep 22 11:53 ifcfg.log

-rw-r–r–. 1 root mousse 0 Sep 22 11:53 packaging.log

-rw-r–r–. 1 root mousse 0 Sep 22 11:53 storage.log

from the above we can see that the files are owned by root and the group is mousse and from the permissions we can see that root has read and write on these files, the group mousse has read and everyone else all has read too.

if we wanted to do this for directories we would issue the following command

ls -dal */

this would give us something similar to the following

drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC

drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH

drew——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd

From this we can see the directories are owned by root with read write and execute permissions however the group mousse and everyone else has no rights at all.this means no one apart from root can gain access to this directory or anything in it.

Now if we wanted to be able to look at these files or directories with the additional information of size and date order we would issue the following

for files

ls -larth

this would give something similar to the following

-rw-r–r–. 1 root root 107 Sep 22 11:54 storage.log

-rw-r–r–. 1 root root 108 Sep 22 11:57 packaging.log

-rw-r–r–. 1 root root 57 Sep 22 11:59 ifcfg.log

as you can see the sizes are listed plus the date and time that were last written too handy if you have an error and you can see which log was written to last good place to start your detective work.

Now if we do the same for directories but this time i will choose the /var directory where logs are written as an example

ls -larthd /var */

You should get something similar to this

drwxr-xr-x. 2 mickey mousse 6 Sep 22 12:06 hsperfdata_nb1302/

drwxr-xr-x. 2 root mousse 18 Sep 22 12:07 hsperfdata_root/

drwx——. 2 mickey mousse 51 Sep 22 16:41 mozilla_nb13020/

drwx——. 2 mickey mousse 6 Sep 22 16:45 tracker-extract-files.10000/

drwxr-xr-x. 20 root mousse 4.0K Sep 30 11:04 /var

drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd/

drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH/

drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC/

drwx——. 2 mickey mousse 23 Sep 30 11:05 ssh-ZbJbou1039oX/

hope this helps. Hopefully we will be publishing more tips soon feel free to contact us through our web page www.blueshell.im or follow us on twitter or our Facebook page

This entry was posted in Tech Tips. Bookmark the permalink.

11 Responses to How to list only directories in Unix or Linux

  1. Naazmi says:

    Alright thanks, I checked for that just now and it turns out that the only directory is the root folder.

  2. Thomas says:

    The answers given until now do not take into account that the file list passed from find to du may be so long that find automatically splits the list into chunks …

  3. Norbert Lanteigne says:

    With thanks! Valuable information!

  4. Emmitt Living says:

    With thanks! Valuable information!

  5. Gene Farwick says:

    With thanks! Valuable information!

  6. Duane says:

    Tutorial on using find, a UNIX and Linux command for walking a file hierarchy. … Linux and Unix find command tutorial with examples … George Ornbo is a hacker, …

  7. Gregorio Amormino says:

    With thanks! Valuable information!

  8. Jessica says:

    How do I find all files containing specific text on Linux? … How to search for a text in specific files in unix. 6 … rev 2018.10.5.31856

    • nb1302 says:

      try using grep as Linux is case sensitive I would suggest changing to the directory that contains the files you wish to search then use something like this , for example if we are searching for any files that contain the expression Duck

      grep -i duck *

      Hope that helps or are you wanting to search multiple directories ?

Comments are closed.