Go2Linux | Linux Operating System

A site dedicated to: Linux Operating System

How to list only directories names on Linux

Date: 2010-04-05 00:00:00 -0400

This is a small tip, but a useful one also, to list only the directories in Linux use this command.

ls -d ./*/

That will have an output like this

./ConsoleKit/       ./bash_completion.d/  ./cron.daily/    ./default/   ./gnome-vfs-2.0/  ./iptables/      ./logrotate.d/   ./nginx/     ./php/       ./profile.d/  ./skel/   ./vbox/
./ODBCDataSources/  ./bluetooth/          ./cron.hourly/   ./fail2ban/  

There are other ways, if you want it in a single column, use this ones:

ls -l | awk ' /^d/ {print $9}'

You will get something like this:

ConsoleKit
ODBCDataSources
PolicyKit
X11
alsa
avahi
bash_completion.d
bluetooth
bonobo-activation
ca-certificates
conf.d
cron.d
cron.daily
cron.hourly
cron.monthly
cron.weekly
cups
dbus-1
default
fail2ban

To list only files use this:

ls -l | grep -v /

Without the -v to get only directories.

From the man page, we have:

       -F, --classify
              append indicator (one of */=>@|) to entries

So you can also use this:

ls -F | grep /

For directories and:

ls -F | grep -v /

For files

ls -F | grep @

For symbolic links

And so on, that one is one of my favorites, which is yours?

If you liked this article please share it.

powered by TinyLetter

If you want to contact me in any other way, please use the contact page.