if (($# < 2 || $# > 4))
then
cat <<EOF
Usage:
`basename "$0"` FROM-EXT TO-EXT [DIRECTORY] [MAXDEPTH]
Renames all files in DIRECTORY (default: ".") from extension FROM-EXT to
TO-EXT. Pass the empty string as FROM-EXT to append TO-EXT to all files in
DIRECTORY. Descend at most MAXDEPTH levels into sub-directories. The default of
1 renames in DIRECTORY only. For an unlimited depth use e.g. 999.
Examples:
$ dosrename .jpeg .jpg ~/images
$ dosrename 'b' 'c' # rename 'aab' to 'aac', 'bb' to 'bc' ...
$ dosrename 'b' '' # rename 'aab' to 'aa', 'bb' to 'b'
$ dosrename '' .tt # add ".tt" to all files in "."
$ dosrename '' '' # no effect
EOF
else
optfrom="$1" optto="$2" optdir="$3" optdepth=$4
for f in `find ${optdir} -maxdepth ${optdepth:-1} -name "*$optfrom" -type f`; do
to="`dirname $f`/`basename $f $optfrom`$optto"
mv -v $f $to
done
fi