Main Tutorials

Linux Search and Replace example

I was recently in a situation of having to change some text that appeared in a bunch of config files that were inside nested sub-directories. Not having time to dust off 10-year old perl skills, I was looking for some shell commands that would work. Surprisingly, there are many examples on the web that do *not* work, so I ended up with these variations:

Feel free to test these out first, and study how they work. In both cases, we are supplying a list of the files we want, to the linux “sed” command, using xargs to build the sed command (see this article for an interesting summary of xargs).

1. Search and Replace All Files

If you need to replace a certain set of text in a set of files, but you do not know which files might contain the text you are looking for, this might be useful:


grep -lr -e 'old text' * | xargs sed -i 's/old text/new text/g'

Using grep here, we are recursively (the “-r” option) looking for files that contain the text “old text”. You can really get creative with regex here, which is facilitated by the “-e” option.

Grep provides the list of files (the “-l” option) upon which to perform the actual search-and replace (you can run that part alone to see its output); this list of files is then piped to the set command, which then does the actual work of searching and replacing the old text with new text.

The “-i” option of sed results in edit-in-place, meaning the original file is modified (in my situation, this was necessary – but sed can be told to make a backup if you supply it with -i). The “s” in “s/…” means “substitute” and the “g” means “global replacement” – which is exactly what happens.

2. Search and Replace Specified File

If you happen to know the name of the file you need to update, you can use this variation:


find -name myconfig_file.txt | xargs sed -i 's/old text/new text/g'

This is basically the same idea, but instead using the find operation to provide a list of files that match the given name. This runs through all the sub-directories starting from current. The list of files is then piped to sed in the same manner as described above.

P.S Above command tested on Ubuntu and RedHat

References

  1. grep command reference
  2. xargs command reference
  3. sed command reference
  4. sed command hits “undefined label” error on Mac OS X

About Author

author image
Cory Berg has been a professional software engineer for 20 years, in a variety of different roles, and with a variety of technologies. His software has been used on airplanes, in armies, and in some of the largest technology companies in the world.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments