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.
ls -l with
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*