Any Reason for Using "*/" in Command "Ls -D */" to List Directories?

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?

2

2 Answers

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.

1

If you use ls -d *, then you will see not just directories, but also files. If you use ls -d */, you will only see directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest