Sed find and replace in folder

Saturday, 23 February 2008

Here's a handy one-liner to help you replace strings globally throughout all the files in a specified folder. This procedure is also called mass find and replace, or global replace.

You can even use it on windows if you're willing to spent a few minutes installing cygwin , a windows port of the gnu/linux most used utilities (including the bash shell and even X11)

The code below will replace every occurence of 'ugly' with 'beautiful'. 

Linux example: 

find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} ;

 

Cygwin example (you have to use unix-style paths):

find /cygdrive/c/xampp/joomla/ -type f -exec sed -i 's/ugly/beautiful/g' {} ;

 

 /cygdrive/c/ stands for the root of your C windows drive letter

Also note that Cygwin requires you to use the forward slash delimiter instead of the windows backslash delimiter. The bash shell allows TAB completion for paths, just type the first few letters and hit TAB to auto-complete the path. 

If you want to filter, let's say,  php files, you can add -name '*.php' to command-line arguments, just after the search path.

find /cygdrive/c/xampp/joomla/ -type f -name '*.php' -exec sed -i 's/ugly/beautiful/g' {} ;

Related articles(beta):



Give us some social love (it really works now)!

Reddit! Del.icio.us! StumbleUpon! Yahoo! Swik!



Comments (3)
RSS comments
1. Cygwin20-02-2010 18:18

Thanks for the info, 
 
to get this to work in Cygwin I needed tp append a \ before the ending semicolon, so it looks like this for instance: 
 
find ./ -type f -name '*.css' -exec sed -i 's|OLD|NEW|g' {} \; 
 
without the final \ I'd get: 
 
find: missing argument to `-exec' 
 
Thanks again!

2. Software Engineer28-05-2008 23:57

Hi, 
 
I appreciated the sample mass replace line. 
 
On my linux (Gentoo), I had to escape the semicolon to get it to work: 
find . -type f -name "*.sh" -exec sed -i 's/projects/sandbox/g' '{}' \;  
 
It then worked great! 
 
Thanks again, 
 
j

3. Delimiters05-05-2008 20:16

If you're having trouble trying to replace strings that contain slashes or backslashes, you can always use pipe '|' as a string delimiter 
find /cygdrive/c/xampp/joomla/ -type f -name '*.php' -exec sed -i 's|yes\/no|ok\/cancel|g' {} \;

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Please don't use comments to plug your web site. Links are rel='nofollow'-ed
  • Please refresh the page if you're having trouble with the security image code
Name:
E-mail
Homepage
Title:
Comment:

:) :grin ;) 8) :p
:roll :eek :upset :zzz :sigh
:? :cry :( :x
Code:* Code

Last Updated ( Thursday, 18 September 2008 )
 
Post and Bid on Joomla/PHP projects! Join EUFreelance.com for free!

Europe freelancer directory

Newsletter

Subscribe to TeachMeJoomla's newsletter
Name:
Email:


Auto tags

sed replace

sed find and replace

find sed

sed find

sed search and replace

sed find replace

sed search replace

find replace sed

sed global replace

find exec sed

linux sed replace

find and replace sed

Sed global search and replace

find and sed

sed search and replace example

find and replace with sed

find sed replace

linux sed search and replace

sed replace line

search replace sed

sed folder

search and replace sed