list files and directories

How to List Directories & Subdirectories in Linux

Sometimes you may need to view or display directories & subdirectories only in a recursive manner in Linux. In this article, we will look at the different commands to list directories & subdirectories in Linux.


How to List Directories & Subdirectories in Linux

You can easily display directories & subdirectories in Linux using ls, tree, find, du command commands.


1. Using ls command

You can recursively list directories & subdirectories using ls -R command.

$ sudo ls -R
.:
project.txt progress.txt

 ./data:
 file1.txt file2.txt file3.txt

The above command lists files in present folder followed by separate sections for each subfolder. Within each section, it lists files in that subfolder.

If you do not specify any folder then ls command will recursively display folders & subfolders in present working directory. If you want to list directories & subdirectories of a specific folder (e.g. /home/ubuntu) mention it after ls command.

$ sudo ls -R /home/ubuntu

If you don’t want a recursive search but simply want to see all the folders & subfolders in a location, then you can pass the output of ls -all command to grep and filter it to list only directories, as shown below.

$ sudo ls -all | grep drw
drwxr-xr-x 18 ubuntu ubuntu     4096 Jul  8 02:33 .
drwxr-xr-x  3 root   root       4096 Jan 14  2020 ..
drwxrwxr-x  2 ubuntu ubuntu     4096 Sep  2  2020 .aws
drwx------  4 ubuntu ubuntu     4096 Feb  7  2020 .cache

The output of ls -all command displays file permissions of each file & directory. File permissions of directories start with ‘d’ while those of files start with ‘-‘. So when we use grep command with drw search string, it will show only directories.


2. Using find command

You can also use find command to list directories at a particular location using type -d option.

$ find . -type d #present location
$ find /home/data -type d #directories in /home/data

In this case, find command will only display the directories and not the files like ls -R command.


3. Using tree command

tree command can also be used to get list of directories & subdirectories. It is not installed by default in Linux. You can install it using following command.

$ sudo apt-get install tree

Here is the tree command to list folders & subfolders.

$ sudo tree . -d 

If you want to list folders & subfolders in a specific location, replace . above with that folder path e.g /home/data

$ sudo tree /home/data -d


4. Using du command

You can also use du command to list folders & subfolders in Linux.

$ sudo du -a .
$ sudo du -a /home/data 

In this article, we have looked at 4 different ways to list directories & subdirectories in Linux. Please note, that for find & tree command you need to mention folder location before options. In case of ls and du command you need to mention location after options.

Also read:

How to Prevent Cross-Site Scripting in Apache/PHP
Shell Script to Replace Text in File
NGINX Block File Extensions
How to Deploy React App on NGINX
How to Monitor NGINX Log with Ngxtop

Leave a Reply

Your email address will not be published.