list only directories (501 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 6 (501 Views)
Accepted Solution

list only directories

hello,

 

i want to list  in a directory all directories, but i have to exclude files and other directories

 

old way was "ls" ( but this command include files)

 

ls 
Output
dir1
dir2

 

better way is "find"

find . \( -name . -o -prune \) -type d
Output
.
./dir1
./dir2

 

but does a better way exist to exclude "./"  and "." . it should the same

output like "ls" (without files)


find . \( -name . -o -prune \) -type d | sed "s|./||g" | sort |grep -v "\."


regards

Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 6 (473 Views)

Re: list only directories

How about:

ls -F | grep '/$'

 

MK
Honored Contributor
Patrick Wallek
Posts: 13,787
Registered: ‎06-21-2000
Message 3 of 6 (470 Views)

Re: list only directories

How about:

 

# find . -type d ! \( -name . -o -name .. \)
./.ssh
./.sw
./.sw/sessions
./.sw/targets
./.sw/software
./bin
./.swa
./.swa/cache
./.swa/report
./DR
./progs
./progs/exe
./progs/memoryAllocation
./progs/memoryAllocation/progs
./progs/tapeinfo
./.elm
./Mail

 

Advisor
karthikbalu
Posts: 20
Registered: ‎10-16-2012
Message 4 of 6 (451 Views)

Re: list only directories

You can just use this below to list directories.

 

ls -l | grep ^d

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,290
Registered: ‎03-06-2006
Message 5 of 6 (448 Views)

Re: list only directories

>find . \( -name . -o -prune \) -type d
>but does a better way exist to exclude "./"  and "."

 

Yes:

$ find * -name . -o -prune  -type d -print

"*" gets rid if "./" and using -print (properly) gets rid of ".".  But "*" will fail if too many files.

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 6 of 6 (444 Views)

Re: list only directories

 

this is the best solution :

find * -name . -o -prune  -type d -print

 

my comments to the another solutions :

 

@ Dennis Handly

find * -name . -o -prune  -type d -print

Perfect

@ "*" gets rid if "./" and using -print (properly) gets rid of ".".  
@ But "*" will fail if too many files.

this applikation exist approximately 250 directories , so it works perfect.
i also tested the "find" command with approximately 60.000 files and a lot of
subdirectories. it works also without problems.

@ Matti_Kurkela
ls -F | grep '/$'

i need another cmd to replace ending "/" like :
ls -F | grep '/$' | sed "s|/$||g"

@ karthikbalu
ls -l | grep ^d

i need another cmd  to get the directory like :
ls -l | grep ^d | awk '{ print $NF}'

@Patrick
find . -type d ! \( -name . -o -name .. \)

It includes also subdirectories like ./dir1/subdir2

better
find . -type d ! \( -name . -o -name .. \) -prune
but i also need another cmd to replace leading "./" like :
find . -type d ! \( -name . -o -name .. \) -prune |sed "s|^./||g"

regards

 

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.