0

I am struggling to understand how ls -d works. I've read through the other ls -d questions but none of them ask why ls -d * doesn't work the same way as ls -d */. Why is the separator character at the end so important?

ls * and ls */ behave the same way, so I was expecting ls -d * and ls -d */ to work the same way as well.

Instead, ls -d * lists all files in current directory just like doing ls, whereas ls -d */ lists the directory in the current directory. Why is this so?

2 Answers 2

4

ls * and ls */ behave the same way.

No, they don't. If you have files in your working directory, they will be listed with the first command, but not the second.

ls will list the files and directories (including their content) passed on the command line. If you use shell globbing your shell will replace the patterns "with an alphabetically sorted list of filenames" (c.f. Filename Expansion).

The pattern * will match every file and directory in the current directory (excluding hidden files starting with .), while */ will only match directories (excluding hidden directories).

The switch -d disables the listing of contents for directories but files passed to ls will still get listed.

1

The answer is: the ending / forces ls to only look at directories.

Please note that behaviour is consistent (with and without -d).

A small test:

mkdir /tmp/test
touch /tmp/test/a
mkdir /tmp/test/b
touch /tmp/test/b/c
cd /tmp/test

And then do ls *and ls */ and you will notice the difference.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .