Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I list folders in shell? 1

Status
Not open for further replies.

bobetko

Programmer
Jan 14, 2003
155
US
how can I list folders in shell?

I tried:

ls -d

and it's producing no result.
 
thanks for fast reply, but it doesn't list folders only.
It lists everything, folders and files.

Code:
#I get same result with 

ls

#and

ls -d *
 
When you do [tt]ls -l[/tt], lines with a directory begin with a [tt]d[/tt], so you can grep those out.
Code:
ls -l |grep ^d


Or, if you want more control over how you list stuff:
Code:
find . -type d -maxdepth 1 -print0 |xargs -0 ls -d
and pass any options you want to [tt]ls[/tt]. If you plan to use that often, you'll probably want to make a shell alias or function for it.

That line says, "Find everything in this directory -- but no deeper ([tt]-maxdepth 1[/tt]) -- that's a directory ([tt]-type d[/tt]), and print it ([tt]-print0[/tt]). Then, run [tt]ls -d[/tt] and give it all of those files as arguments. The [tt]-print0[/tt] and [tt]-0[/tt] ensure that directory names with spaces are handled properly.
 
If you want to use find and don't have maxdepth try

find . -type d ! -path './*/*'

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Also, if you use my above command and get warnings, that's because I should have put [tt]-maxdepth 1[/tt] ahead of [tt]-type d[/tt]. Just switch those two around and you won't get the warnings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top