I want my shell script to visit all subdirectories in a main directory. Do something in directories, sent output to a spool file and move on to next directory.
Consider
Main Dir = /tmp
Sub Dir = A B C D (Four sub directories)
Bash – Traverse all subdirectories in and do something in Unix shell script
bashshell-script
Best Answer
Use a
for
loop:It searches only the subdirectories of the directory
/path/to/dir
. Note that the simple example above will fail if the directory names contain whitespace or special characters. A safer approach is:Or in plain
bash
:(note that contrary to
find
that one also considers symlinks to directories and excludes hidden ones)