X

Recursively replacing a string in all files in a directory [Linux]

Ramkumar Menon
Director, Product Strategy

My colleague provided me an extremely useful script that will replace a source string with a target string in all files in the current directory and its subdirectories.

find . -type f | xargs perl -pi~ -e 's/oldtext/newtext/g;'


Replace "oldtext" with the source string, and "newtext" with the target string.



During the replacement, all original files are backed up with a  "~" suffix.

Join the discussion

Comments ( 6 )
  • Paul Dunbar Monday, April 14, 2008
    Or you could use
    find . -type f -exec sed -i=~ -e 's/oldtext/newtext/g' and reduce the number of utilities involved by 1
  • Ramkumar Menon Monday, April 14, 2008
    thanks for the input, Paul.
  • Laurent Schneider Tuesday, April 15, 2008
    According to the title, you are using UNIX and sed -i is a Linux-only parameter.
    So on Solaris and AIX, use perl :-)
  • Ramkumar Menon Tuesday, April 15, 2008
    Thanks for pointing out. I have modified the title to reflect this.
  • Website Promotion Tuesday, April 26, 2011
    With havin so much content do you ever run into any issues of plagorism or copyright infringement? My website has a lot of completely unique content I've either authored myself or outsourced but it appears a lot of it is popping it up all over the internet without my authorization. Do you know any methods to help protect against content from being ripped off? I'd genuinely appreciate it.
  • Gudrun Gaudette Wednesday, April 27, 2011
    Loving the info on this website , you have done great job on the articles .
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha