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. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    889
    Likes Received:
    288
    Trophy Points:
    63
    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.
  3. 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
  4. Rodrigo

    Rodrigo New Member

    Messages:
    18
    Likes Received:
    8
    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.
  5. KenJackson

    KenJackson Active Member

    Messages:
    129
    Likes Received:
    27
    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.
  6. 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.

Share This Page