How to: ls only directories

ls only directories

As a developer or system administrator, you might often find yourself working with the command line interface (CLI). One of the most commonly used commands in the CLI is the ls command. This command is used to list the contents of a directory. However, sometimes you might only be interested in seeing the directories within a directory, and not the files. This is where the ls command comes in handy with the option to list only directories.

What is ls?

ls is a command in Unix and Unix-like operating systems that is used to list the contents of a directory. It is a basic command that is used frequently in the command line interface. The ls command can be used to list files and directories in a directory.

How to use ls to list only directories

The ls command can be used with various options to perform different tasks. To list only directories within a directory, we can use the -d option. The -d option instructs the ls command to list only directories.

For example, to list only directories in the current directory, you can use the following command:

ls -d */

The */ at the end of the command is a wildcard character that instructs the ls command to only list directories. The output of this command will be a list of directories in the current directory.

You can also use the ls command with the -l option to get a long listing of directories. The -l option provides detailed information about the directories, such as the permissions, ownership, size, and modification time.

For example, to list only directories in the current directory with a long listing, you can use the following command:

ls -ld */

The output of this command will be a list of directories with detailed information.

Conclusion

In conclusion, the ls command is a powerful tool that can be used to list the contents of a directory. The -d option is a useful option that can be used to list only directories within a directory. This option can be combined with other options, such as -l, to provide detailed information about the directories. By using the ls command with the -d option, you can save time and effort by only listing the directories you are interested in.