Running a command on all files that contain a string

4

I'm finding a bunch of files with find . -exec grep -l mypattern {} \;

What's the simplest, most elegant way to run a certain command separately on each of the resulting list of files?

GJ.

Posted 2012-05-10T12:12:36.947

Reputation: 8 151

Answers

9

To begin with, use:

grep -Rl mypattern .

directly instead of the find construct.

What is the certain command? Probably the best and most fail-safe way would be

grep -ZRl mypattern . | xargs -0 mycommand

Exactly what you want to do is up to xargs from here.

Daniel Andersson

Posted 2012-05-10T12:12:36.947

Reputation: 20 465