why ls -d can't get the directories

Discussion in 'General Linux' started by loryliu, Jul 22, 2013.

  1. loryliu

    loryliu New Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    in the man page of ls, -d means list the dorectories
    when I issue ls -d ,I get nothing.
    Anyone has an idea

  2. Jarret W. Buse

    Jarret W. Buse Active Member Staff Writer

    Messages:
    142
    Likes Received:
    172
    Trophy Points:
    43
    Best Answer
    The ls -d command requires a filename to list. to make this work, you try: ls -d */.

    This will list only the directories by themselves. If you drop the -d, the directories are listed separately with the files located within them.
    DevynCJohnson and JasKinasis like this.
  3. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,143
    Likes Received:
    386
    Trophy Points:
    83
    where are you performing the command? Lets say that you are in /home/username. And for some reason your homefolder is empty. It won't show anything. Also, sometimes if you are performing the command in a directory that you do not have read permission, you won't see anything either.
  4. McPhee

    McPhee New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    Hi lorylui,

    The -d option to the ls command will work the way you expect it to if you provide it a directory name.
    Eg:
    Code:
    $ ls -ld scripts
    drwxr-xr-x 2 user user 4096 Jul 17 21:12 scripts
    This will show you the the directory itself rather than it's contents.

    A work around to this would be:
    Code:
    ls -la|grep ^d
    Hope this helps.

    McPhee
  5. Rodrigo

    Rodrigo New Member

    Messages:
    22
    Likes Received:
    9
    Trophy Points:
    3
    Hi loryliu try this:

    Code:
    ls -F | grep /
    I know that exist a better way, but it's another form to list all directories.
  6. KenJackson

    KenJackson Active Member

    Messages:
    129
    Likes Received:
    28
    Trophy Points:
    28
    My lsd function will probably give you the results you're looking for. I find it so handy that I keep it in my ~/.bashrc on every machine I have access to.

    The trick is ${D:+/}. If you specify a directory, it expands to a slash. If not, then it does not add a leading slash. So lsd shows you all the subdirectories in the current directory, or the directory you specify.

    Code:
    function lsd { local D="${1:-}"; command ls -d "${D%/}${D:+/}"*/; }
    jadhav.sudeep likes this.
  7. Minty-Linux

    Minty-Linux New Member

    Messages:
    13
    Likes Received:
    6
    Trophy Points:
    1
    in my terminal "ls" shows my directories as blue, and executables as Green, and normal files as light blue.
  8. Jarret W. Buse

    Jarret W. Buse Active Member Staff Writer

    Messages:
    142
    Likes Received:
    172
    Trophy Points:
    43
    Best Answer
    The ls -d command requires a filename to list. to make this work, you try: ls -d */.

    This will list only the directories by themselves. If you drop the -d, the directories are listed separately with the files located within them.
    DevynCJohnson and JasKinasis like this.

Share This Page