Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Search-replace recursively in files and folders UNIX
I wrote a bash script that lets you search and replace text strings in files of a specified folder and its subfolders recursively. Key features:
  • recursive operation with immediate visible results in simulation mode (no waiting time)
  • perl/POSIX compliant regex for search strings
  • retains ownership and permissions of replaced files
  • cross-platform (BSD/Mac/POSIX Linux compatible)
  • easy to compare ANSI-colorized diff output (switchable)
  • safe: creates and removes temporary files in the system temporary folder
  • simulation mode
  • free :)


One of the main reasons for me to write this script compared to existing alternatives was a possibility to implement simulation mode (fake replacement demonstration mode) combined with presenting clear results, in order to quickly distinguish between searched and replaced text.

In simulation mode (switch -s) no actual replacements will be performed, but you can see what is being replaced (with ANSI color diff output, if colors are supported by your terminal; even work with pipes). By utilising 'while read file' loop it is possible to see results coming at once with no delay, so long as there are matched strings found -- makes perfect sense for a long tree of folders and sub-folders -- no waiting time until script finishes.

[crarko adds: The author mailed me an updated version of the script; please download it at the link given below.]

Some examples of usage:

Replace 'blue' with 'green' in all files in the current directory tree. No actual replacement is done because of the '-s' switch (run in simulation mode)

srpl -s . 'blue' 'green'

Delete leading whitespace (spaces, tabs) from front of each line -- aligns all text flush left.

srpl 's/^[ \t]*//'

Tested on Mac OS X 10.6.3, Linux Slackware 8.1, 10.1 and Gentoo 2.0.1.

[crarko adds: I haven't tested this one. The script's source is available at here, along with that of another script by the same author. I left out some additional examples of using the script to recursively edit PHP source files, but it looks like a useful tool in that area.]
    •    
  • Currently 1.77 / 5
  You rated: 2 / 5 (22 votes cast)
 
[10,231 views]  

Search-replace recursively in files and folders | 8 comments | Create New Account
Click here to return to the 'Search-replace recursively in files and folders' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Search-replace recursively in files and folders
Authored by: nschum on Jun 18, '10 10:38:34AM
Looks powerful, but I usually just do:
find dir/ -type f -exec sed -i'' -e s/old/new/g "{}" \;
A little risky, maybe, but all my files are under version control (which takes care of the diffing, as well).

[ Reply to This | # ]
Search-replace recursively in files and folders
Authored by: ssevenup on Jun 18, '10 10:43:44AM

Nifty, thanks

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
Search-replace recursively in files and folders
Authored by: barefootguru on Jun 18, '10 11:11:18AM

For a GUI interface to replace items in multiple (sub) folders--using regular expressions--check out the free TextWrangler.



[ Reply to This | # ]
Search-replace recursively in files and folders
Authored by: llee on Jun 18, '10 11:36:49AM

I got errors referring to line 115 and 116 "tempfile: command not found" and "xargs: illegal option -- r", and the operation apparently halted. I'm using 10.6.4.



[ Reply to This | # ]
Search-replace recursively in files and folders
Authored by: locojohn on Jun 18, '10 12:16:18PM
Please download update at www.andrews.lv/tmp/rgrep-utils-1.0.1.tar.gz and let me know if it works.

Best regards,

Andrejs

[ Reply to This | # ]
In C, recursive
Authored by: Kalak on Jun 22, '10 09:00:23AM

"rpl - Replace Strings" is written in C. It has less forking, so it's great for a huge number of files. The bash / find / perl suggestions all have to fork something, so for a large number of files, they slow down. It also has options to search multiple extensions only (great for websites so it doesn't search images files, etc.) Also GPL.

http://www.laffeycomputer.com/rpl.html

---
--
Kalak
I am, and always will be, an Idiot.



[ Reply to This | # ]
Search-replace recursively in files and folders
Authored by: valhowells on Jun 29, '10 06:09:07AM

to change blue to green in all files *.xml

for i in $( ls *.xml ); do
sed -e 's/blue/green/' $i > x
mv -f x $i
done


pretty simple. this can even be done on the command line.



[ Reply to This | # ]
Search-replace recursively in files and folders
Authored by: terry81 on Aug 14, '10 08:16:30AM
The above solution work for text/code replacement only on the same row. If you need to replace text over multiple rows check this find and replace script written in Perl.

[ Reply to This | # ]