Revision: 64596
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 27, 2013 12:50 by tedw
Initial Code
#!/bin/sh
# Make sure file is saved with Unix encoding (dos2unix)
#
# This script will rename a file from the first parameter to the second parameter
# and do a sed replace of all references of the first parameter with the second parameter
#
# For example:
# RenameFileAndUpdateReferences.sh filename-dc80c59.css filename.css
if [ $# -lt 2 ] ; then
echo
echo Wrong number of parameters.
echo Try again using the following format:
echo RenameFileAndUpdateReferences.sh findstring replacestring
echo
echo example:
echo "RenameFileAndUpdateReferences.sh \"filenameABC.ext\" \"filenameDEF.ext\""
echo
exit 1
fi
IFS=$'\n'
if [[ "$3" == 'go' ]]; then
TIMESTAMP=$(date +%s)
BACKUP_EXT="rfaur_saved"
BACKUP_SUFFIX=".$TIMESTAMP.$BACKUP_EXT"
COUNTER=0
echo "Replacing files..."
for FILENAME in $(find . -type f -exec grep -l "$1" {} \;); do
if [[ "$FILENAME" != *.$BACKUP_EXT* ]]; then # don't text in backup files
echo " working on file \"$FILENAME\""
BACKUP_FILENAME="$FILENAME$BACKUP_SUFFIX"
###echo "$BACKUP_FILENAME"
cp "$FILENAME" "$BACKUP_FILENAME"
sed "s#$1#$2#g" "$BACKUP_FILENAME" > "$FILENAME"
### rm "$BACKUP_FILENAME" # uncomment this line to remove backup file
COUNTER=$[$COUNTER +1]
fi
done
echo "String replaced in $COUNTER file(s) (backup timestamp is $TIMESTAMP)"
COUNTER=0
echo "Renaming files..."
for FILENAME in $(find . -type f -name "$1"); do
NEW_FILENAME="$(dirname ${FILENAME})/$2" # e.g. some/path/to/the/newfilename
echo " renaming file \"$FILENAME\" to \"$NEW_FILENAME\""
if [ -e $NEW_FILENAME ]; then
BACKUP_FILENAME="$NEW_FILENAME$BACKUP_SUFFIX"
echo " \"$NEW_FILENAME\" already exists, backing up to \"$BACKUP_FILENAME\""
cp "$NEW_FILENAME" "$BACKUP_FILENAME"
fi
mv $FILENAME $NEW_FILENAME
COUNTER=$[$COUNTER +1]
done
echo "$COUNTER file(s) renamed"
echo "-- DONE --"
else
echo "All instances of \"$1\" will be changed to \"$2\" in the following files:"
find . -type f -exec grep -n "$1" {} /dev/null \;
echo
echo "The following files will be renamed:"
find . -type f -name "$1"
echo
echo "Preview done, add 'go' as the 3rd argument to execute changes"
fi
Initial URL
Initial Description
This script will rename a file from the first parameter to the second parameter and do a sed replace of all references of the first parameter with the second parameter For example: RenameFileAndUpdateReferences.sh filename-dc80c59.css filename.css
Initial Title
RenameFileAndUpdateReferences.sh
Initial Tags
Initial Language
Bash