Nightly file replacement bash script
2010-1-04 20:41
This time small script witch replaces one word in all files in custom directory. It's sometimes usefull to have sutch script. Just in case that some file must have unique name each night. And remain linked in related files correct.
Script content. This time i replace squiremail compose.php file with random generated name each night.
#!/bin/bash
#Start config
#Current compose.php name
FILE_CONFIG='/path/to/config.cnf'
#Squiremail for example directory
SQUIREMAIL_DIRECTORY='/var/www/squirrelmail'
#End config
echo 'Starting file changed...'
OLD_FILE=''
NEW_FILE=''
#Reading current file
exec 10<$FILE_CONFIG
let count=0
while read LINE <&10; do
OLD_FILE=$LINE
done
echo "Old file name - $OLD_FILE"
#Generating new file name
PASS=""
PASSLEN=8
array1=( q w e r t y u i o p a s d f g h j k l z x c v b n m
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
1 2 3 4 5 6 7 8 9 0 )
MODNUM=${#array1[*]}
count=0
while [ ${count:=0} -lt $PASSLEN ]
do
number=$(($RANDOM%$MODNUM))
PASS="$PASS""${array1[$number]}"
((count++))
done
NEW_FILE="$PASS.php"
echo "New file name - $NEW_FILE"
echo "Replacing links in plugin directory"
cd "$SQUIREMAIL_DIRECTORY/plugins"
find ./ -type f -exec sed -i "s/$OLD_FILE/$NEW_FILE/" {} \;
echo "Replacing links in src folder"
cd "$SQUIREMAIL_DIRECTORY/src"
find ./ -type f -exec sed -i "s/$OLD_FILE/$NEW_FILE/" {} \;
echo "Replacing links in functions folder"
cd "$SQUIREMAIL_DIRECTORY/functions"
find ./ -type f -exec sed -i "s/$OLD_FILE/$NEW_FILE/" {} \;
echo "Replacing files"
OLD_FILE_NAME="$SQUIREMAIL_DIRECTORY/src/$OLD_FILE"
NEW_FILE_NAME="$SQUIREMAIL_DIRECTORY/src/$NEW_FILE"
echo "Old file path - $OLD_FILE_NAME"
echo "New file path - $NEW_FILE_NAME"
echo "Renaming file"
mv $OLD_FILE_NAME $NEW_FILE_NAME
echo "Updating config"
echo "$NEW_FILE" > $FILE_CONFIG
echo "Finished..."
Back »
Comments: 0
Leave a reply »