How do I recursively find and replace only in files named index.php on Linux webserver?

97 views Asked by At

I'm trying to perform a recursive find and replace on a Linux webserver (searches 100,000+ files, but it takes so long that my SSH session times out. Is there a way to only search files named "index.php"? This would cut down on execution time dramatically.

Here is the command I'm using:

$     find . -type f -print0 | xargs -0 sed -i “s%Apples%Oranges%g”
1

There are 1 answers

0
Toby Speight On

Modify your find command to add the -name option:

find . -name 'index.php' -type f -print0 | xargs -0 sed -i "s/Apples/Oranges/g"

(I put the -name predicate first, on the principle that it's marginally faster than anything which needs to stat() files, but it may well be that find optimizes for that anyway.)

Also, consider a parallel invocation of xargs; e.g. with GNU xargs:

find . -name 'index.php' -type f -print0 \
  | xargs --max-procs `getconf _NPROCESSORS_ONLN` -0 sed -i "s/Apples/Oranges/g"