How can I do a fast text replace with recursive directories and filenames with spaces and single quotes? Preferably using standard UNIX tools, or alternatively a well-known package.
Using find
is extremely slow for many files, because it spawns a new process for each file, so I'm looking for a way that has directory traversing and string replacement integrated as one operation.
Slow search:
find . -name '*.txt' -exec grep foo {} \;
Fast search:
grep -lr --include=*.txt foo
Slow replace:
find . -name '*.txt' -exec perl -i -pe 's/foo/bar/' {} \;
Fast replace:
# Your suggestion here
(This one is rather fast, but is two-pass and doesn't handle spaces.)
perl -p -i -e 's/foo/bar/g' `grep -lr --include=*.txt foo`
+
is for. I've seen it around (eg. Emacsgrep-find
), but never pondered it's meaning. Way faster. Good point about not rewriting unchanged files (I assume perl has no shorthand way of avoiding this?) Good answer! One that will make my life easier! – forthrin Mar 30 '18 at 09:22