Sign In

Bulk Find and Replace Text Files Using Bash for Dataset Prep

1

Jun 18, 2025

(Updated: 5 months ago)

data prep
 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 .txt files 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

  1. Open your terminal.

  2. Create a new script file:

nano find_replace.sh
  1. paste 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'."
  1. Save and exit:

    • Press CTRL+O, then Enter to save

    • Press CTRL+X to exit

Step 2: Make It Executable

Before you can run it, give the script execute permissions:

chmod +x find_replace.sh

Step 3: Run the Script

Run it with:

./find_replace.sh

It will prompt you for:

  1. The target folder (e.g. ./myfiles)

  2. The string to find (e.g. score_9)

  3. The string to replace it with (e.g. score 9)

It will then scan and update all .txt files in the folder.

1