Sometimes it is useful to be able to replace a certain piece of text in multiple files, even across multiple folders. Opening each file and using find/replace is tedious. Fortunately, there is a much easier way using the sed and grep utilities. For Windows users, please install Cygwin to get these programs on your system. Linux and Mac OS X users will already have these programs pre-installed.
Launch a terminal window, and cd to the root directory where all of these files and folders exist. Once in the correct directory, type the following:
sed -i ‘s/textToFind/replacementText/g’ `grep -ril ‘textToFind’ *`
This command will recursively replace any instances of textToFind with replacementText in all documents in the current folder and sub folders. This command works by first searching for the names of all the files containing textToFind using grep. The output of this command is piped inline to the input for the sed command, which does the actual replacement. This inline substitution is accomplished with the `` quotes (probably to the left of the 1 key on your keyboard). This is a special syntax in the BASH Shell which causes the contents of whatever is inside the `` to be executed and the results to be put in its place.
To do a simpler find and replace on only 1 file, use the following syntax:
sed -i ‘s/textToFind/replacementText/g’ /path/to/file.txt




