17

In my Ubuntu version the man ls has the next info about --directory:

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

So I'm a little confused how it works. I used the command ls --directory and I expected a list with all directories but instead I got .

So what exactly does ls --directory or ls -d do?

24
$ man ls
...
-d, --directory
              list directories themselves, not their contents

The current directory is represented as . so that's what ls -d is listing.

The directories inside the current directory are contents of the directory, and are therefore not shown with this option.

I use the -d option in an alias to display hidden files and directories

alias l.='ls -dC .* --color'

Without -d, this will list out the contents of the hidden directories too, which isn't what I want.

Another use for it is when I want to see metadata of a directory using the -l option, not its contents:

$ ls -ld playground
drwxr-xr-x 72 zanna zanna 12288 Mar  1 23:10 playground

If you want a list of directories in the current directory you can use

ls -d */
| improve this answer | |
  • ls -dC .* --color also lists hidden files not just directories. – Pau Chorro Mar 4 '17 at 17:31
  • 1
    yes @PauChorro I actually say that in my answer. I use it to display all hidden files and directories – Zanna Mar 4 '17 at 17:31
  • 1
    @PauChorro I added a way to list only directories – Zanna Mar 4 '17 at 17:36
  • 1
    In my Ubuntu version the man ls has the next info: list directory entries instead of contents, and do not derefer‐ ence symbolic links. So in your version is a better explanation of what it really does. – Pau Chorro Mar 4 '17 at 17:42
  • 1
    @Pilot6 it will list files too – Zanna Mar 4 '17 at 17:52
11

Using ls only with -d is almost useless. It gives information about the directory itself. It does not list its contents.

That is why you do not see a list of directories. The directories that you expected to see are the "contents" of the current directory

If you run simply ls -d it shows the current directory by ..

If makes sense to run it with other keys like -l.

ls -ld will show permissions of the current directory.

pilot6@Pilot6:~$ ls -ld
drwxrwxr-x 1 pilot6 pilot6 2570 Mar  4 12:14 .

You can also see permissions of any other directory like

ls -ld /bin

pilot6@Pilot6:~$ ls -ld /bin
drwxr-xr-x 1 root root 2584 Feb 25 15:19 /bin

If you run ls -l without -d you will have the list of all permissions for files and folders in the current directory. If you do not need it, that's a good example for ls -ld usage.

You can list directories in the current one by

ls -d */
| improve this answer | |
6

Using ls -d by itself is fairly useless because without a parameter it always returns .. After you specify a parameter it makes sense though. For example if your user name is rick and you want to see all the directories in your home use:

$ ls -d /home/rick/*/
/home/rick/AAC/        /home/rick/EnhanceIO/         /home/rick/silentcast/
/home/rick/assembly/   /home/rick/EnhanceIO-master/  /home/rick/Templates/
/home/rick/bin/        /home/rick/log/               /home/rick/test/
/home/rick/Desktop/    /home/rick/Music/             /home/rick/tmpe/
/home/rick/Documents/  /home/rick/Pictures/          /home/rick/Videos/
/home/rick/Downloads/  /home/rick/Public/
| improve this answer | |
5

Let me explain with examples:

  1. ls: list contents of a directory

  2. ls -d or ls --directory: list directories themselves, not their contents

Examples:

ls -d android-sdk-linux/

# result
android-sdk-linux/

ls android-sdk-linux/

# result
add-ons  build-tools  platforms  platform-tools  SDK Readme.txt  temp  tools

Your case of ls -d this says to list only show me the directory I am in not its content (i.e. . in Linux terms), so the result is: ..

But if you had said: ls, then you will see all that is present in the current directory ..

| improve this answer | |
4

ls -d list directories them self instead of their content. Files are listed as normal. Look into man lsto se description of all options.

. is the directory it self, the same you use when running a command from current directory, ./runscript.sh

.. is parent directory, the parent to the current directore, as in cd .. to move at step up in directory hieracy

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.