How to list just directories
September 19th, 2007 mysurface Posted in Bash, Text Manipulation, column, find, grep, ls | Hits: 24178 |
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.
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*
Live Chat!









September 20th, 2007 at 4:57 am
Another way using grep and ls to list all directories:
ls -dp * | grep /
or to list all non-directories:
ls -dp * | grep -v /
September 20th, 2007 at 6:26 pm
I usually use that option with the ls alone, as:
ls -d */
eNjoy
September 20th, 2007 at 11:34 pm
Ntropia:
Great! and that is far more better, thank you. With that, lsdir is ridiculously stupid. x.x
September 21st, 2007 at 12:24 am
[...] have wrote a post stated that there are no direct ways to list just directories, and wrote a bash scripts using find [...]
September 21st, 2007 at 5:55 pm
[...] have wrote a post stated that there are no direct ways to list just directories, and wrote a bash scripts using find [...]
December 5th, 2007 at 6:51 am
IT work very nice!!! thx.
June 2nd, 2008 at 5:46 am
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!
August 8th, 2008 at 2:36 am
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
# - - - - - - - - - - - - - - - - - - - - - - - -