LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I list directories only in linux? (https://www.linuxquestions.org/questions/programming-9/how-can-i-list-directories-only-in-linux-375219/)

abefroman 10-20-2005 05:34 PM

How can I list directories only in linux?
 
How can I list directories only in linux?

What would the command be?

itsme86 10-20-2005 05:54 PM

'ls'? You need to be more specific with your question.

trevelluk 10-20-2005 05:54 PM

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.

itsme86 10-20-2005 06:26 PM

You could try 'tree -d -L 1'

homey 10-20-2005 07:32 PM

Here's another one
Code:

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

rsheridan6 10-20-2005 10:30 PM

The shortest and simplest way
 
ls -d */

I just found this out after nearly 3 years of using Linux.

homey 10-21-2005 12:55 AM

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


moranar 10-27-2005 02:01 PM

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...

paulsm4 10-27-2005 02:06 PM

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}'

eddiebaby1023 10-29-2005 03:33 PM

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.

perfect_circle 10-29-2005 04:41 PM

Code:

ls -l |grep ^d

lydgate 10-29-2005 06:37 PM

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

amjith 01-27-2009 07:59 PM

ls -d */
 
Check this out:
http://www.amjith.blogspot.com/2005/...-in-linux.html

bajidude 01-31-2012 07:53 AM

'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 !

Andy_Crowd 01-04-2014 01:21 PM

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;


All times are GMT -5. The time now is 07:12 PM.