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.
ls -d *
compared tols *
. (ls -d
is same asls -d .
) – ctrl-alt-delor Feb 23 '15 at 23:12ls -d
doesn't do what you expect. If you want to print only directories thenfind
is right tool, or if you usezsh
you may tryprint *(/)
. What you can do with purels
is to group directories first with...--group-directories-first
option. – jimmij Feb 23 '15 at 23:27ls */
will portably list directories in the current directory. Similarly,for dir in ./*/
will loop over directories only, – tripleee Apr 06 '22 at 09:01