BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

list only directories

SOLVED
Go to Solution
Valued Contributor

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

5 REPLIES
Honored Contributor

Re: list only directories

How about:

ls -F | grep '/$'

 

MK
Honored Contributor

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

Re: list only directories

You can just use this below to list directories.

 

ls -l | grep ^d

 

 

Acclaimed Contributor

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.

Highlighted
Valued Contributor

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