3

I have a function name that was misspelled, and I want to change it. The function is called in many places of the project.

How can I do this with a Linux command on SUSE?

4 Answers 4

4

You can use a combination of find, xargs and sed:

find ./ -type f -name "*.php" | xargs sed -i 's/\<oldfunction\>/newfunction/g'
5
  • careful there! Doing a s/he/him/ on my comment would break "there", and that'd be difficult to fix as simply! Take backups first, and remember sed uses regex, so you have quite a bit of leeway in how you write your parameters.
    – Phoshi
    Oct 14, 2009 at 12:17
  • you're right, fixed.
    – user1931
    Oct 14, 2009 at 12:28
  • aren't you trimming off the surrounding < > characters? don't you want s/\<oldfunction\>/\<newfunction\>/g ? or am i not recognizing a std RE element? Oct 14, 2009 at 13:35
  • 1
    Yea, they are word boundaries, I forgot to add them but Phoshi reminded me. Without \< (beginning of word) and \> (end of word), any occurance of the first string will be replaced in the file. Example, if his function is named len, it will also replace the len in strlen and anywhere else len is found in each file regardless if it's a single word or part of a ward, which is not what he wants. You have to make sure you are only replacing complete words, as sed has no knowledge of "words" without them.
    – user1931
    Oct 14, 2009 at 13:57
  • i thought it might be but couldn't find a quick sed-regex reference to verify. thanks! Oct 14, 2009 at 16:25
2

A variant done with Perl:

find . -type f -name "*.php" -print0 | xargs -0 perl -pi.bak -e 's/oldfunction/newfunction/g'

An advantage to this is that Perl will create the backup files (with .bak extension) so that if you screw it up, you can revert.

(The -print0 option to find and the -0 option for xargs are only needed if you have any files or directories with spaces in them.)

3
  • i think the sed versions will do this too if you use -i .bak instead of -i Oct 14, 2009 at 16:27
  • aha, I just RTFM and ~quack is right. sed will do this too. Oct 14, 2009 at 17:26
  • I'd love to see the Python variation (I'm trying to learn it) :)
    – Pitto
    Mar 26, 2012 at 7:56
1

This is exactly the same thing as John T's solution:

find ./ -type f -name "*.php" -exec sed -i 's/\<oldfunction\>/newfunction/g' {} \;

I usually don't pipe out to xargs

EDIT: more expensive.... per comments below.

4
  • there's one difference: this runs a separate instance of the sed for each found file; the xargs version runs one instance with many arguments. Oct 14, 2009 at 13:27
  • yep, more forking involved.
    – user1931
    Oct 14, 2009 at 13:51
  • 1
    If your find supports it, use + instead of \; and it will reduce the number of sed processes so that it mimics xargs. Oct 14, 2009 at 17:27
  • never paid attention to that... thanks for the clarification.
    – Nasir
    Oct 14, 2009 at 17:35
0

Supplement to the other very good suggestions already posted: make a backup of the whole site (cp -r /var/www /var/wold) and do the editing there. Personally I would then move the modified files under, for instance /var/www/changed and test http://www.sitename.tld/changed to make sure all the scripts work before going live.

You must log in to answer this question.