Automate text replacement in multiple files using a Bash script in Linux.
It takes a lot of effort and is capable of error to manually update out-of-date information across several files. Whether it’s modifying configuration values, renaming variables, or updating file paths, Bash provides powerful tools like grep, sed, and find to automate this process efficiently. In this guide, we’ll walk you through automating bulk text replacement in multiple files using a simple Bash script.
Read More: How to Transfer Files Between Windows and Linux Using WinSCP?
Automating text replacement is useful for:
Instead of manually editing each file, a Bash script can accomplish this task in seconds.
Imagine you have multiple files referring to outdated log file paths:
Old Path: ~/data/logs/app.log
New Path: ~/data/log/app.log
Instead of manually searching and replacing each instance, let’s automate it using Bash.
Before modifying real files, it’s best to test on sample files. Follow these steps:
Create a directory and navigate to it:
mkdir -p ~/replace_test && cd ~/replace_test Create sample files containing the old path:
echo 'Log file: ~/data/logs/app.log' > script1.sh
echo 'ERROR_LOG="~/data/logs/app.log"' > config.env
echo 'echo "Processing ~/data/logs/app.log"' > process.sh Verify occurrences of the old path:
grep "~/data/logs/app.log" * Now, create a script named replace_text.sh with the following content:
#!/usr/bin/env bash
if [[ $# -ne 3 ]]; then
echo "Usage: $0 <old_path> <new_path> <directory>"
exit 1
fi
OLD_PATH=$(printf '%s\n' "$1" | sed 's/[\/&]/\\&/g')
NEW_PATH=$(printf '%s\n' "$2" | sed 's/[\/&]/\\&/g')
SEARCH_DIR=$3
echo "Replacing occurrences of: $1 -> $2 in $SEARCH_DIR"
# Find and replace text safely
find "$SEARCH_DIR" -type f -exec sed -i "s/$OLD_PATH/$NEW_PATH/g" {} +
echo "Replacement completed." Grant execution permission:
chmod +x replace_text.sh Run the script to replace all occurrences of ~/data/logs/app.log with ~/data/log/app.log:
./replace_text.sh "~/data/logs/app.log" "~/data/log/app.log" ~/replace_test Sample Output:
Replacing occurrences of: ~/data/logs/app.log -> ~/data/log/app.log in /home/user/replace_test
Replacement completed. Check for the new path:
grep "~/data/log/app.log" * Ensure no old paths remain:
grep "~/data/logs/app.log" * If there’s no output, the replacement was successful.
/, &, and other symbols correctly.find -exec sed Instead of grep | xargs sed: Safer for handling filenames with spaces.grep: Check affected files before making changes using:grep -rl "~/data/logs/app.log" ~/replace_test/Automation is key in modern IT workflows, and with the right tools, businesses can streamline operations efficiently. This is where Techwrix comes in, offering B2B media services to help companies stay ahead with the latest automation techniques, cloud solutions, and enterprise IT strategies.
Read More : Building a Robust Digital Infrastructure: The Role of SASE in Today’s Networking Landscape
In this tutorial, we learned how to replace text in multiple files using a simple Bash script. Automating bulk text replacement helps prevent errors and saves time. Before running it on critical files, always test with backups and verify results.
For more insights into IT automation and business solutions, trust Techwrix, your go-to partner for technology innovation and growth!
Cloud cost issues rarely start as major failures. They build quietly as operations teams scale…
What happens when attackers can scan your environment, generate exploits, and launch attacks faster than…
In the evolving landscape of cloud computing, selecting the right platform is crucial for developers,…
Cloud computing is no longer just about hosting applications or scaling infrastructure. In 2026, it…
What are JPG and WebP Image Formats? Joint Photographic Experts Group introduced JPG format in…
Enterprise technology strategies in 2026 evolve from isolated initiatives into operationally critical systems that influence…