WARNING! MAKE BACKUP of ALL FILES ON THE HARDDISK!

that being said, this search replace script works with the tool sed which required to escape all special characters.

This tool will help with that: https://dwaves.de/tools/escape/

per default it will search the current folder the user is in.

# make backup of current folder
mkdir /root/backups
tar fcvz /root/backups/backup_of_current_folder.tar.gz .

# create the script
vim /scripts/global-search-replace.sh
# paste the content

#!/bin/bash
echo "=== global-search-replace.sh v1.1 ==="
echo "WARNING! MAKE BACKUP OF ALL FILES BELOW . BEFORE PROCEEDING!"
echo "INPUT ONLY SED ESCAPED STRINGS! (https://dwaves.org/tools/escape/)"

echo "REPLACE-ALL:" $2
echo "WITH:" $3
echo "IN ALL FILES BELOW THIS PATH:" $1
echo "LAST-WARNING: ENTER = PROCEED, CTRL+C = ABORT"
read input

THIS=$2
# shall become
THAT=$3

find $1 -type f -exec sed --debug -i "s/$THIS/$THAT/g" {} \;

make runnable:

chmod +x /scripts/*.sh

usage:

/scripts/global-search-replace.sh "./path/to/files/" 'search-for-this' 'replace-with-that'

# for example:
/scripts/global-search-replace.sh "/scripts/" 'user1' 'user2'

 

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin