Find the answer to your Linux question:
Results 1 to 6 of 6
Someone recently posted: Here is what works for me, to use grep on only those files found by find: Code: find /usr/src/ -name '*.h' | while read FILE do if ...
  1. #1
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044

    Using find and grep

    Someone recently posted:
    Here is what works for me, to use grep on only those files found by find:
    Code:
    find /usr/src/ -name '*.h' | while read FILE
    do
        if grep 'integer_' $FILE
        then
              echo $FILE
       fi  
    done
    The simplest way to do this is:
    Code:
    find /usr/src -name '*.h' | xargs grep 'integer_'

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by scm View Post
    Someone recently posted:

    The simplest way to do this is:
    Code:
    find /usr/src -name '*.h' | xargs grep 'integer_'
    Not really, this is simpler because you don't need a pipe:

    Code:
    find /usr/src -name '*.h' -exec grep 'integer_' '{}' \;
    But to tell the truth, you don't even need find:

    Code:
    grep -r 'integer_' /usr/src/

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by i92guboj View Post
    Not really, this is simpler because you don't need a pipe:

    Code:
    find /usr/src -name '*.h' -exec grep 'integer_' '{}' \;
    The disadvantages with this approach are that it has to invoke grep for each filename, so performance may be an issue, and also since you're only grepping a single file at a time, matching lines will be shown without a filename, so not very useful. Of course, you could overcome the latter with the /dev/null trick, but you'll still require grep to be exec-ed for each file.

    But to tell the truth, you don't even need find:
    Code:
    grep -r 'integer_' /usr/src/
    True, if you're using GNU grep, but if you're trying to write portable scripts you'd do well to consider the versions of grep you're likely to need to accommodate on some quite elderly UNIX systems.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by scm View Post
    Someone recently posted:

    The simplest way to do this is:
    Code:
    find /usr/src -name '*.h' | xargs grep 'integer_'
    i am a bit lazy to try this out, say if there is too many *.h files, will it produce some "argument too long" blah blah errors. Just a thought.

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by scm View Post
    The disadvantages with this approach are that it has to invoke grep for each filename, so performance may be an issue, and also since you're only grepping a single file at a time, matching lines will be shown without a filename, so not very useful. Of course, you could overcome the latter with the /dev/null trick, but you'll still require grep to be exec-ed for each file.
    Well, that's true.

    True, if you're using GNU grep, but if you're trying to write portable scripts you'd do well to consider the versions of grep you're likely to need to accommodate on some quite elderly UNIX systems.
    Yes, but since this is mostly a linux forum, we can assume that all people around here use gnu stuff. But you indeed have the point: if portability is an issue, we should not use obscure features.

    Quote Originally Posted by ghostdog74 View Post
    i am a bit lazy to try this out, say if there is too many *.h files, will it produce some "argument too long" blah blah errors. Just a thought.
    Yes. and that's why the other thread suggests using the while construct:

    Code:
    find .... | while read file
    This will work always, as long as everything is correctly written and quoted. Which is why this way is preferable sometimes.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by ghostdog74 View Post
    i am a bit lazy to try this out, say if there is too many *.h files, will it produce some "argument too long" blah blah errors. Just a thought.
    Not a very good thought, however - xargs was primarily invented precisely to overcome the early limitations of the shell environment so unless GNU xargs is broken it will always make the argument list fit within the environment space. If I could guarantee that the find list would fit the environment I'd have done
    Code:
    grep 'integer_' $(find /usr/src -name '*.h')

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...