Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I loop through only directories in bash?

I have a folder with some directories and some files (some are hidden, beginning with dot).
for d in *; do
echo $d
done


will loop through all files and directories, but I want to loop only through directories. How do I do that?
by

2 Answers

akshay1995
You can specify a slash at the end to match only directories:

for d in */ ; do
echo "$d"
done
pankajshivnani123
You can use pure bash for that, but it's better to use find:

find . -maxdepth 1 -type d -exec echo {} \;

(find additionally will include hidden directories)

Login / Signup to Answer the Question.