List Directories In Linux

Directories or folders are used to store different items like files, directories, folder data, etc. While working with these items we may need to list them especially directories. There are different commands those can be used to list directories. In this tutorial, we will learn how to list directories in Linux.

List Directories with ls Command

The ls command is the most popular and useful command to list files and directories. It provides a lot of options for different listing types. In this case, we use the ls command to list only directories. The ls command with the -d option is used to list only directories in the current working directory or path. The -d option is used to directories themselves not their contents so we provide “*/” as the path.

ls -d */

List Directories with ls and grep Command

The ls command output may provide a lot of details about the files and directories. One of them is the type of the item. The listed directories are tagged with the “d” letter in order to express this is a directory. We can use this information and use grep command in order to list only directories.

ls -l | grep '^d'
List Directories with ls and grep Command

From the output we can see that every line starts with the “d” lettern in order to express this is a directory. By using the grep command the “d” letter is colored as red.

We can also use the ls and grep command in order to list directories for different directories by using the absolute path. In the following example, we list directories located under the “/home/ismail“.

$ ls -l /home/ismail/ | grep '^d'

Create Alias To List Only Directories

If we require to list directories regularly typing these commands, again and again, is not so useful. We can create an alias for these commands which is simple. In the following example, the “lsdir” alias acts as a command. Add the following line to the user home directory “.bashrc” file.

alias lsdir = "ls -d */"

Now we can call the lsdir alias like below to list only directories for the current working path.

lsdir

List Directories with find Command

Another command to list directories is the find command. The find command is used to find directories and files and we can set the type as directory which lists all directories for the specified path.

$ find . -maxdepth 1 -type d -ls
List Directories with find Command
READ  Linux ls Command Sort Files By Size

Leave a Comment