Operating System - HP-UX
1829102 Members
2371 Online
109986 Solutions
New Discussion

Command to list/find ONLY all directories.

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Command to list/find ONLY all directories.

I need to list or find only directories.
But the following command shows all files & subdirectories, instead of showing only directories.

find . -type d -exec ll {} \;

OR

The next command just list a dot “.” Only.
/tmp#ll -ld
drwxrwxrwx 60 bin bin 7168 Feb 15 13:33 .


Thanks,

Gulam.
Everyday Learning.
12 REPLIES 12
Patrick Wallek
Honored Contributor

Re: Command to list/find ONLY all directories.

You are 99% there. A basic ll will list the contents of the directories it finds. You need to add the '-d' flag to your ll command.

# find . -type d -exec ll -d {} \;

will do precisely what you require.
RAC_1
Honored Contributor

Re: Command to list/find ONLY all directories.

You need to ll -d {} or you do not need -exec at all.

Just plain
find . -type d
works fine.
find . -type d -exec ll -d {} \;
Also works fine.

Why find??

ll -R . | grep ^dr
There is no substitute to HARDWORK
Gulam Mohiuddin
Regular Advisor

Re: Command to list/find ONLY all directories.

Thanks "find . -type d -exec ll -d {} \;" works, but it also list recursive directoires.

I just would to list the all dir without recursive directories.

Thanks,

Gulam.
Everyday Learning.
James R. Ferguson
Acclaimed Contributor

Re: Command to list/find ONLY all directories.

Hi Gulam:

'find' is designed to descend a directory tree.

If all you want to see are directories immediately beneath a mountpoint (directory) or beneath another directory, do:

# ls -l /tmp | grep "^d"

The caret (^) anchors the grep to the beginning of the line. Sice 'ls' reports directories with a leading "d", the above command should give you what you want.

Regards!

...JRF...
Gulam Mohiuddin
Regular Advisor

Re: Command to list/find ONLY all directories.

/tmp#ll |grep -c ^dr
58
/tmp#ll -R . | grep -c ^dr
201
tmp#find . -type d -exec ll -d {} \;|wc -l
202

Everyday Learning.
Rodney Hills
Honored Contributor
Solution

Re: Command to list/find ONLY all directories.

You can get a list of directories only and not descending into subdirectories by-

cd /tmp
find * -type d -prune -print

HTH

-- Rod Hills
There be dragons...
Rick Garland
Honored Contributor

Re: Command to list/find ONLY all directories.

find / -depth -exec ls -la {} \; | grep '^d'

Find everything on the system then pipe it through grep and listing only directories. Using the ^ character as an anchor.

The resulting list will be directories only with full list as supplied by the 'ls -la' command


Arturo Galbiati
Esteemed Contributor

Re: Command to list/find ONLY all directories.

Hi,
ls -l|grep ^d

HTH,
Art
Muthukumar_5
Honored Contributor

Re: Command to list/find ONLY all directories.

Do you want to get only the directories in the pwd then,

# ls -l . | grep -E '^d'

That is it.

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Command to list/find ONLY all directories.

You can use find as well as,

# cd

# find . ! -name "." -type d -prune

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Command to list/find ONLY all directories.

Hi Gulam,

A small script will do,

for i in * ; do if [ -d $i ]; then echo $i ; fi ; done

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Command to list/find ONLY all directories.

for i in * ; do if [ -d $i ]; then echo $i ; fi ; done

will miss .

Try to use as,

for i in `ls` ; do if [ -d $i ]; then echo $i ; fi ; done

--
Muthu
Easy to suggest when don't know about the problem!