views:

81

answers:

5

i want to list only the directories in specified path(ls don't have such option). Also, can this be done with a single line command?

+3  A: 

Try this ls -d */ to list directories within the current directory

davgothic
Hey, that's pretty nifty, I always used to us `ls -al | grep '^d'` - this is much more succinct.
paxdiablo
This however gives e.g. `ls: */: No such file or directory` on `stderr` if there is no directory in the working directory. (Removed nonsense about `-d` before the other comments were posted).
Georg Fritzsche
No, the `-d` is not redundant. Without this parameter, `ls */` will show the contents of all directories in the current working directory.
joschi
That it does, I'm not sure if there is an easy way to overcome that. The -d is to prevent the directory contents being displayed.
davgothic
grep '^d' is really useful. Works like charm!
S'am
+3  A: 

Try this:

find . -maxdepth 1 -type d
Robin
+1  A: 

find specifiedpath -type d

If you don't want to recurse in subdirectories, you can do this instead:

find specifiedpath -type d -mindepth 1 -maxdepth 1

Note that "dot" directories (whose name start with .) will be listed too; but not the special directories . nor ... If you don't want "dot" directories, you can just grep them out:

find specifiedpath -type d -mindepth 1 -maxdepth 1 | grep -v '^\.'

DomQ
A: 

The answer will depend on your shell.

In zsh, for example, you can do the following:

echo *(/)

And all directories within the current working directory will be displayed.

See man zshexpn for more information.

An alternative approach would be to use find(1), which should work on most Unix flavours:

find . -maxdepth 1 -type d -print  

find(1) has many uses, so I'd definitely recommend man find.

Johnsyweb
A: 

You can use ls -d */ or tree -d

Another solution would be globbing but this depends on the shell you are using and if globbing for directories is supported.

For example ZSH:

zsh # ls *(/)
echox
With which flavour of Unix do you get `tree`?
Johnsyweb
all major linux distries for example? sorry, I don't get the question?
echox