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 ...
- 03-21-2008 #1Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Using find and grep
Someone recently posted:
The simplest way to do this is: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
Code:find /usr/src -name '*.h' | xargs grep 'integer_'
- 03-21-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
- 03-23-2008 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
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.
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.But to tell the truth, you don't even need find:
Code:grep -r 'integer_' /usr/src/
- 03-24-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 03-24-2008 #5Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Well, that's true.

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.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. and that's why the other thread suggests using the while construct:
This will work always, as long as everything is correctly written and quoted. Which is why this way is preferable sometimes.Code:find .... | while read file
- 03-25-2008 #6Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
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')


Reply With Quote
