#!/bin/bash
# Define the search and replace strings
search=”old_text”
replace=”new_text”
# Define the directory to search (include trailing slash)
dir=”/path/to/search/in/”
# Find all files in the directory and its subdirectories
files=$(find $dir -type f)
# Iterate through each file
for file in $files; do
# Check if the file contains the search string
if grep -q “$search” “$file”; then
# Replace the text in the file
sed -i “s/$search/$replace/g” “$file”
# Print the file name to confirm the change
echo “Changed $file”
fi
done
This script uses the find
command to search for all files in the specified directory and its subdirectories. The grep
command is used to check if the file contains the search string. If it does, the sed
command is used to replace the text in the file. The -i
flag is used to edit the files in place, without creating backups. Finally, the script prints the file name to confirm that the text was changed.
You can customize the script by changing the search
and replace
variables to the text you want to find and replace, and the dir
variable to the directory you want to search in.
Note: This script uses the sed
command with the -i
option which will change the file in place, this could be dangerous if you’re not sure what you’re doing, so please make sure to backup your files or test the script with a small set of files first.