sed - PHP - How to do a string replace in a very large number of files? -
i have 2 million text files in server online accesible internet users. asked make change (a string replace operation) these files possible. thinking doing str_replace
on every text file on server. however, don't want tie server , make unreachable internet users.
do think following idea?
<?php ini_set('max_execution_time', 1000); $path=realpath('/dir/'); $objects = new recursiveiteratoriterator(new recursivedirectoryiterator($path), recursiveiteratoriterator::self_first); foreach($objects $name => $object){ set_time_limit(100); //do str_replace stuff on file }
use find, xargs , sed shell
, i.e.:
cd /dir find . -type f -print0 | xargs -0 sed -i 's/old/new/g
will search files recursively (hidden also) inside current dir
, replace old
new
using sed
.
why -print0
?
from man find:
if piping output of find program , there faintest possibility files searching might contain newline, should consider using '-print0' option instead of '-print'.
why xargs
?
from man find:
the specified command run once each matched file.
that is, if there 2000 files in /dir
, find ... -exec ...
result in 2000 invocations of sed
; whereas find ... | xargs ...
invoke sed
once or twice.
Comments
Post a Comment