LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2005, 05:34 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
How can I list directories only in linux?


How can I list directories only in linux?

What would the command be?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-20-2005, 05:54 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
'ls'? You need to be more specific with your question.
 
Old 10-20-2005, 05:54 PM   #3
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Here's one way:

ls -p | grep "/"

The -p option to ls appends a / character to all directories (this character can never appear in a file name). Grep then filters the resulting information, to only show entries that contain a / character.

The only slight downside of this is that the list you get will have the slash on the end of each line. If this is a problem, you should be able to get rid of it using cut.
 
1 members found this post helpful.
Old 10-20-2005, 06:26 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You could try 'tree -d -L 1'
 
Old 10-20-2005, 07:32 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's another one
Code:
find . -type d -exec ls -d {} \;
 
1 members found this post helpful.
Old 10-20-2005, 10:30 PM   #6
rsheridan6
Member
 
Registered: Mar 2003
Location: Kansas City
Distribution: Debian unstable
Posts: 57

Rep: Reputation: 22
The shortest and simplest way

ls -d */

I just found this out after nearly 3 years of using Linux.
 
7 members found this post helpful.
Old 10-21-2005, 12:55 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
If you want to include the hidden directories, you may want to try one of these....
Code:
find . -type d -exec ls -d {} \;
or
ls -d .*"/" *"/" 
or 
find . -type d
 
1 members found this post helpful.
Old 10-27-2005, 02:01 PM   #8
moranar
LQ Newbie
 
Registered: Jan 2005
Distribution: Mandrake Linux
Posts: 1

Rep: Reputation: 0
What about listing only directories recursively? Can it be done with just ls? ls -dR doesn't seem to work as I'd like it. It seems that "list recursively" and "list dirs but not contents" clash...
 
Old 10-27-2005, 02:06 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try "du":
Quote:
du
0 ./.mc
20 ./.qt
0 ./bin
0 ./mnt/winxp-main/C
0 ./mnt/winxp-main
0 ./mnt
0 ./.ddd/themes
0 ./.ddd/sessions
36 ./.ddd
0 ./.dia/objects
0 ./.dia/shapes
...

If the left column (directory size) annoys you, you can easily eliminate it with: du|awk '{print $2}'
 
Old 10-29-2005, 03:33 PM   #10
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by moranar
What about listing only directories recursively? Can it be done with just ls? ls -dR doesn't seem to work as I'd like it. It seems that "list recursively" and "list dirs but not contents" clash...
find() and ls are the way to go for this as homey showed in his first post.
 
Old 10-29-2005, 04:41 PM   #11
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Code:
ls -l |grep ^d
 
Old 10-29-2005, 06:37 PM   #12
lydgate
Member
 
Registered: Jun 2005
Location: Fullerton, CA
Distribution: Arch 0.7.1 (laptop) / Slack 10.2 (desktop)
Posts: 41

Rep: Reputation: 15
Quote:
Originally posted by moranar
What about listing only directories recursively? Can it be done with just ls? ls -dR doesn't seem to work as I'd like it. It seems that "list recursively" and "list dirs but not contents" clash...
tree -d
 
Old 01-27-2009, 07:59 PM   #13
amjith
LQ Newbie
 
Registered: Aug 2004
Posts: 5

Rep: Reputation: 0
ls -d */

Check this out:
http://www.amjith.blogspot.com/2005/...-in-linux.html
 
Old 01-31-2012, 07:53 AM   #14
bajidude
LQ Newbie
 
Registered: Jan 2012
Distribution: Debian & Ubuntu
Posts: 1

Rep: Reputation: Disabled
'ls -ld */' rocks !

Code:
ls -ld */

or

ls -ld */ .*/
(from Amjith) is the only single command that works correctly with directories & links to other dirs, including the hidden ones

Code:
ls -p | grep "/"
fails when you have links to other files. I am adding this to my bash
Code:
lsd='ls -ld */ .*/'
thanks !
 
Old 01-04-2014, 01:21 PM   #15
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Rep: Reputation: Disabled
Here is inline bash scripts

listing only directories:
Code:
for f in *;do if [[ -d $f  ]]; then echo $f;fi; done;
listing only files:
Code:
for f in *;do if [[ -d $f  ]]; then echo "" > /dev/null ;else echo $f ;fi; done;
listing all:
Code:
for f in *;do if [[ -d $f  ]]; then echo $f; done;

Last edited by Andy_Crowd; 01-06-2014 at 09:33 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can ls recursively list only directories? Vosper Linux - General 3 07-16-2005 03:57 AM
list sub-directories only CowboyJ Linux - Newbie 1 12-04-2003 11:42 PM
Asking LS for a list of directories only gary knott Linux - Newbie 1 11-12-2003 05:49 PM
list of directories greg108 Linux - Newbie 5 08-08-2003 03:11 AM
Does anyone know how to list directories vertically ? lostboy Linux - General 2 04-07-2003 08:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration