PHP - How to replace a string in a very large number of files?

I have two million text files on a server that is accessible to internet users. I was asked to make changes (line replace operation) to these files as soon as possible. I was thinking about creating str_replace

for every text file on the server. However, I don't want to bind the server and make it inaccessible to Internet users.

Do you think this is a good idea?


ini_set('max_execution_time', 1000);

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
  //do str_replace stuff on the file



source to share

3 answers

Use find , xargs and sed from shell

, that is:

cd /dir

find . -type f -print0 | xargs -0 sed -i 's/OLD/NEW/g


Will search all the files recursively (hidden as well) within the current dir

and replace OLD

on NEW

using sed


Why -print0


From man find :

If you are piping the search output into another program and there is a very slight chance that the files you are looking for may contain a newline, then you should seriously consider using the '-print0' option instead of '-print'.

Why xargs


From man find :

The specified command runs once for each associated file.

That is, if /dir

there are 2000 files, it find ... -exec ...

will result in 2000 calls sed

; whereas it find ... | xargs ...

will only call sed

once or twice.



Don't do this with PHP, it will most likely fail and I'll take over all your system resources.

find . -type f -exec sed -i 's/search/replace/g' {} +


Example above with string search and replace, as well as recursive and regular files, including hidden ones.



You can also do this with a Python program limited to one core (the default). If your machine has multiple cores, and at least one is usually free, you should have it installed.



All Articles