How to list just directories

September 19th, 2007 mysurface Posted in Bash, column, find, grep, ls, Text Manipulation | Hits: 95793 | 12 Comments »

There are no such direct ways to list only directories using the UNIX/ linux famous command ls. Anyway with command combo’s, we can achieve the goal in two ways shown as below.

1. use ls -l with grep
2. use find -type d

( The statement above is WRONG, check out the simple way to list just directories with ls -d )

The simple way to list just directories, is using ls -l pipe with grep

ls -l | grep "^d" 

Simply list all the files in details an grep only the line start with character d, it works just fine.

But what if I want the directories listing result like ls ?
We can use find for that, find is powerful enough that it allows you to customize the result output. With help of column, the find result is similar to ls besides the color.

find -L -maxdepth 1 -type d -name '*' ! -name '.*' -printf '%f\n' | column

-L is to show also for the symbolic link to directory, -maxdepth limit the search up to only 1 level, -type d indicates that the file must be directory.

-name ‘*’ ! -name ‘.*’ list all the directories but don’t list the hidden one.

At last -printf ‘%f\n’ | column, print out the directory name and display it in columns.

In order to come out with something more convenient to use, instead of remembering such long command line, I have created a bash script, name as lsdir.

Download lsdir.

Simply download it to /usr/bin, change the name to lsdir and then change the file mode to executable.

mv lsdir.txt /usr/bin/lsdir; chmod +x /usr/bin/lsdir

lsdir supports few options, kinda refers to the usage and the limitation of lsdir as below:


USAGE: lsdir [OPTION] [PATH]

OPTION:
 -r,      recursively list all the directories, including hidden directories.
 -l,      detail list of directories.
 -s,      list also hidden directories.
 -v,      verbose, display the acture find command line.
 -h,      help, display the usage.

LIMITATION:
 1. lsdir do NOT support options separation well,
    the line below may NOT work as expected.
        lsdir -l -s ~/
    Options have to specified together , i.g. lsdir -ls ~/

 2. Not like ls, options have to be specified before PATH,
    the line below may NOT work as expected.
        lsdir ~/ -l

 3. PATH do NOT support regular expression.
    the line below do NOT work as expected.
        lsdir ~/b*

12 Responses to “How to list just directories”

  1. Another way using grep and ls to list all directories:
    ls -dp * | grep /

    or to list all non-directories:
    ls -dp * | grep -v /

  2. I usually use that option with the ls alone, as:

    ls -d */

    eNjoy

  3. Ntropia:
    Great! and that is far more better, thank you. With that, lsdir is ridiculously stupid. x.x

  4. [...] have wrote a post stated that there are no direct ways to list just directories, and wrote a bash scripts using find [...]

  5. [...] have wrote a post stated that there are no direct ways to list just directories, and wrote a bash scripts using find [...]

  6. IT work very nice!!! thx.

  7. Another way of doing that with ls and grep.

    Listing only directories:
    ls -l | grep “^d” | awk ‘{ print $8 }’ | column

    Listing only files:
    ls -l | grep -v “^d” | awk ‘{ print $8 }’ | column

    Great example of the find command! Keep this good work!

  8. Matt Thomas Says:

    I made somewhat extreme script to list dot dir or files with less option. Try it out if you want – it works for me.

    #!/bin/sh

    # file: ./lsdot

    # show dot directories or dot files

    # how the script works
    #
    # the script performs tests on the arguments received
    # the tests determine if a directory has been provided
    # and/or specific flags have been received which produce specific output
    #
    # the output is a user choice of dot directories or dot files

    # a key is used to store the configuration which will be used
    # to produce specific output to stdout (the screen)
    #
    # set a default key of { 0110 }
    # the key offsets are represented left to right {1,2,3,4}
    #
    # 1) 1=use directory argument, 0=use current directory
    # 2) 1=show dot files
    # 3) 1=show dot directories
    # 4) 1=pipe output to less
    #
    # set key default: show all dot files and directories not using less
    key=”0110″

    # process each of the shell args received by the script

    # set the default dir to a variable
    adir=”.”

    # if a dir arg has been received by the script it
    # will be determined in the case statement below
    # where all flags are tested
    # any arg that is not a flag is assumed to be a dir
    # after which it is tested to confirm it is a dir
    # and the adir variable is updated

    # cycle thru each shell arg testing for matches
    for itm in $@
    do
    case “$itm” in
    -f) key=`echo $key | sed ‘s/./1/2′`
    key=`echo $key | sed ‘s/./0/3′`;;
    -d) key=`echo $key | sed ‘s/./1/3′`
    key=`echo $key | sed ‘s/./0/2′`;;
    -l) key=`echo $key | sed ‘s/./1/4′`;;
    -fl|-lf) key=`echo $key | sed ‘s/./1/2′`
    key=`echo $key | sed ‘s/./0/3′`
    key=`echo $key | sed ‘s/./1/4′`;;
    -dl|-ld) key=`echo $key | sed ‘s/./1/3′`
    key=`echo $key | sed ‘s/./0/2′`
    key=`echo $key | sed ‘s/./1/4′`;;
    *) if [ -d "$itm" ]
    then
    adir=”$itm”
    fi
    ;;
    esac
    done
    # – - – - – - – - – - – - – - – - – - – - – - – -

    # list directory contents per options

    # this is where the key is compared to 6 possible
    # output combinations and then the dir or file
    # listing is displayed to stdout
    #
    # the key bits and what they represent
    # 1) 1=use directory argument, 0=use current directory
    # 2) 1=show dot files
    # 3) 1=show dot directories
    # 4) 1=pipe output to less

    case “$key” in
    0110) ls -lap $adir | grep -e ‘ \.’;;

    0100) ls -lap $adir | grep -e ‘ \.’ | sed -e ‘/\//d’;;

    0010) ls -lap $adir | grep -e ‘ \.’ | grep /;;

    0111) ls -lap $adir | grep -e ‘ \.’ | less;;

    0101) ls -lap $adir | grep -e ‘ \.’ | sed -e ‘/\//d’ | less;;

    0011) ls -lap $adir | grep -e ‘ \.’ | grep / | less;;

    esac
    # – - – - – - – - – - – - – - – - – - – - – - – -

  9. Unfortunately this lsdir script falls for one thing:
    $ lsdir Serj\ Tankian\ -\ Elect\ The\ Dead/
    find: `Serj’: No such file or directory
    find: `Tankian’: No such file or directory
    find: `-’: No such file or directory
    find: `Elect’: No such file or directory
    find: `The’: No such file or directory
    find: `Dead/’: No such file or directory
    While in the same time script:
    #!/bin/sh
    ls -p “$1″ | grep /$
    basically does the thing. Key is that $1 needs to be putted in “” to work with spaces in paths…

  10. Juliann Lockerman Says:

    Really like the song!: Chemical

  11. How do you make this site look this good. Email me if you want and share your wisdom. Id be thankful.

  12. My buddy suggested I might this way blog. This individual used to be entirely right. This text basically created my day. You can not envision basically the way a ton occasion I had put together put in with this data! Many thanks!

Leave a Reply