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?
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?
You can use a combination of find
, xargs
and sed
:
find ./ -type f -name "*.php" | xargs sed -i 's/\<oldfunction\>/newfunction/g'
< >
characters? don't you want s/\<oldfunction\>/\<newfunction\>/g
? or am i not recognizing a std RE element?
Oct 14, 2009 at 13:35
\<
(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.
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.)
-i .bak
instead of -i
Oct 14, 2009 at 16:27
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.
sed
for each found file; the xargs
version runs one instance with many arguments.
Oct 14, 2009 at 13:27
+
instead of \;
and it will reduce the number of sed processes so that it mimics xargs.
Oct 14, 2009 at 17:27
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.