Re: List files containing a string..



In article <JFqtMv.12pA@xxxxxxx> bv@xxxxxxx (Bill Vermillion) writes:
In article <Q-6dnWQdVLdGEpDbnZ2dnUVZ_vCknZ2d@xxxxxxxxxxx>,
Max Files <maxfiles@xxxxxxxxx> wrote:

"Per Hedeland" <per@xxxxxxxxxxxx> wrote in message
news:eujsig$18f1$4@xxxxxxxxxxxxxxx
In article <slrnf0q9s0.va.philip+usenet@xxxxxxxxxxxxxxxxxxxx> Philip
Paeps <philip+usenet@xxxxxxxx> writes:
Geir <geir02@xxxxxxxxx> wrote:
Pardon my ignorance, but if I need to search all files on a disk and
list
the path/name of files containing <string>, how would I do that in
freebsd?

With find(1).

eg: find / -name '*string*'

Er, the question was about files containing a string, not about file
*names* containing a string. And while I basically agree that this
question rather belongs in comp.unix.shell or the like, I think we have
to accept that the concept of "Unix" isn't as clear-cut as it is for us
old-timers - people run FreeBSD or Linux-<mumble>, and if they wonder
about something they (at best) post a question in the group dedicated to
that OS. We do see a lot of non-FreeBSD-specific questions here, and
they mostly get concrete replies, which I think is reasonable. So
finally, the correct reply is:

find / -type f | xargs grep -l string

And anyone who wants to take that as a cue to talk about -print0 is
welcome to do so.:-)

--Per Hedeland
per@xxxxxxxxxxxx

I love you man.....

I've been asking this kind of question for a long time. And all I ever
got were responses like man grep, and check out another newsgroup.

I was never aware of the xargs command. At least now I can do a
man xargs and find out more.

What is not readily apparent in the xargs man page is that it
avoids the problems that occur when you use an -exec command in
a find statement and get the error about the list being too large.

Uh, maybe because it doesn't?:-) I.e. there is no such problem since
-exec only ever gets a list of length 1. I guess you're thinking of

grep string `find / -type f`

But actually the xargs man page doesn't say anything about why you'd
want to use it at all:-) - and indeed avoiding the argument length limit
of execve(2) is certainly the main reason.

And it is also more efficient working on many items at once
instead of one at a time when you typically use -exec.

Definitely so, but the `` variant would probably be more efficient still
if it only worked.:-) Hm, well, find | xargs may be "better" due to
making use of parallellism, in particular if the command is
CPU-intensive and/or if you have multiple CPUs, also it may make better
use of file system caching.

And for completeness, I guess it should be mentioned that the GNU grep
that is the standard one on FreeBSD has a -r/-R option and even
--include and --exclude, which means it can subsume some of the
functionality provided by 'find' - the above find | xargs pipeline is
(mostly) functionally equivalent to

grep -lr string /

I don't really "like" that because it's "non-standard", but laziness has
made me use it more and more since all the Unices that I normally use
have GNU grep... Hm, maybe not on '/' though:

grep -lr foo /
grep: /dev/network: Permission denied
grep: /dev/geom.ctl: Permission denied
grep: /dev/devctl: Permission denied
grep: /dev/ata: Permission denied
grep: /dev/io: Permission denied
grep: /dev/sysmouse: Permission denied
grep: memory exhausted


Also, it seems grep -r follows symlinks, which you almost never want for
a recursing operation - e.g. 'find' doesn't do that unless you
specifically ask for it, for grep I can't even see a way to turn it off.

--Per Hedeland
per@xxxxxxxxxxxx
.



Relevant Pages

  • Looking for text inside files does not work at all
    ... for an ascii text string in a file. ... Hex-editing the file reveals that there is a string called "reloc" right ... So if text search (aka grep) worked, this should work too shouldn't it? ... That's how it worked in Windows 2000. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: modifying files using script
    ... specific string with another one. ... This script works well for one file but exits after that. ... so I used fgrep in the example (grep -F may be preferable or required ... as string by fgrep and RE by $editor, ...
    (comp.unix.shell)
  • Re: grep a string
    ... > I am trying a find all occurences of a SUBSTR in string ... > will -b option for grep be useful. ... SUBSTR or not. ... > perl script to find all occurences of SUBSTR.. ...
    (comp.unix.shell)
  • Looking for ascii text inside files doesnt work at all
    ... for an ascii text string in a file. ... Hex-editing the file reveals that there is a string called "reloc" right at the beginning of the file. ... So if text search (aka grep) worked, this should work too shouldn't it? ... I change the term in the _middle_ input field to 'i.cpl'. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: List files containing a string..
    ... the path/name of files containing, how would I do that in ... Er, the question was about files containing a string, not about file ... What is not readily apparent in the xargs man page is that it ...
    (comp.unix.bsd.freebsd.misc)