Skip to content

Instantly share code, notes, and snippets.

@liuggio
Created May 24, 2012 14:57
Show Gist options
  • Save liuggio/2782061 to your computer and use it in GitHub Desktop.
Save liuggio/2782061 to your computer and use it in GitHub Desktop.
Bulk recursive rename dirs and files
#!/bin/bash
# $1 is the var
# $2 is replace from
# $3 is replace to
EXPECTED_ARGS=3
E_BADARGS=65
HELP=$(cat <<EOF
Usage with three arguments:
`basename $0` /var/www/path/ typo type
EOF
)
if [ $# -ne $EXPECTED_ARGS ]
then
echo $HELP
exit $E_BADARGS
fi
echo "Renaming Directory"
find $1 -type d -name "*$2*" | while read DIR
do
newname=`echo $DIR | sed s/$2/$3/`
echo $newname
mv "$DIR" "$newname"
done
echo "Renaming Files"
find $1 -type f -name "*$2*" | while read FILE
do
newname=`echo $FILE | sed s/$2/$3/`
echo $newname
mv "$FILE" "$newname"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment