Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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

find ./ -type f -name "*.php" | xargs sed -i 's/\<oldfunction\>/newfunction/g'
share|improve this answer
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 '09 at 12:17
you're right, fixed. – John T Oct 14 '09 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? – quack quixote Oct 14 '09 at 13:35
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. – John T Oct 14 '09 at 13:57
i thought it might be but couldn't find a quick sed-regex reference to verify. thanks! – quack quixote Oct 14 '09 at 16:25

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.)

share|improve this answer
i think the sed versions will do this too if you use -i .bak instead of -i – quack quixote Oct 14 '09 at 16:27
aha, I just RTFM and ~quack is right. sed will do this too. – Doug Harris Oct 14 '09 at 17:26
I'd love to see the Python variation (I'm trying to learn it) :) – Pitto Mar 26 '12 at 7:56

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.

share|improve this answer
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. – quack quixote Oct 14 '09 at 13:27
yep, more forking involved. – John T Oct 14 '09 at 13:51
If your find supports it, use + instead of \; and it will reduce the number of sed processes so that it mimics xargs. – Doug Harris Oct 14 '09 at 17:27
never paid attention to that... thanks for the clarification. – Nasir Oct 14 '09 at 17:35

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.

share|improve this answer

You must log in to answer this question.