I have a script that is giving me some problems, I know that I have to re-do it recursively but am having some trouble. The script should rename all files and folders in a given folder by removing special characters like $-,)(! and replacing " " with "_"
Here is the piece I need to re-do:
# Rename directories
find "$ORIG_DIR" -name "*" -type d| while read dname
do
NEW_DIR=`echo "$dname" |sed -E 's/^[ \t]*//g; s/[ \t]*$//g; s/\./_/g; s/\,/_/g; s/ /_/g'`
# Rename files
find "$ORIG_DIR" -name "*" -type f | while read fname
do
NEW_FNAME=`echo "$fname" |sed -E 's/^[ \t]*//g; s/[ \t]*$//g; s/\./_/g; s/(.*)_/\1\./; s/\,/_/g; s/ /_/g'`
if [ -e $NEW_FNAME ]
then
echo "$NEW_FNAME already exists. Not replacing $fname"
else
echo "Replacing $fname with $NEW_FNAME"
mv "$fname" $NEW_FNAME
fi
done
if [ -e $NEW_DIR ]
then
echo "$NEW_DIR already exists. Not replacing $dname"
else
echo "Replacing $dname with $NEW_DIR"
mv "$dname" $NEW_DIR
fi
done
END
find
commands will always travers the whole directory tree (replace the functionality inside by echo statements to see what's going on).