How could I list sub-directories with ls, with '-d' only the current directory is shown. I want something like find . -type d -maxdepth 1
would give me.
Asked
Active
Viewed 6.7k times
25
![](../../users/profiles/639054.webp)
Bernhard
- 7,557
- 2
- 37
- 39
-
1Perhaps you wanted to write: `find . -type d -maxdepth 1` (with no `=` for `-maxdepth`. – Chen Levy Mar 02 '11 at 13:43
3 Answers
34
This should help:
ls -d */
*/
will only match directories under the current dir. The output directory names will probably contain the trailing '/' though.
![](../../users/profiles/262683.webp)
Costi Ciudatu
- 33,403
- 5
- 52
- 89
-
4To be pedantic about this, you should do `ls -d .*/ */` or `shopt -s dotglob; ls -d .*/`, otherwise directories starting with dot (`.`) will not be listed. – Chen Levy Mar 02 '11 at 13:41
-
-
@ZhaoGang See https://stackoverflow.com/questions/14352290/listing-only-directories-using-ls-in-bash-an-examination – Costi Ciudatu Jul 23 '19 at 21:03
5
You can combine with grep:
ls -l | grep '^d'
To get just the filenames:
ls -l | grep '^d' | awk '{ print $9 }'
You can make this into a handy alias:
alias ldir="ls -l | grep '^d'"
![](../../users/profiles/7412.webp)
dogbane
- 242,394
- 72
- 372
- 395
-
2If you are already using `awk`, `grep` is superfluous: `ls -l | awk '/^d/ { print $9 }'` – Chen Levy Mar 02 '11 at 13:48