I am writing a script in bash on Linux and need to go through all subdirectory names in a given directory. How can I loop through these directories (and skip regular files)?
For example:
the given directory is /tmp/
it has the following subdirectories: /tmp/A, /tmp/B, /tmp/C
I want to retrieve A, B, C.
A short explanation:
findfinds files (quite obviously).is the current directory, which after thecdis/tmp(IMHO this is more flexible than having/tmpdirectly in thefindcommand. You have only one place, thecd, to change, if you want more actions to take place in this folder)-maxdepth 1and-mindepth 1make sure thatfindonly looks in the current directory and doesn’t include.itself in the result-type dlooks only for directories-printf '%f\nprints only the found folder’s name (plus a newline) for each hit.Et voilà!