# ---------------------------------------------------------------------- # Name: replacehtml # ---------------------------------------------------------------------- # Author: Dean Stringer (deeknow@pobox.com) # Desc: Performs a FIND search starting from the directory # specified by the 1st argument (after checking if this # DIR actually exists) and looks for all *.?htm? files # that contain the string specified by the 2nd argument # adding their names to an index file specified by the # constant '$dirList' # # Uses SED to do a search/replace on these files, based # on the 2nd and 3rd arguments. # # The script first checks to see if the 1st paramater # is "UNDO" in which case it will return the previoulsy # modified files to their original state by 'mv'ing # the backup copy over the changed one. # # It goes without saying (of course) that you will need # write privaledges to all directories and files that # match the search. # # # !!!! WARNING !!!! # # you should check the results of the output from this # script before running it again, otherwise you will # not be able to do a subsequent UNDO. # # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- # Set some constants # ---------------------------------------------------------------------- backupSuffix="old~" # suffix to use for backup of changed files dirList="dirlist" # file to contain DIR listing seperator="------------------------------------------------------------------------" # Get the 1st paramater startDir=$1 # ---------------------------------------------------------------------- # Dont let the user run at web root level # ---------------------------------------------------------------------- if [ "$startDir" = "/home/www/" -o "$startDir" = "/home/www" ] ; then echo "ERROR !!! Cannot perform at root level" exit fi # ---------------------------------------------------------------------- # Save the last directory listing if there is one # ---------------------------------------------------------------------- if [ -f $dirList ]; then cp $dirList $dirList.last fi # ---------------------------------------------------------------------- # Check there's at least three arguments # ---------------------------------------------------------------------- if [ $# -ge 3 ] ; then str2find=$2 str2replace=$3 # Make sure the start directory exists if [ -d $startDir ]; then echo "Searching $startDir" # Find iall matching files and pipe out to directory file # set maxdepth to 1 (ie only process current DIR) unless $4 is set to "CRAWL" depth=$4 if [ "$depth" = "CRAWL" ] ; then find $startDir -iregex ".*htm.?" -print0 | xargs -0 grep -l "$str2find" > $dirList else find $startDir -iregex ".*htm.?" -print0 -maxdepth 1 | xargs -0 grep -l "$str2find" > $dirList fi # Check no of files being replaced echo "Found `cat $dirList | wc -l` files that will be updated." # Save date/time and user name into history file echo -e "$seperator\n`whoami` ran DIRLIST on `date`" >> $dirList.history echo "Replacing: $str2find" >> $dirList.history echo "With: $str2replace" >> $dirList.history cat $dirList >> $dirList.history echo "Total = `cat $dirList | wc -l` files" >> $dirList.history # Do a search/replace on each file in the index file for file in `cat $dirList` ; do cp $file $file.$backupSuffix sed "s/$str2find/$str2replace/g" $file.$backupSuffix > $file done else errorMessage="Start Directory does not exist" fi # ---------------------------------------------------------------------- # Not enough arguments, check for other run-options # ---------------------------------------------------------------------- else # Check to see if user wants to UNDO if [ "$startDir" = "UNDO" ] ; then # we ARE undoing, so check an old directory file exists if [ -s $dirList ]; then # It does, so restore each file in the index file echo -e "$seperator\n`whoami` ran a DIRLIST UNDO on `date`" >> $dirList.history echo "Performing an UNDO" for file in `cat $dirList` ; do mv $file.$backupSuffix $file done mv $dirList $dirList.undo else # No directory file exists so can't undo errorMessage="Cannot UNDO, no '$dirList' found" fi else errorMessage="Wrong no. of Arguments" fi fi # ---------------------------------------------------------------------- # If there's an error message set, display it and the correct syntax # ---------------------------------------------------------------------- if [ "$errorMessage" != "" ] ; then echo ''; echo "ERROR !!! $errorMessage" echo ''; echo "Syntax is : replacehtml " echo " or replacehtml UNDO" fi