Pete's Linux Advent Calendar 2007
The 21st day
Recursive Scripting
If you want to recursively process directories you can
write a function that calls itself:
#! /bin/bash
getdirs() {
dir="$1"
for file in "$dir"/* "$dir"/.*; do
case "$file" in
*/..|*/.)
continue
;;
*)
if [ -d "$file" -a ! -h "$file" ]; then
echo "$file"
getdirs "$file"
fi
esac
done
}
if [ "$1" ]; then
if [ -d "$1" -a ! -h "$1" ]; then
echo "$1"
getdirs "$1"
fi
else
echo "usage: $0 <dir>" >&2
exit 2
fi
This only prints all the directories it finds and does not follow
symbolic links. Note that this only used builtins.