Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's the test script I'm running

matt@server:~ $ cat test.sh
mkdir test
cd test
echo "has the string foo" > yes.txt
echo "hasn't the string" > no.txt
ls -l --time-style=full-iso
cat *
perl -e 's/foo/bar/g;' -pi $(find -type f)
ls -l --time-style=full-iso
cat *
matt@server:~ $ ./test.sh
total 8
-rw-r--r-- 1 matt matt 18 2011-01-29 13:52:17.240316663 -0700 no.txt
-rw-r--r-- 1 matt matt 19 2011-01-29 13:52:17.240316663 -0700 yes.txt
hasn't the string
has the string foo
total 8
-rw-r--r-- 1 matt matt 18 2011-01-29 13:52:17.260317727 -0700 no.txt
-rw-r--r-- 1 matt matt 19 2011-01-29 13:52:17.260317727 -0700 yes.txt
hasn't the string
has the string bar

I need to figure out how to tweak this line:

perl -e 's/foo/bar/g;' -pi $(find -type f)

To not write every file it finds but to just write the files that need to be changed.

share|improve this question
up vote 2 down vote accepted

This should be a suitable replacement:

grep -l foo * | sed -e 's/[^/0-9A-Z_a-z]/\\&/g' | xargs sed -i 's/foo/bar/g'

share|improve this answer
Note that special characters in file names (\'" and whitespace) need to be protected against expansion by xargs. – Gilles Jan 30 '11 at 19:10

Try this:

perl -e 's/foo/bar/g;' -pi `egrep -l 'foo' $(find -type f)`

That searches for the regular expression and returns on the filenames where it is found. Then Perl will operate only on those files.

share|improve this answer
Might as well run $(egrep -rl 'foo' .) – jsbillings Jan 29 '11 at 23:37
Using the result of command substitution as a list of file names doesn't work if the file names can contain special characters (\[*? or whitespace). This is also a problem in the question. – Gilles Jan 30 '11 at 19:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.