Listing only directories in UNIX

前端 未结 20 725
青春惊慌失措
青春惊慌失措 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 09:59

    Long listing of directories

    • ls -l | grep '^d'
    • ls -l | grep "^d"

    Listing directories
    - ls -d */

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

    Try this:

    find . -maxdepth 1 -type d
    
    0 讨论(0)
  • 2020-12-12 10:02

    You can use ls -d */ or tree -d

    Another solution would be globbing but this depends on the shell you are using and if globbing for directories is supported.

    For example ZSH:

    zsh # ls *(/)
    
    0 讨论(0)
  • 2020-12-12 10:02
    ### If you need full path of dir and list selective dir with "name" of dir(or dir_prefix*):
    find $(pwd) -maxdepth 1 -type d -name "SL*"
    
    0 讨论(0)
  • 2020-12-12 10:02

    Here's another solution that shows linked directories. I slightly prefer it because it's a subset of the "normal" ls -l output:

    ls -1d */ | rev | cut -c2- | rev | xargs ls -ld --color=always
    
    0 讨论(0)
  • 2020-12-12 10:06

    The following

    find * -maxdepth 0 -type d
    

    basically filters the expansion of '*', i.e. all entries in the current dir, by the -type d condition.

    Advantage is that, output is same as ls -1 *, but only with directories and entries do not start with a dot

    0 讨论(0)
提交回复
热议问题