3

Having the following simple structure:

project
 main
  aaa
  aaa 
 test
  ddd
  ddd 
 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

Question

  • 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?
Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40

3 Answers3

5

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
project

This sounds boring until you look at things like wildcards:

$ ls project/*
project/main:
aaa  aab

project/test: 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
  • Just being curious, if ls -d is the same as ls -d . - so why the dot is not interpreted as the current directory as relative path and is not shown the main and test directories? – Manuel Jordan Mar 13 '24 at 13:47
  • 1
    ls -d . would display the . directory and not the contents of the . directory; You can see this if you combine with l flag; eg ls -ld . will show permissions and inode and stuff for the directory itself. – Stephen Harris Mar 13 '24 at 15:54
  • Thank You. Now is more clear that point, if you consider necessary consider to add it to your answer. – Manuel Jordan Mar 13 '24 at 19:34
  • Just in case, at Ubuntu Desktop 22.04, the ls -d project/* command returns project/main project/pom.xml project/test. Therefore consider to fix that part and explain why appears the pom.xml file – Manuel Jordan Mar 13 '24 at 23:06
5

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 *
bar:
dir

baz: dir

foo: dir $ 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.

Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • Just being curious, if ls -d is the same as ls -d . - so why the dot is not interpreted as the current directory as relative path and is not shown the main and test directories? – Manuel Jordan Mar 13 '24 at 13:48
  • 1
    @ManuelJordan Could you please clarify? I'm not sure I understand the question. . is a directory, so it follows the same rules as if you used the absolute path. – Chris Down Mar 13 '24 at 14:28
3

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 ..

  • Just being curious, if ls -d is the same as ls -d . - so why the dot is not interpreted as the current directory as relative path and is not shown the main and test directories? – Manuel Jordan Mar 13 '24 at 13:48
  • 1
    What do you mean? The dot *is interpreted* as the current directory. – G-Man Says 'Reinstate Monica' Mar 13 '24 at 13:54
  • But if pwd returns the current project location why either ls -d or ls -d . does not return the main and test as content? Why appears again the .? I am assuming it is related about . and .. that exists in each directory - Am I correct? – Manuel Jordan Mar 13 '24 at 13:56
  • 2
    It's right there in your question: -d tells ls to list directories themselves, not their contents.  This has nothing to do with ... When you say “ls -d” or “ls -d .”, you're saying “tell me about ., not its content.”  You may gain a better understanding of what -d does if you try “ls -l”, “ls -la” and “ls -ld”. – G-Man Says 'Reinstate Monica' Mar 13 '24 at 14:10