//View Tip #167
Similar Tips
» list the most recent files in a directory
» Bash function to decompress archives
» Vi mode in bash
» Version all unversioned files in an SVN checkout
» Zero files

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Find and replace recursively over several files:
perl -pi.bak -e "s/Bob/Steve/gi" *.html

The '.bak' will create copies of your original files with the .bak extension added incase of mistakes. Be careful of running this twice though as the backups will get overwritten.


View Comments »




Comments 

Add your comment

Comments are currently disabled
That command does not run recursively. "*.html" is expanded by the shell to match all files ending in ".html" for the current directory only.

Depending on the total number of files, you could do the same thing recursively like this:

 perl -pi.bak -e 's/arse/tits/' $(find . -iname "*.html")  # for a relatively small number of .html files

or

 find . -iname '*.html' -exec perl -pi.bak -e '{}' \;  # for a larger number of .html files

Actually, I think find has a way to modify -exec's behaviour so that it builds the list of files instead of execing for each match. I can't be bothered to look it up right now.
Posted 2008-09-25 02:03:21

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit