Any reason for using "*/" in command "ls -d */" to list directories?

Issue

I know there are some other ways to do the same thing, such as

ls -l | grep "^d"

or

ls -F | grep "/$"

I am just curious about the reason for adding */ after ls -d. Why simply using ls -d not work? Is there any story or tricky stuff behind it?

Solution

Adding the -d flag simply instructs ls to simply list directory entries rather than their contents. The * given to ls is expanded to all the entries in the current directory, both files and dirs. So ls -d * will list all entries in this directory, without expanding the subdirectories. But if you use */, then bash expands this to only include the directories in this directory. But with just ls */, all the directories will be expanded. Adding the -d flag prevents that, and you get just the directories in this directory.

Answered By – lxop

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published