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 thought on “Partitioning searching from processing

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>