ls -d Linux Command Explained

Written by: Bobbin Zachariah   |   Last updated: March 13, 2024

The ls command is commonly used to list the contents of a directory. This list includes directories and files. What about if you need list directories themselves? Let's take a look at ls -d Linux command.

What does ls -d stand for?

The ls -d command is used to list directories themselves in the list. That means the -d option tells ls to display information about the directory itself not about the contents inside it.

For example, I have a directory named Projects which contains some files and directories. The ls Projects lists files and directories in that directory whereas ls -d Projects simply returns "Projects".

ls -d Projects
ls -d listing the directory itself

To check the permissions of the directory itself run ls -ld Projects

listing permissions of the directory itself

Examples of Using ls -d

Let's look into some use cases of ls -d with examples.

List all directories in the current location

Use ls -d */ to list all directories in the current location. Where ls -d print directories without content and */ tell the shell to match all directories.

Example:

ls -d */
list all directories in the current directory

The output confirms only directories in the current directory are listed.

Note: if you remove / at the end ie ls -d * then it will list all files and directories.

List all hidden directories

In the previous example, it listed all directories. What about hidden directories?

Use ls -d .*/ to list all hidden directories in the current location.

Example:

ls -d .*/
list all hidden directories

The output confirms that only hidden directories in the current directory are listed.

To include both hidden directories and non-hidden directories (i.e., all directories), run:

ls -d */ .*/
list all hidden and non-hidden directories

Combining ls -d with Other Commands

The ls -d command can be combined with other Linux commands to perform actions specifically on directories. Let me show you a few examples.

The most common use case is to count the number of directories in a directory.

ls -d */ | wc -l
ls -d command with wc -l command

The output shows there are a total of 12 directories in the current directory. Note hidden directories are not included.

We can combine grep with ls -d to search specific patterns of directories. Examples

ls -d */ | grep i "tem"
ls -d command with grep

This command searches for directories with the pattern "tem" case insensitively in the current directory.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment