15

I entered the man page of ls, the entry for the -d option is as follows:

-d, --directory list directory entries instead of contents, and do not dereference symbolic links

So, I thought the ls -d command will display all directories within a given directory. However I entered one directory and tried two different commands:

  1. obtaining directories with: ls -l | grep ^d: this one worked, all 7 directories being displayed

  2. obtaining directories with ls -d: this does not display the 7 directories, only a point .

I do not understand if I am using the option wrongly or if I am misunderstanding its meaning, what is the actual meaning and usage for the -d option ?

4
  • 5
    It makes more sence when you specify file-names. e.g. ls -d * compared to ls *. (ls -d is same as ls -d .) Feb 23, 2015 at 23:12
  • @richard : you should make that an answer.
    – tink
    Feb 23, 2015 at 23:13
  • ls -d doesn't do what you expect. If you want to print only directories then find is right tool, or if you use zsh you may try print *(/). What you can do with pure ls is to group directories first with... --group-directories-first option.
    – jimmij
    Feb 23, 2015 at 23:27
  • ls */ will portably list directories in the current directory. Similarly, for dir in ./*/ will loop over directories only,
    – tripleee
    Apr 6, 2022 at 9:01

4 Answers 4

18

ls -d shows information about a directory or symbolic link - with this information being (in simple terms) its respective path. The logical assumption is that the d stands for directory, since it's most basic definition in UNIX terminology I've come across is 'lists directories'.

This can seem on the surface to not be that useful; say you currently reside within a directory with 2 subdirectories:

documents music

Using ls documents with no options would give you a listing of the contents of documents as you correctly state above. Using ls -d documents will merely print the name - since the path to that folder, relative to where you currently reside - is the same as it's name - documents! This can be expanded on (though somewhat redundant in it's basic usage) to ls -d $PWD/* which will display all files and folders, but with their working directories (hence the PWD - previous working directories).

It does have a useful function when combined with the * operator - ls -d */ will display ONLY the directories from within your current working directory.

Turning the focus to the symbolic link part of the definition, if you were to use ls -d ~- a tilde expansion - it would print the path of the current HOME directory. This could then be used with varying applications (dependent on the OS) and does have some practical use; you could modify your statement further to ls -d ~your-username, ls -d ~another-username or ls -d ~root and be provided with the HOME path for those users. A further example of this can be seen in the apache server environment, with the use of a username here similarly displaying the path to their hosting space (their HOME directory, relatively speaking).

There are various functions that can be used in combination with this, but the above covers the core function behind the operator - in short, -d is an operator to display specifically directory/path information. There's a nice code generator to play with the ls function (including some -d functionality) if you want to investigate further: ls code generator.

2
  • Ahh I see. So basically it's best useful for when it's used with a wildcard ie *. Otherwise for a literal file/directory it really doesn't do much
    – Honey
    Mar 30, 2020 at 11:36
  • @Honey Yep that's pretty much it - in simple terms, the -d is more of a specification to target/ouput directories information - and the vanilla output without any additional string or operators is 'display information on current directory' - namely the path - which gives us our . response. Mar 31, 2020 at 13:12
10

ls by itself, with no options or anything, will "list the current directory." By default, "listing a directory" refers to the files that are contained in it.

The -d flag changes ls not to list the files within the specified directory, but to list the information about the directory itself. It is not, as you mistakenly thought, referring to "directories in the current directory." It is only referring to "the current directory."

This is why ls -d only returns . .

1
  • How can this information be used?
    – KSK
    Jun 1, 2023 at 10:05
6

Not sure about the terminology but ls -d just lists info about the thing that follows and nothing more, ie it does NOT expand directory lists, so:

ls -ld /usr/bin

will just give you one line of output about /usr/bin itself.

And

ls -ld *

will just give you info about each file or directory in the pwd but won't also expand any of the directories to list their contents.

As already mentioned you can use find to list directories, and still use ls -d if you like:

find /usr/bin -type d -exec ls -ld {} \;

that's to find all directories under /usr/bin in case it's not clear.

3

ls -d is same as ls -d . Before executing the ls command, bash shell will expand any wild cards. The -d option instructs ls to just list the information about the passed argument and not its contents regardless of the argument is a directory or file.

To explain further, take the example of ls .*; the expectation is that it will list all files and directories that have . as the prefix. But, when bash expands the wildcard * it includes . and .. as well. You can verify the output of bash expansion by checking output of echo .* command. For each argument ls .* received after bash expanded the * (which includes . and ..) , it will list the contents if the argument is a directory. The result is some output that is not what you expected.

If you use ls -d .* instead, the '-d' option instructs to list details of only the argument and not its contents even if it is a directory. Now this gives you the output you expected.

1
  • 1
    I have read the use case where ls -d .* being used for listing hidden file (and anything that begin with 'dot') in the current directory but never understand why. But now I do! thanks @Ravi Kumar.
    – apollo
    May 13, 2019 at 2:59

You must log in to answer this question.

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