What is the meaning of ls -d ?
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.
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.
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 .
.