27.1 C
Wednesday, May 12, 2021

Is List Only Directories Recursively in Linux? – Linux Hint

Must read

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.

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.

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

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:

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.

$ ls –l | egrep –v ‘^d’

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”.

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.

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/

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

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

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/

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/

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.


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

Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

- Advertisement -

Latest article