Partitioning searching from processing
There are two ways to grep recursively through all files of a certain type.
-
grep -R --include='*.txt' 'search string' * -
find -type f -name '*.txt' -print0 | \ xargs -0 grep 'search string'
While grep’s include directive is certainly more concise, I’d argue that learning all of the general case is more useful in the long run. What if you want to exclude a certain path? What it you want to change file permissions instead of grepping? Combining find with xargs tends to be far more flexible than the alternatives.
Examples
-
find -type f -not \( -path '*/.svn/*' -a -prune \) \ -print0 | xargs -0 grep 'search string' -
find -type f -print0 | xargs -0 du | sort -g | tail
One Response to “Partitioning searching from processing”
Leave a Reply
Phil Dufault on October 1st, 2009
Using xargs with find is for noobs!