How does the d option for the ls command exactly work?

Having the following simple structure:

 pom.xml  <--- unique regular file

If the current directory is project and if is executed the ls command appears

main  pom.xml  test

Until here all is OK. Now, according with man ls about the d option exists:

   -d, --directory

          list directories themselves, not their contents

Thus when is executed the ls -d command appears:


Why? To be honest I expected only the directories and not regular files. Such as

main test


  • How does the d option for the ls command exactly work?

Extra question:

  • What does not their contents exactly mean? Is it about of the internal content of each directory?
Asked By: Manuel Jordan


Normally when you ls a directory then the contents of the directory are shown.

e.g. in your example:

$ ls project
main  test
$ ls project/main
aaa  aab
$ ls project/test
dda  ddb

If you use the -d flag then the directory itself is listed

$ ls -d project

This sounds boring until you look at things like wildcards:

$ ls project/*
aaa  aab

dda  ddb

We can see that each directory matched we get a list of the files in those directories.

But with -d we just get the directory names themselves and not the files inside the directories.

$ ls -d project/*
project/main  project/test
Answered By: Stephen Harris

How does the d option for the ls command exactly work?

ls -d is about not traversing directories further. ls with no argument is equivalent to ls ., which is why you only see . as output. As an example of how it can be used:

$ mkdir -p {foo,bar,baz}/dir
$ ls *


$ ls -d *
bar  baz  foo

This can be particularly useful if you want to look at some of the directory metadata, for example, with ls -ld.

Answered By: Chris Down

You’re asking a very broad question. 
But the short answer to your puzzlement is:
the default argument for ls (when none is specified) is .
When you run ls with no options, you get the contents of .. 
When you run ls -d, you get ..

Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.