The word “recursive” refers to the fact that a Linux or Unix operating system commands deals with the contents of folders, and if a folder has subfolders and documents, the instruction still functions with all of those documents (recursively). It could be possible that the child directory has its own set of files and folders (for example, large folders), so on and so forth. You will use different Linux instructions to recursively traverse each folder before making it to the edge of the specified folder. At that level, Linux instructions return to a turnoff of the tree and repeat the process for any sub-folders that exist. In this tutorial, you will learn all the methods to list the directories recursively.

Examples of Simple List Directories

First of all, you must know how to list all the directories from your Linux system using the simple list command. Open the command-line terminal and try the below “ls’” command to list only the directories. It will not only list the directories but the total number of sub-folders and files residing in them. The output shows the folders with the information regarding their owner, group, read-write privileges, and the date of creation of folders.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-01.png" data-lazy- height="229" src="data:image/svg xml,” width=”612″>

To list all the directories and files within your Linux system, you have to use “-la” instead of simple “-l” in the list command. Hence, execute the updated command to do so. The output shows all the files and folders along with their additional information.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-02.png" data-lazy- height="508" src="data:image/svg xml,” width=”608″>

To simply list the directories without additional information, try the below command followed by the flag “-d”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-03.png" data-lazy- height="47" src="data:image/svg xml,” width=”877″>

You can also use the same command with little modification to list the folders along with their additional information. You just have to add the “-l” flag within this command as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-04.png" data-lazy- height="187" src="data:image/svg xml,” width=”587″>

To only check the total number of files in the specific home directory, try the below-presented command in the shell. You will get the total number of files in your Linux operating system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-05.png" data-lazy- height="50" src="data:image/svg xml,” width=”629″>

Example of Listing Directories Recursively

There are different types of recursive commands available in Linux Systems, which can be used to list the directories recursively. To understand each of them separately, assume you have 5 files in your Linux directory named “Documents”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-06.png" data-lazy- height="233" src="data:image/svg xml,” width=”974″>

List Recursively Using “-lR” Flag

To use the “-lR” flag in your query to recursively list all the directories of your Linux system is the first method to do so. Upon the execution of the list command below, it will list all the folders and their subfolders along with their bonus details, as shown in the output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-07.png" data-lazy- height="593" src="data:image/svg xml,” width=”860″>

Let us just recursively list all the files or folders from some specific folder or directory of your Linux system. For this purpose, add the location path of the specific directory to list it recursively. We want to recursively list all the files and folders of the directory “Documents” residing in the “home” directory using the below-updated command. The output displays all the files and folders within the “Documents” folder along with its extra information.

$ ls –lR /home/aqsayasin/Documents/

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-08.png" data-lazy- height="210" src="data:image/svg xml,” width=”974″>

List Recursively Using “Find” Command

Make it clear to yourself that not all Linux systems, e.g., macOS, Unix-like OS, have the choice of using “-R” to list the directories. In that case, there are other commands available to fulfill our needs which are the “find” and “print” command. In this command, we will givea path to a folder along with the leverage of using the “-ls” command. When “-ls” is being used in the command, this means it will also show the extra information regarding the residing files and folders of a specific directory. Try the below-stated command in the shell to recursively list all the files and folders of the directory “Documents”.

$ find /home/aqsayasin/Documents/ -print -ls

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-09.png" data-lazy- height="252" src="data:image/svg xml,” width=”974″>

Now, you have to try the very same instruction in the shell with a little change. We will avoid using the “-ls” flag in this command to stop showing the additional information regarding files. Execute the below-listed query in the shell, and you will have the displayed output.

$ find /home/aqsayasin/Documents/ -print

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-10.png" data-lazy- height="143" src="data:image/svg xml,” width=”691″>

List Recursively Using “-du” Command

Let’s have a look at another example to recursively list the files or directories with another new method. This time, we will be using the “-du” command to accomplish our purpose. This command contains the “-a flag in it. Along with the flags, we have to specify the directory location as well. Let us execute the below instruction in the terminal shell to recursively list all folders or files of the directory “Documents”. The output shows the 5 records for it, as shown below. You can see this command also shows the number of bites residing in each file.

$ du –a /home/aqsayasin/Documents/

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-11.png" data-lazy- height="147" src="data:image/svg xml,” width=”689″>

List Recursively Using “tree” Command

Last but not the least, we have a very simple and easy-to-use “tree” command method to recursively list the files or folders. We will not be using any flag in this command so far. You have to specify the directory location along with the keyword “tree”. Let us have an example of it using the below-listed command. The output shows the “tree” branched type output of the directory “Documents”. It shows 0 directories and 5 files in the output.

$ tree /home/aqsayasin/Documents/

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-12.png" data-lazy- height="183" src="data:image/svg xml,” width=”664″>

Let’s try the same “tree” command with a different location. This time we are recursively listing the folders of the ‘Home’ directory of the user “aqsayasin”. The output shows the branches of folder “Home” having a total of 14 different folders, sub-folders, and 5 files.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/list-only-directories-recursively-in-linux-13.png" data-lazy- height="468" src="data:image/svg xml,” width=”570″>

Conclusion

We have tried all the recursive methods to recursively list the directories and files, e.g., “-lR” flag, find and print, du, and tree command

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/Author-Image-150×150.jpg607e25f0a2b2c.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.