List files containing a string.. - BSD

This is a discussion on List files containing a string.. - BSD ; Hi, Pardon my ignorance, but if I need to search all files on a disk and list the path/name of files containing , how would I do that in freebsd? Thanks for an elegant solution on this /geir...

+ Reply to Thread
Results 1 to 13 of 13

Thread: List files containing a string..

  1. List files containing a string..

    Hi,

    Pardon my ignorance, but if I need to search all files on a disk and
    list the path/name of files containing , how would I do that in
    freebsd?

    Thanks for an elegant solution on this

    /geir

  2. Re: List files containing a string..

    On Mar 28, 6:07 am, Geir wrote:
    > Hi,
    >
    > Pardon my ignorance, but if I need to search all files on a disk and
    > list the path/name of files containing , how would I do that in
    > freebsd?
    >
    > Thanks for an elegant solution on this
    >
    > /geir


    comp.unix.shell is your friend


  3. Re: List files containing a string..

    In our last episode,
    <56v0j9F2aafnlU1@mid.individual.net>,
    the lovely and talented Geir
    broadcast on comp.unix.bsd.freebsd.misc:

    > Hi,


    > Pardon my ignorance, but if I need to search all files on a disk and
    > list the path/name of files containing , how would I do that in
    > freebsd?


    > Thanks for an elegant solution on this


    See man grep

    --
    Lars Eighner
    Countdown: 664 days to go.

  4. Re: List files containing a string..

    Geir wrote:
    > Pardon my ignorance, but if I need to search all files on a disk and list
    > the path/name of files containing , how would I do that in freebsd?


    With find(1).

    eg: find / -name '*string*'

    - Philip

    --
    Philip Paeps Please don't email any replies
    philip@paeps.cx I follow the newsgroup.

    BOFH Excuse #203:
    Write-only-memory subsystem too slow for this machine. Contact your local
    dealer.

  5. Re: List files containing a string..

    In article Philip
    Paeps writes:
    >Geir wrote:
    >> Pardon my ignorance, but if I need to search all files on a disk and list
    >> the path/name of files containing , 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-, 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@hedeland.org


  6. Re: List files containing a string..


    "Per Hedeland" wrote in message
    news:eujsig$18f1$4@hedeland.org...
    > In article Philip
    > Paeps writes:
    >>Geir wrote:
    >>> Pardon my ignorance, but if I need to search all files on a disk and
    >>> list
    >>> the path/name of files containing , 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-, 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@hedeland.org
    >

    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.

    And for all you posters who think you're helping some poor putz out
    with the man something....man this!

    man man



  7. Re: List files containing a string..

    On Mar 30, 5:32 pm, "Max Files" wrote:
    > "Per Hedeland" wrote in message
    >
    > news:eujsig$18f1$4@hedeland.org...
    >
    > > In article Philip
    > > Paeps writes:
    > >>Geir wrote:
    > >>> Pardon my ignorance, but if I need to search all files on a disk and
    > >>> list
    > >>> the path/name of files containing , 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-, 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
    > > p...@hedeland.org

    >
    > 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.
    >
    > And for all you posters who think you're helping some poor putz out
    > with the man something....man this!
    >
    > man man


    O man O man


  8. Re: List files containing a string..

    In article ,
    Max Files wrote:
    >
    >"Per Hedeland" wrote in message
    >news:eujsig$18f1$4@hedeland.org...
    >> In article Philip
    >> Paeps writes:
    >>>Geir wrote:
    >>>> Pardon my ignorance, but if I need to search all files on a disk and
    >>>> list
    >>>> the path/name of files containing , 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-, 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@hedeland.org
    >>

    >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.
    And it is also more efficient working on many items at once
    instead of one at a time when you typically use -exec.

    >And for all you posters who think you're helping some poor putz out
    >with the man something....man this!
    >
    >man man


    And don't overlook 'apropos' which equivalent to man -k .

    Bill

    --
    Bill Vermillion - bv @ wjv . com

  9. Re: List files containing a string..

    In the interest of completeness... I should point out
    two more gotcha's:

    1) As has been mentioned in this thread already,
    if you're on FreeBSD, you should use print0 to avoid
    troubles when a filename contains space characters.

    2) If the pattern you're searching for could one day
    start with "-", then it would confuse grep.
    So you should add "-e" to make it explicit.

    3) The first filename could start with -
    which would confuse grep.
    So you should add "--".

    Like this:

    find DIRECTORY -type f -print0 | xargs -0 grep -l -e PATTERN --

    Or, since grep supports directory search,
    it's much faster to just do this:

    grep -r -l -e PATTERN -- DIRECTORY



  10. Re: List files containing a string..

    In article bv@wjv.com (Bill Vermillion) writes:
    >In article ,
    >Max Files wrote:
    >>
    >>"Per Hedeland" wrote in message
    >>news:eujsig$18f1$4@hedeland.org...
    >>> In article Philip
    >>> Paeps writes:
    >>>>Geir wrote:
    >>>>> Pardon my ignorance, but if I need to search all files on a disk and
    >>>>> list
    >>>>> the path/name of files containing , 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-, 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@hedeland.org
    >>>

    >>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@hedeland.org

  11. Re: List files containing a string..

    per@hedeland.org (Per Hedeland) writes:
    > 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 /


    True, very true

    > 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


    I usually ssh into Solaris build systems at work, and I am facing a
    similar but slightly funny problem. My brain has started hardcoding the
    fact that grep doesn't work with -r (so it should always be used with
    find | xargs ...), but `ggrep' does work with -r (being the SFW version
    of GNU grep).

    So there are cases when I'm sitting merrily on a FreeBSD box and I type:

    $ ggrep -r PATTERN tmp/DIR

    and amaze myself for a while when I get:

    bash: ggrep: command not found

    > 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.


    That's another reason why the find | xargs trick is cool


  12. Re: List files containing a string..

    Giorgos Keramidas wrote:
    > per@hedeland.org (Per Hedeland) writes:
    > > grep: /dev/network: Permission denied
    > > grep: /dev/geom.ctl: Permission denied

    >
    > I usually ssh into Solaris build systems at work, and I am facing a
    > similar but slightly funny problem.


    You Are Not Alone[tm]. :-o

    > My brain has started hardcoding the fact that grep doesn't work with -r (so
    > it should always be used with find | xargs ...), but `ggrep' does work with
    > -r (being the SFW version of GNU grep).


    To keep things simple, I have a $HOME/bin.SunOS (and bin.Linux) which I list
    very early in my $PATH and where I stick things that Solaris (and Linux) do
    wrong (or different, depending on ones perception of Right and Wrong).

    In there, is a grep, a vi (don't you love "terminal too wide"?) an awk, and a
    number of other things.

    I find it much simpler to have a predictable set of tools around in a
    predictable place than having to remember to use different tools on different
    machines.

    On Solaris, I also list /usr/ucb early in my $PATH. I like having a "ps" that
    works. But I digress...

    It makes my .cshrc a mess of "if ($OSTYPE ... )" constructions, but it's
    better than keeping those constructions in my head. :-)

    - Philip

    --
    Philip Paeps Please don't email any replies
    philip@paeps.cx I follow the newsgroup.

    Never insult an alligator
    until after you have crossed the river.

  13. Re: List files containing a string..

    Philip Paeps writes:
    >Giorgos Keramidas wrote:
    >> I usually ssh into Solaris build systems at work, and I am
    >> facing a similar but slightly funny problem.

    >
    > You Are Not Alone[tm]. :-o


    LoL! Thanks Philip :-)

    >> My brain has started hardcoding the fact that grep doesn't
    >> work with -r (so it should always be used with find | xargs
    >> ...), but `ggrep' does work with -r (being the SFW version of
    >> GNU grep).

    >
    > To keep things simple, I have a $HOME/bin.SunOS (and bin.Linux)
    > which I list very early in my $PATH and where I stick things
    > that Solaris (and Linux) do wrong (or different, depending on
    > ones perception of Right and Wrong).
    >
    > In there, is a grep, a vi (don't you love "terminal too wide"?)
    > an awk, and a number of other things.


    Oh Gods, the horrors!

    But we are waaaaaaaaay off topic now


+ Reply to Thread