Thursday, October 19, 2006

How to list only directories in Unix

Well this may seem a very simple problem, but its not. So it deserves a thread for sure

Traditional method

ls -l|grep ^d

But u get the other unwanted details, incase u are automating

Slightly Sophisticated

find . -type d

But on execution u will dicern that find is recursive, so "just the" folders of the current directory are not displayed

If its getting slightly complicated try this simpler solution.

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

Pretty neat.
This command works almost fine. But just in case the folder has a "space", the command fails to get the exact name of the folder.

Here is a solution which "WORKS" and is very simple

ls -p |grep /

Just an indicator of the power of the raw tools invented ages ago. KISS(Keep it Simple S......)

No comments: