Linux: How do I mass find and replace text in files under linux using perl?
Few friends have asked me how to do mass find and replace text in files under linux. There are quite a bit of options in linux to achieve mass replacing of text in files. If you are doing it file by file, you can achieve that in vi by opening, running replace and closing and going to next file. But sometimes that can be very tedious and you would rather do mass replacement on all files containing certain extension. We can do this by using sed or perl. Since most people are familiar with perl (at least most system admins and programmers), I will show you a perl way of doing it which you can use with sed as well if you wish. First step is to get perl to do what we want on one file
perl -w -i -p -e "s/search_text/replace_text/g" filename
-w turns warnings on
-i makes Perl operate on files in-place (if you would like to make backups, use -i.bak, this will save filename.bak files)
-p loops over the whole input
-e specifies Perl expression
filename works on one file at a time
Once we get the results we want, we can now pass it multiple files by doing something like:
perl -w -i -p -e "s/search_text/replace_text/g" *.php
This will search and replace within all php files in the directory you are in.
Now, let us say you want to go through your whole web directory and replace every place where you have “Perl is good” to “Perl is great”, you would use following command:
find /www_root -name "*.php"|xargs perl -w -i -p -e "s/Perl is good/perl is great/g"
find will start at /www_root, look for filenames which have .php extension, xargs takes that filename and passes it to perl as an arguement.
————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.


Handy perl bits like this rule. I, myself, am an ignorant schnub and rely on folks like you to share your hard-earned knowledge.
Thanks for it. I found it on Google quickly with the following search phrase:
“find and replace text in directory +linux”
Thanks,
machiner
Here’s a time saver:
find /www_root -name “*.php” | …
if you’re already in the desired path:
ls -1 *.php |
Using google, i found several solutions for this problem. But this one was the shortest, so I used it and it worked!
Great post!
[...] and xargs takes the filename and passes it to perl. To understand what perl command is doing, see this post. Print This Post (No Ratings Yet) Loading … digg_url = [...]
[...] You can read the original article here: Linux: How do I mass find and replace text in files under linux using perl? | Technology: Learn and …. [...]
THANK YOU !!
My friend’s website was hacked and then its files were infested with malicious links.
It’s wordpress – there seems to be a mass hack on websites running old version of wordpress.
It would take HOURS to clean up those bloody links.
With the trick mentioned here, it took only several seconds. Awesome.
Thanks again !
Excellent Help!!!Thanks a lot.
Thanks to you, I was able to clean up a big mistake one of my rookie developers made in a matter of seconds.
Thanks a ton.
how is it possible to replace text with another that contains ” and / \ *
You would escape them: \” and \/ \\ \*
so putting \ in front should do the trick.
[...] this could be done through an sed script , but found a better one using perl from here. export OLD_TEXT=’<link rel=”stylesheet” type=”text/css” [...]
great article..
What a great article!!! THANKS you saved me some work. I used to have a different method of doing this with Perl, and so I started trying to refresh my memory and found you. Great job! Proves that Perl’s mantra is alive and well in that there is “Always more than one way to do it”.