Bulk Find and Replace Text Files Using Bash for Dataset Prep

Working with lots of .txt files and need to find and replace a specific string across them all? Doing it manually is tedious — but with a simple shell script, you can automate the process and save tons of time.
In this guide, you’ll learn how to:
Create a shell script to find and replace text
Prompt the user for inputs interactively
Process all
.txtfiles in a selected directory
🧰 What the Script Does
This Bash script will:
✅ Ask you for the target folder
✅ Prompt you for the string to find and the replacement string
✅ Automatically update all .txt files in that folder
✅ Use sed to perform in-place, case-sensitive replacement
📄 Step 1: Create the Script
Open your terminal.
Create a new script file:
nano find_replace.shpaste the following code
#!/bin/bash
# Prompt user for the target directory
read -p "Enter the target directory (e.g., ./ or /path/to/dir): " TARGET_DIR
# Verify directory exists
if [ ! -d "$TARGET_DIR" ]; then
echo "❌ Error: Directory '$TARGET_DIR' does not exist."
exit 1
fi
# Prompt user for the string to find
read -p "Enter the string to find (case-sensitive): " FIND_STRING
# Prompt user for the replacement string
read -p "Enter the string to replace it with: " REPLACE_STRING
# Escape characters for safe sed usage
ESCAPED_FIND=$(printf '%s\n' "$FIND_STRING" | sed -e 's/[\/&]/\\&/g')
ESCAPED_REPLACE=$(printf '%s\n' "$REPLACE_STRING" | sed -e 's/[\/&]/\\&/g')
# Process each .txt file
find "$TARGET_DIR" -type f -name "*.txt" | while read -r file; do
echo "🔄 Replacing in: $file"
sed -i "s/${ESCAPED_FIND}/${ESCAPED_REPLACE}/g" "$file"
done
echo "✅ Replacement complete in all .txt files in '$TARGET_DIR'."
Save and exit:
Press
CTRL+O, thenEnterto savePress
CTRL+Xto exit
Step 2: Make It Executable
Before you can run it, give the script execute permissions:
chmod +x find_replace.shStep 3: Run the Script
Run it with:
./find_replace.shIt will prompt you for:
The target folder (e.g.
./myfiles)The string to find (e.g.
score_9)The string to replace it with (e.g.
score 9)
It will then scan and update all .txt files in the folder.