How to list folders using bash commands?
Solution 1:
You can use:
ls -d -- */
Since all directories end in /
, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.
Solution 2:
Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest
find . -mindepth 1 -maxdepth 1 -type d
(This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)
Solution 3:
find . -maxdepth 1 -type d
Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs
Solution 4:
Daniel’s answer is correct. Here are some useful additions, though.
To avoid listing hidden folders (like .git
), try this:
find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \)
And to replace the dreaded dot slash at the beginning of find
output in some environments, use this:
find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \) | sed 's|^\./||g'
Solution 5:
You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.
if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".
I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of
for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done