BASH
Find and Replace Text Recursively in Multiple Files
Learn to use a bash script to efficiently find and replace specific text strings across multiple files within a directory and its subdirectories.
#!/bin/bash
SEARCH_TEXT="old_domain.com"
REPLACE_TEXT="new_domain.com"
DIRECTORY="/var/www/mywebsite"
FILE_EXTENSION="php|js|html|css|txt|conf"
find "$DIRECTORY" -type f -regextype posix-egrep -iregex ".*\.($FILE_EXTENSION)$" -print0 | xargs -0 sed -i "s/$SEARCH_TEXT/$REPLACE_TEXT/g"
How it works: This script performs a find and replace operation across multiple files in a specified directory and its subdirectories. It uses `find` to locate files matching given extensions and `sed` to replace all occurrences of a specified text string with a new one, providing a powerful way to update configurations or codebases.