This article provides a guide for using recursive find and replace (Linux command line method) to efficiently replace data.
Reference this guide for help with mass find-and-replace file manipulation in Linux servers.
Find and replace text strings recursively through all files and sub-folders of a particular directory using sed command-line tool.
Using Recursive Find and Replace (Linux Command Line Method)
Follow the steps below to utilize the recursive 'Find and Replace' tool.
- Login to SSH terminal.
- Run the following command from SSH terminal:
egrep -lRZ 'old-text-string' . | xargs -0 -l sed -i -e 's/old-text-string/new-text-string/g'
How to Modify Command
This command will recursively replace every instance of "old-text-string" with your "new-text-string". Modify the command to best meet your needs.
For example, if we wanted to change every instance of "red" to "blue", we could run the following command:
egrep -lRZ 'red' . | xargs -0 -l sed -i -e 's/red/blue/g'
The command will find all instances of "red" and replace with "blue". Simply navigate to the directory that you want to recursively change and run the command.
You now know how to use recursive Find and Replace (Linux command line method).