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. Show
How to List Directories & Subdirectories in LinuxYou can easily display directories & subdirectories in Linux using ls, tree, find, du command commands. 1. Using ls commandYou 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 commandYou can also use find command to list directories at a particular location using type -d option. $ In this case, find command will only display the directories and not the files like ls -R command. 3. Using tree commandtree 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. $ Here is the tree command to list folders & subfolders. $ sudo 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 commandYou 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. In this tutorial, we’ll discuss how to list only directories for a given path in Linux. There are several ways to list only directories in Linux and we will cover a few of them here. 2. Using ls CommandThe ls command is one of the most popular commands to list the contents of the directory. To only list directories, we can use this command with the ‘-d‘ option which only lists the directories in the current path and not their contents or sub-directories:
3. Using dir CommandThe dir command lists the directory contents. When we use this command with the ‘-d‘ option, it will only show the details about the directories in the current path:
4. Using find CommandThe find command searches for the file starting from the given starting point in a directory hierarchy. We can pass the expressions to this command to narrow down the search criteria. For instance, to list the file type directory, we can use ‘type‘ with the directory option ‘d‘. Also to avoid listing all the contents of subdirectories, we can limit the search level with the help of ‘maxdepth‘:
5. Using tree CommandThe tree command lists the contents of the directory in a depth indented tree-like format. We can use this command to show only directories with the help of the option ‘d‘. However, this may list all subdirectories and their contents, too. So we can use the level option (L) which limits the maximum display depth of the directory tree:
6. Using echo CommandAs we know that the echo command displays the string back to the standard output that we passed to it. We can also display the content of the directory using this command. If we try the ‘echo *‘ command, it will show all the contents of the current directory. Moreover, to only list directories we can use it like this:
7. Using grep with Other CommandsWhen we list the contents of the directory with the long listing option of ls command (ls -l) or dir command, we can see in the first column that regular files are shown as ‘–‘ and directories are shown as ‘d‘:
So from this output, we can filter directories with the help of the grep tool. Grep is just a Linux / Unix command-line tool used to search for a string of characters, so the option ‘^d‘ filters those strings that start with ‘d’. 7.1. Using grep with ls CommandIf we can list all files and directories with ‘ls -l‘ and pass them to grep, we can filter them to keep only directories like this:
7.2. Using grep with dir CommandWe can use the grep command with the dir command as well in a similar way as shown above to list directories:
8. ConclusionIn this article, we’ve discussed how to list only directories for any path in Linux. We’ve accomplished that using the commands ls, dir, find, tree, and echo. Authors Bottom If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines. Which command will find all the subdirectories within directories?To Search Subdirectories
To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.
How to find directory in Linux using ls command?Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.
Which command is used to show the directory and all subdirectories of files?The ls (list) command is used to display the names of the files and subdirectories in the current directory.
How will you count all subdirectories within a directory in Linux?The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count. However, in this case, we are using this command to count the number of files in a directory.
|