While troubleshooting issues, organizing files, and permission management the listing of the directories or files is the most important factor in Linux/ Ubuntu. Sometimes it is necessary to list the files in a directory but not the directory directly. However, in some cases, users may need to list down the directories only. For that particular purpose, different commands are used in Linux/Ubuntu, which will be discussed in detail in this article.

How Do I List Only Directories in Ubuntu 22.04?

There are different ways to list the directories in Ubuntu/ Linux, among them the most frequently used methods are listed below:

  • Use “ls” command with different options to list directories
  • Use “find” command to list directories
  • Use “tree” command to list directories
  • Use “du” command to list the files in a directory
  • Use “echo” command to list the directories
  • Use “grep” command to list the directories
  • Use “dir” command to list the directories

Use “ls” Command with Different Options to List Directories

Using the “ls” command to list the directories in Linux/ Ubuntu is the most straightforward way. This command gives the names of directories as well as files. To get only the names of directories, we have to execute the ls command with the “-d” flag as shown in the following snippet.

ls -d */

In the above command 

  • ls” lists all the directories and files.
  • -d */”  fore the “ls” to list only directories.

The above command with a little modification used to list the directory and this command also gives additional information about the directories such as its read-write capability, when this folder was created, and owner information. The command to be executed is given as

ls -l -d */

In the above command

  • ls -l” lists all the directories including files in a manageable form.
  • -d */” is used to remove the files from the output and shows only directories.

To list the directories in a more specific way use the list command. This command gives the output results in more detail such as the number of items in a directory, its read-write capability, when this folder was created, and owner information. The command to list the directories is given as 

ls -l

In the below given command flag “-la” is for the long listing of directories. The command used to list the directory, subdirectories, files, and hidden files is given as

ls -la

Use “lR” flag to List the Directories 

Applying an operation on the files shows the property of recursive. By using the “-lR” flag the detailed output result will be obtained such as the number of files in the subdirectories, its read-write capability, when this folder was created, and owner information. Flag  “-l” gives the result in tabular format and “-R” gives the list of directories and other information.  For this purpose, the command to be executed is given as

ls -lR

To list the files from the specific directory use the above command with a little modification, by adding the location of the files. For example, list the files in the directory “My_files” which lies in the home directory execute the command given as

ls -lR /home/tahir/My_files/

Use “find” Command to List Directories

The most powerful command used in Linux to list the directories is “find”.  This command lists all the files in the directories and its subdirectories. This command is helpful in finding the files when you have huge data meaning thousands of files and folders. The command to list the directories is given as.

find . -type d

In the above command

  • find” search files from the start point directory hierarchy.
  • -type” used to list the type of directory by using “d”.

Use the “find” command with little modification in different ways such as giving the location of files to list the files in the directory given as

find /home/tahir/My_files/ -print -ls

In the above command 

  • /home/tahir/My_files” is the path of the files in the directory
  • -print” shows the address of every file.
  • -ls” gives the details of the files in a directory. 

In the above command, “ls” flag is used to take some extra information about the files. Now use the same command by avoiding the use of “ls”. 

find /home/tahir/My_files/ -print

Use “tree” Command to List Directories

This is another way to list the directories in Linux/ Ubuntu. This is a stand-alone command used without any flag and gives the output results in a hierarchical way. The below-given result shows that there is no file in the first four directories and the fifth directory “My_files” contains four files. The command to list the directories is given as

tree /home/tahir/

With a little modification in the above command by giving the location of the files in a directory the below command is derived out.

tree /home/tahir/My_files/

Use “du” command to list the Directories

The most sophisticated way to list the directories in Linux/ Ubuntu is “du” command. You can use this command in different ways as the below result shows that by using the different command you go to the desired directory where you use this command to see the list of files. The executed command is given as

du -a

In the above command 

  • du” gives the information of disk usage.
  • -a” lists down the directories.

You can directly list the files in the directories by giving the location of the files.  

du -a /home/tahir/My_files/

Use “echo” Command to List the Directories

The most simple command to list the directories in Linux/Ubuntu is the “echo” command. This command only gives the directories as output. The command executes to list the directories is given as

echo */

The command used to display the files in the current directory is given as.

echo *

Use “grep” Command to list the Directories

To list only directories with detailed output the “grep” command is used. The command is to be executed for the purpose of listing only the directories.

ls -l | grep '^d'

In the above command 

  • To list only the directories, pass the “ls -l” command to “grep”. 

Now use the grep command with dir to show the directories. The command to be executed for this purpose is given as

dir | grep '^D'

Use “dir” Command to list the Directories

The “dir” command is used to list the files within the directories

dir -d */

In the above command 

  • dir” gives the files within a directory.
  • -d” flag with “dir” lists the directories in the current path.

Conclusion

To list the directories in Linux/ Ubuntu there are different ways such as Using “ls” command with different options to list directories, using “find” command to list directories, using “tree” command to list directories, and using “du” command to list the files in a directory. Among all these methods the most important and easy one is using the “ls” command because we can use this command in different ways such as “ls -lR” which gives a very detailed view of the directory and subdirectories.