Listing only directories in UNIX

前端 未结 20 727
青春惊慌失措
青春惊慌失措 2020-12-12 09:40

I want to list only the directories in specified path (ls doesn\'t have such option). Also, can this be done with a single line command?

相关标签:
20条回答
  • 2020-12-12 10:06

    If I have this directory:

    ls -l

    lrwxrwxrwx  1 nagios nagios     11 août   2 18:46 conf_nagios -> /etc/icinga
    -rw-------  1 nagios nagios 724930 août  15 21:00 dead.letter
    -rw-r--r--  1 nagios nagios  12312 août  23 00:13 icinga.log
    -rw-r--r--  1 nagios nagios   8323 août  23 00:12 icinga.log.gz
    drwxr-xr-x  2 nagios nagios   4096 août  23 16:36 tmp
    

    To get all directories, use -L to resolve links:

    ls -lL | grep '^d'

    drwxr-xr-x 5 nagios nagios   4096 août  15 21:22 conf_nagios
    drwxr-xr-x 2 nagios nagios   4096 août  23 16:41 tmp
    

    Without -L:

    ls -l | grep '^d'

    drwxr-xr-x 2 nagios nagios   4096 août  23 16:41 tmp
    

    conf_nagios directory is missing.

    0 讨论(0)
  • 2020-12-12 10:08

    This has been working for me:

    `ls -F | grep /`
    

    (But, I am switching to echo */ as mentioned by @nos)

    0 讨论(0)
  • 2020-12-12 10:11

    Try this ls -d */ to list directories within the current directory

    0 讨论(0)
  • 2020-12-12 10:12
    find . -maxdepth 1 -type d -name [^\.]\* | sed 's:^\./::'
    
    0 讨论(0)
  • 2020-12-12 10:12

    The answer will depend on your shell.

    In zsh, for example, you can do the following:

    echo *(/)
    

    And all directories within the current working directory will be displayed.

    See man zshexpn for more information.

    An alternative approach would be to use find(1), which should work on most Unix flavours:

    find . -maxdepth 1 -type d -print  
    

    find(1) has many uses, so I'd definitely recommend man find.

    0 讨论(0)
  • 2020-12-12 10:14

    Since there are dozens of ways to do it, here is another one:

    tree -d -L 1 -i --noreport
    
    • -d: directories
    • -L: depth of the tree (hence 1, our working directory)
    • -i: no indentation, print names only
    • --noreport: do not report information at the end of the tree listing
    0 讨论(0)
提交回复
热议问题