Find and Replace Text in Multiple Files

(0 votes, average 0 out of 5)

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

 

 

Partner Links:
Last Updated on Tuesday, 10 August 2010 18:18  
Related Articles

» Access Windows Files From within a Ubuntu (Wubi) Install

So maybe you have just installed Ubuntu through the Wubi installation or have been using Ubuntu through Wubi for a while, but you find yourself trying to get access to your Window's documents/music/videos/etcetera from Ubuntu and your not sure where to look.  Well, the solution is pretty simple but not very intuitive for most users. Follow these instructions to get access to the files contained within your Windows installation:Assuming your using Ubuntu 10.xx, go to the top left:...

» How to Use gprof (The Basics)

gprof is a GNU profiler, which allows users to see important information about their code such as how much time is spent in a function or how often certain call trees are executed.  It's a very powerful program -- this article will give you only the information you need to know to see a basic report.  Here's how to do it. 1.  Compile Your Code with the -pg flagCompile your C code as you typically would, but add in a -pg flag.  This indicates to the compiler that it should...

» Mount a WebDAV filesystem (like box.net) on Linux using davfs2

You can easily mount a WebDAV filesystem on Linux so that you can access the files just like they were on your local computer. One way to do is to install the davfs2 package (this is the Ubuntu/Debian package name, though it should be similar for other distros). Once you have installed it, add an entry to /etc/davfs2/secrets with your login credentials for the WebDAV account. For box.net, the command would look like this:sudo echo “https://www.box.net/dav username password” >>...