The ls command in Linux is used for itemizing the contents of any listing.

By default, it lists all of the contents, be it a file or a listing or a hyperlink or a named pipe.

However what if you wish to record solely the directories? How do you that?

Like something in Linux, there are a number of methods to perform the identical activity. Itemizing solely the directories is not any completely different:

  • ls -d */
  • ls -l | grep ‘^d’
  • discover . -maxdepth 1 -type d
  • echo */
  • tree -d -L 1

Don’t fret. I am going to clarify issues intimately. Here is the content material of the listing I’m going to make use of within the examples right here:

List Only Directories in Linux With ls and Other Commands

Use ls command to record directories solely

It’s all the time good to do it with the acquainted ls command as a result of that is the command you utilize for displaying the content material of a listing.

To record solely the subdirectories, use the -d possibility with ls command like this:

ls -d */

Here is the output it reveals:

[[email protected] Paperwork]$ ls -d */
another_dir/ my_dir/

Why */? As a result of with out it, ls -d will solely return the listing title. The -d possibility record directories not its contents (which incorporates file, directories and many others).

The */ is a sample. With *, you record all of the content material (together with contents of the subdirectories) and the / restricts the sample to directories.

This image depicts the distinction fairly nicely.

List Only Directories in Linux With ls and Other Commands

It’s possible you’ll mix it with lengthy itemizing possibility -l and most different choices:

[[email protected] Paperwork]$ ls -ld */
drwxrwxr-x. 1 abhishek abhishek 16 Nov 7 18:22 another_dir/
drwxrwxr-x. 1 abhishek abhishek 44 Nov 7 18:22 my_dir/

If you don’t want the trailing slash (/) on the finish of the listing names, you should use the reduce command to chop it out:

[[email protected] Paperwork]$ ls -ld */ | reduce -f1 -d’/’
drwxrwxr-x. 1 abhishek abhishek 16 Nov 7 18:22 another_dir
drwxrwxr-x. 1 abhishek abhishek 44 Nov 7 18:22 my_dir

Listing solely subdirectories in a selected listing

The above command works within the present listing. What if you’re not in the identical listing?

On this scenario, you should use */ on the finish of the trail of the listing with ls -d:

ls -d Path/To/Dir/*/

Here is an instance the place I transfer out of the Paperwork listing after which record solely the directories inside Paperwork listing:

[[email protected] ~]$ ls -ld Paperwork/*/
drwxrwxr-x. 1 abhishek abhishek 16 Nov 7 18:22 Paperwork/another_dir/
drwxrwxr-x. 1 abhishek abhishek 44 Nov 7 18:22 Paperwork/my_dir/

Did you discover that it does not record the hidden listing? That is one shortcoming of this methodology. It’s possible you’ll use ls -d .*/ to show hidden directories, nevertheless it solely shows hidden directories.

Use mixture of ls and grep command

You’ll be able to all the time depend on the nice outdated grep command for filtering the output for particular content material.

If you happen to lengthy record the contents, you may determine the directories as a result of begin with d.

You need to use grep to filter the contents that begin with d:

ls -l | grep ‘^d’

However this offers you much more fields than simply the listing names:

[[email protected] Paperwork]$ ls -l | grep ‘^d’
drwxrwxr-x. 1 abhishek abhishek 16 Nov 7 18:22 another_dir
drwxrwxr-x. 1 abhishek abhishek 44 Nov 7 18:22 my_dir

Use discover command to record solely directories

Here is find out how to use the discover command to record solely the subdirectories:

discover directory_path -maxdepth 1 -type d

I hope you might be accustomed to the discover command. I am going to clarify it nonetheless.

With sort d, you ask the discover command to solely search for directories.

With maxdepth 1 you ask the discover command to maintain the search on the present stage solely (and never go contained in the subdirectories).

[[email protected] Paperwork]$ discover . -maxdepth 1 -type d
.
./my_dir
./another_dir
./.my_hidden_dir
List Only Directories in Linux With ls and Other Commands

As you may see within the output above, it additionally reveals the hidden listing.

Use tree command to record solely directories

In case your intention is to record solely the directories, you might also use the tree command.

By default, the tree command provides you the entire listing construction. You’ll be able to modify it to point out solely directories and solely on the present stage.

tree -dai -L 1

  • d – search for directories solely
  • a – search for hidden recordsdata and directories as nicely
  • i – take away the tree construction from the show
  • L 1 – do not go into the subdirectories

Here is the output:

[email protected] Paperwork]$ tree -dai -L 1
.
another_dir
my_dir
.my_hidden_dir

three directories
List Only Directories in Linux With ls and Other Commands

Utilizing echo command for itemizing directories

The unlikely candidate? You will be stunned to know that echo command in Linux may also be used for displaying the contents of a listing. Attempt utilizing echo * and see for your self.

Much like the ls command, you can too use the */ sample to record solely the directories within the present working listing.

echo */

Here is the output which is equivalent to what you bought with the ls -d command:

List Only Directories in Linux With ls and Other Commands

There could possibly be extra methods for itemizing solely the directories, not recordsdata. In truth, the strategies mentioned right here might have some ifs and buts based mostly on what you might be searching for.

In case your intention is to only show the directories, a lot of the instructions I mentioned would work. If you need one thing extra particular like solely getting the directories title with slash and many others, you will should do some formatting by yourself.

I hope you discover this Linux tip useful. Questions and options are all the time welcome.

bash list directories into array,ls list only file names,ls -d */,list current directory linux,list directories python,linux list directories and subdirectories,list all files in a directory linux,ls -l command explained,ls-al command in linux,linux change directory,what is use of who command in linux,linux list current directory,ls – l command in linux,ls command in linux geeksforgeeks,ls command in windows,cd command in windows,pwd command in windows,in cut, which two options come together?,ls output explained,ls terminal,dd (unix),"ls –version",ls – ltr,use of ls,ls command output description,list directories in linux,unix command to list all directories and subdirectories,ls command in linux,how to list only files in unix