how to list folders or directories in linux

In Linux, everything is implemented as “Files“. That mostly means that they all share several “common” features/properties while having some unique properties of their own. All files and folders are implemented the same way, as Files. That is a very abstract explanation of the whole thing and there are still some differences between file systems.

If you want to really understand how files and folders are implemented in your filesystem, then you can find what specific filesystems you are using and then look into the specific implementation of the same. Folders are just “special” files with certain specific properties. Any more detailed explanation of files and folders is probably beyond the scope of this post.

The important thing for you to remember that everything including files, folders, links and devices etc. etc. are all Files at its core. That helps to better understand how the whole thing works.

So, you want to list all folders or directories inside a folder. We will mainly deal with listing only folders (and not files) although these commands can list both files and folders. There are mainly two different commands you can use to list files and folders: ls and find.

Please remember that the output might work slightly different depending on the shell you use. I use fish by default, but the following examples should work in other shells as well, such as bash and zsh.

list all folders in a folder

When using ls to list folders, the command line option of consequence is -d. The -d option will list the directories by themselves, and not its content. This, however also depends on how you specify the path to the folder. So, if you want to all directories in the current working folder…

$ ls -d */

In order to specify another directory, which is not the current folder you will need to specify the path as below…ending with */

$ ls -d newfolder/*/

Another option is the find command. I find that the find command is much more versatile than ls itself, but it can be slower depending on the number of files you have in the folder. So, using find command to print out folders in the current directory.

$ find . -maxdepth 1 -type d

The -maxdepth command line option lets you control the depth of the folders you want to traverse. This can be an extremely powerful feature when you want to traverse the folder recursively. The -type option is used to specify what types of files you want listed. The d specifies that we want only directories. You can use f for files and l for symbolic links.

If you want to list first level folders in another directory, then substitute the path (.) in the above example with the path. Also, if you want to see the directory/file metadata such as date, permissions, user etc then tack on the -ls option at the end.

$ find newfolder/ -maxdepth 1 -type d -ls

list all folders and sub-folders

Now, let’s say you want to print out all folders and sub-folders inside a directory. In my opinion, the simplest and easiest command for this is find. You can use ls as well, but there is no straight forward way to print out only the directories without filtering the output by using either awk, grep or cut etc.

$ ls -R ./

The command line option -R or –recursive will allow you to recurse into each and every folder and its sub-folders. You can use pretty much the same find command in the previous section to print the folders and sub-folders.

$find myfolder/ -type d -ls

Now, you can easily filter the depth of the folders using the -maxdepth option. If you want to display only folders and sub-folder that are 3 levels deep, then

$ find path/to/folder -maxdepth 3 -type d

list folders by size

If you want to find the size of any particular folder, then it can easily be done with either ls or du commands. We will quickly deal with listing the folders along with the total size of the folder. We can then sort the output to find which folder is using the most space.

We use the du command to compute the size or space used by each listed folder first. We can then the sort command to sort the folders in decreasing order of size.

bash$ find ./ -maxdepth 3 -type d -exec du -s {} \; | sort -nr

You can leave out the maxdepth option if you want to list all sub-folders recursively.

The above syntax will work with bash. If you encounter an error with another shell, then refer to the shell documentation. For example, in fish you will need to either escape the “{” and “}” (ie.  \{\} ) or use quotes around it ‘{}’. The example below will work with both shells.

fish$ find ./ -maxdepth 3 -type d -exec du -s '{}' \; | sort -nr