find any occurance of a string in any file???

I want to search all the files on my computer for the occurance of a 
particular string.  Before you blast me, I know, this seems silly.  But I 
have a reason.

I can use find ./ -name * to list all files, but how do I send them through 
a "grep" to find any occurance of, say, "poohead" within the files ????


Mairhtin O'Feannag

0
irishboyca (42)
10/8/2003 9:25:48 PM
comp.os.linux.misc 33511 articles. 0 followers. amosa69 (78) is leader. Post Follow

22 Replies
402 Views

Similar Articles

[PageSpeed] 24

On Wed, 08 Oct 2003 21:25:48 +0000, Mairhtin O'Feannag wrote:

> I want to search all the files on my computer for the occurance of a 
> particular string.  Before you blast me, I know, this seems silly.  But I 
> have a reason.
> 
> I can use find ./ -name * to list all files, but how do I send them through 
> a "grep" to find any occurance of, say, "poohead" within the files ????

find / -type f -exec grep -l poohead {} +

0
daveuhring (1185)
10/8/2003 9:50:13 PM
On Wed, 08 Oct 2003 21:25:48 GMT, Mairhtin O'Feannag <irishboyca@rocketmail.com> wrote:
> 
> 
> I want to search all the files on my computer for the occurance of a 
> particular string.  Before you blast me, I know, this seems silly.  But I 
> have a reason.
> 
> I can use find ./ -name * to list all files, but how do I send them through 
> a "grep" to find any occurance of, say, "poohead" within the files ????
> 
> 
> Mairhtin O'Feannag
> 

If you have GNU grep, you can do:

grep -rs  'poolhead'  /   # the r means recursive and the s silences error
                          # messages 

If not, then try something like this:

find /  -type f  -exec  grep 'poolhead' {} \;

The {} refers to whatever file find is currently focussing on.


-- 
Later, Alan C
You can find my email address at the website: contact.html
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
0
zzzzzz (1966)
10/8/2003 9:59:12 PM
Mairhtin O'Feannag wrote:

> I want to search all the files on my computer for the occurance of a
> particular string.  Before you blast me, I know, this seems silly.  But I
> have a reason.
> 
> I can use find ./ -name * to list all files, but how do I send them
> through a "grep" to find any occurance of, say, "poohead" within the files
> ????

$ find . | xargs grep pattern

-- 
Paul Lutus
http://www.arachnoid.com

0
nospam248 (2592)
10/8/2003 10:06:41 PM
Dave Uhring wrote:

> On Wed, 08 Oct 2003 21:25:48 +0000, Mairhtin O'Feannag wrote:
> 
>> I want to search all the files on my computer for the occurance of a
>> particular string.  Before you blast me, I know, this seems silly.  But I
>> have a reason.
>> 
>> I can use find ./ -name * to list all files, but how do I send them
>> through a "grep" to find any occurance of, say, "poohead" within the
>> files ????
> 
> find / -type f -exec grep -l poohead {} +

on my system you need to do:

find / -type f -exec grep -l poohead '{}' ';'

{} means the found file, ; is the delimiter for find -exec, and they both
need quoting.

andy.

-- 
remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
bin unless notified with [html] or [attachment] in the subject line.
0
news21 (108)
10/8/2003 10:07:30 PM
On Wed, 08 Oct 2003 16:50:13 -0500, Dave Uhring <daveuhring@yahoo.com> wrote:
> 
> 
> On Wed, 08 Oct 2003 21:25:48 +0000, Mairhtin O'Feannag wrote:
> 
>> I want to search all the files on my computer for the occurance of a 
>> particular string.  Before you blast me, I know, this seems silly.  But I 
>> have a reason.
>> 
>> I can use find ./ -name * to list all files, but how do I send them through 
>> a "grep" to find any occurance of, say, "poohead" within the files ????
> 
> find / -type f -exec grep -l poohead {} +
> 

Why the  + ?

The only possibly relevant reference to + in the find man page is:

-perm +mode
              Any of the permission bits mode  are  set  for  the
              file.

And it doesn't seem to be part of a regex for grep.... 


-- 
Later, Alan C
You can find my email address at the website: contact.html
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
0
zzzzzz (1966)
10/8/2003 10:19:12 PM
On Wed, 08 Oct 2003 22:19:12 +0000, Alan Connor wrote:

> Why the  + ?
> 
> The only possibly relevant reference to + in the find man page is:
> 
> -perm +mode
>               Any of the permission bits mode  are  set  for  the
>               file.
> 
> And it doesn't seem to be part of a regex for grep....

It came from Solaris.  I posted the wrong command ;-)

0
daveuhring (1185)
10/8/2003 10:33:56 PM
On Wed, 08 Oct 2003 17:33:56 -0500, Dave Uhring <daveuhring@yahoo.com> wrote:
> 
> 
> On Wed, 08 Oct 2003 22:19:12 +0000, Alan Connor wrote:
> 
>> Why the  + ?
>> 
>> The only possibly relevant reference to + in the find man page is:
>> 
>> -perm +mode
>>               Any of the permission bits mode  are  set  for  the
>>               file.
>> 
>> And it doesn't seem to be part of a regex for grep....
> 
> It came from Solaris.  I posted the wrong command ;-)
> 

<chuckle>

Thanks.

-- 
Later, Alan C
You can find my email address at the website: contact.html
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
0
zzzzzz (1966)
10/8/2003 10:59:18 PM
On Wed, 08 Oct 2003 22:59:18 +0000, Alan Connor wrote:

> On Wed, 08 Oct 2003 17:33:56 -0500, Dave Uhring <daveuhring@yahoo.com> wrote:

>> It came from Solaris.  I posted the wrong command ;-)
>> 
> 
> <chuckle>
> 
> Thanks.

Solaris find(1):

     -exec command
           True if the executed command returns a zero  value  as
           exit  status. The end of command must be punctuated by
           an escaped semicolon (;). A  command  argument  {}  is
           replaced  by  the current path name. If the last argu-
           ment to -exec is {} and you specify + rather than  the
           semicolon  (;),  the  command  will  be  invoked fewer
           times, with {} replaced by groups of pathnames.

Makes the search run quicker.

0
daveuhring (1185)
10/8/2003 11:25:33 PM
=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2003-10-08T22:06:41Z, Paul Lutus <nospam@nosite.zzz> writes:

> $ find . | xargs grep pattern

I second this.  It will be a huge win over the "find . -exec grep" syntax in
that it will spawn a vastly smaller number grep processes over the duration
of the run.
=2D --=20
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/hKQo5sRg+Y0CpvERAurgAJ0XB8pDWsTT6Etuwfy9lyhRDAHkTwCfRLI4
spgHGqo4FzL+7uRGeWML+nk=3D
=3DqPBI
=2D----END PGP SIGNATURE-----
0
kirk (233)
10/9/2003 12:00:19 AM
Mairhtin O'Feannag wrote:
> I can use find ./ -name * to list all files, but how do I send them through 
> a "grep" to find any occurance of, say, "poohead" within the files ????

$ find . -print0 | xargs -0 grep

The "-print0" arg to find and "-0" arg to xargs deal with files with
embedded newlines.

For more info on what xargs does, 
$ man xargs 

Ed

0
news20 (49)
10/9/2003 12:45:25 AM
On Thu, 09 Oct 2003 00:00:19 GMT, Kirk Strauser <kirk@strauser.com> wrote:
> 
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> At 2003-10-08T22:06:41Z, Paul Lutus <nospam@nosite.zzz> writes:
> 
>> $ find . | xargs grep pattern
> 
> I second this.  It will be a huge win over the "find . -exec grep" syntax in
> that it will spawn a vastly smaller number grep processes over the duration
> of the run.
> - -- 
> Kirk Strauser
> The Strauser Group
> Open. Solutions. Simple.
> http://www.strausergroup.com/
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.3 (GNU/Linux)
> 
> iD8DBQE/hKQo5sRg+Y0CpvERAurgAJ0XB8pDWsTT6Etuwfy9lyhRDAHkTwCfRLI4
> spgHGqo4FzL+7uRGeWML+nk=
> =qPBI
> -----END PGP SIGNATURE-----


I like the xargs solution, but why do you have to get in our faces with the
PGP crap?

Do you have any idea how FEW people use it, and how FEW people give a
tinker's damn whether you are posting a message with a key registered to
the name Kirk Strauser?

You real name could be Gertrude Hoskenblatt, so what the hell is the point?

If you were communicating with your bank on a sensitive matter involving
patents and copyrights, I could see using PGP.

But HERE, we only care about the quality of our posts, and part of that
quality involves whether you clutter them with useless garbage or not.

It's nothing but a big, pretentious ego-trip.

(if it wasn't, you would put the info in your headers and refer to it in
a short line in your sig, like people with manners do)

Guess what? It is no harder to install PGP than it is to install a frigging
text editor, so I cannot imagine who the hell you think you are impressing.

You have a BIZARRE idea of what constitutes "Solutions" that are "Simple".

If I see the stupid PGP sig again, you are killfiled for 30 days.

(Or are you going to tell me that I have to install a different newsreader
that hides PGP sigs so that you can get some kind of neurotic satisfaction
from chronically advertising the fact that you use a program that most
people don't?)


-- 
Later, Alan C
You can find my email address at the website: contact.html
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
0
zzzzzz (1966)
10/9/2003 12:59:19 AM
On Wed, 08 Oct 2003 18:25:33 -0500, Dave Uhring <daveuhring@yahoo.com> wrote:
> 
> 
> On Wed, 08 Oct 2003 22:59:18 +0000, Alan Connor wrote:
> 
>> On Wed, 08 Oct 2003 17:33:56 -0500, Dave Uhring <daveuhring@yahoo.com> wrote:
> 
>>> It came from Solaris.  I posted the wrong command ;-)
>>> 
>> 
>> <chuckle>
>> 
>> Thanks.
> 
> Solaris find(1):
> 
>      -exec command
>            True if the executed command returns a zero  value  as
>            exit  status. The end of command must be punctuated by
>            an escaped semicolon (;). A  command  argument  {}  is
>            replaced  by  the current path name. If the last argu-
>            ment to -exec is {} and you specify + rather than  the
>            semicolon  (;),  the  command  will  be  invoked fewer
>            times, with {} replaced by groups of pathnames.
> 
> Makes the search run quicker.
> 

Ahh.

-- 
Later, Alan C
You can find my email address at the website: contact.html
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
0
zzzzzz (1966)
10/9/2003 12:59:20 AM
On Wed, 08 Oct 2003 21:59:12 GMT, Alan Connor <zzzzzz@xxx.yyy> wrote:
> On Wed, 08 Oct 2003 21:25:48 GMT, Mairhtin O'Feannag <irishboyca@rocketmail.com> wrote:
>> 
>> 
>> I want to search all the files on my computer for the occurance of a 
>> particular string.  Before you blast me, I know, this seems silly.  But I 
>> have a reason.
>> 
>> I can use find ./ -name * to list all files, but how do I send them through 
>> a "grep" to find any occurance of, say, "poohead" within the files ????
>> 
>> 
>> Mairhtin O'Feannag
>> 
> 
> If you have GNU grep, you can do:
> 
> grep -rs  'poolhead'  /   # the r means recursive and the s silences error
>                           # messages 
> 
> If not, then try something like this:
> 
> find /  -type f  -exec  grep 'poolhead' {} \;
> 
> The {} refers to whatever file find is currently focussing on.
> 
> 

I usually do it like this

find / -type f -exec grep 'poolhead' /dev/null \{\} \;

having two files in the arguments for grep will make grep print
out the file name it finds the string in.  Of course it'll never
find it in /dev/null  :)


0
creideiki (13)
10/9/2003 1:59:22 AM
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alan Connor wrote:
> I like the xargs solution, but why do you have to get in our faces with the
> PGP crap?
> 
> Do you have any idea how FEW people use it, and how FEW people give a
> tinker's damn whether you are posting a message with a key registered to
> the name Kirk Strauser?
> 
> You real name could be Gertrude Hoskenblatt, so what the hell is the point?
> 
> If you were communicating with your bank on a sensitive matter involving
> patents and copyrights, I could see using PGP.
> 
> But HERE, we only care about the quality of our posts, and part of that
> quality involves whether you clutter them with useless garbage or not.
> 
> It's nothing but a big, pretentious ego-trip.
> 
> (if it wasn't, you would put the info in your headers and refer to it in
> a short line in your sig, like people with manners do)
> 
> Guess what? It is no harder to install PGP than it is to install a frigging
> text editor, so I cannot imagine who the hell you think you are impressing.
> 
> You have a BIZARRE idea of what constitutes "Solutions" that are "Simple".
> 
> If I see the stupid PGP sig again, you are killfiled for 30 days.
> 
> (Or are you going to tell me that I have to install a different newsreader
> that hides PGP sigs so that you can get some kind of neurotic satisfaction
> from chronically advertising the fact that you use a program that most
> people don't?)

Nope, but do find a newsreader with decent filtering capabilities, and have it chuck out any message 
that begins with "BEGIN PGP SIGNED MESSAGE" in the first couple of lines.

I think that will make everyone happier :)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/hNMHeS99pGMif6wRAixfAJwJOmmP3qPmbfo5Bbp00g2yAniG+gCg1sxD
kSIFM+qiKQqgjHx6wzBmCHM=
=Rv4v
-----END PGP SIGNATURE-----

0
spam405 (14)
10/9/2003 3:16:17 AM
On Thu, 09 Oct 2003 00:59:19 +0000, Alan Connor wrote:

> (Or are you going to tell me that I have to install a different newsreader
> that hides PGP sigs so that you can get some kind of neurotic satisfaction
> from chronically advertising the fact that you use a program that most
> people don't?)

Like you do with elrav1.  But you've killfiled me forever (didn't you once
claim that you never do that?); I'm writing this solely for the benefit of
the others reading the thread.

0
emurphy42 (1226)
10/9/2003 4:58:01 AM
=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2003-10-09T01:59:22Z, Creideiki <creideiki@cfl.rr.com> writes:

> I usually do it like this
>
> find / -type f -exec grep 'poolhead' /dev/null \{\} \;
>
> having two files in the arguments for grep will make grep print out the
> file name it finds the string in.  Of course it'll never find it in
> /dev/null :)

So will the '-l' option to grep:

    $ grep -l foo test.mail
    test.mail

And in any case you really want to use xargs instead of -exec .
=2D --=20
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/hUp95sRg+Y0CpvERAo7WAJwPfGEk2rPQSq7NqtGE/ZFSF/x8aACePF3q
4nCxH0nDdyfvGmyXETMpUEA=3D
=3DWqZe
=2D----END PGP SIGNATURE-----
0
kirk (233)
10/9/2003 11:50:06 AM
[This followup was posted to comp.os.linux.misc]

In article <Xns940E92E72FD06mairhtinofeannag@64.164.98.49>, 
irishboyca@rocketmail.com says...
> I want to search all the files on my computer for the occurance of a 
> particular string.  Before you blast me, I know, this seems silly.  But I 
> have a reason.
> 
> I can use find ./ -name * to list all files, but how do I send them through 
> a "grep" to find any occurance of, say, "poohead" within the files ????
> 
> 
> Mairhtin O'Feannag

find ./ -name '*' -exec fgrep poolhead /dev/null '{}' \;
0
raisin3 (35)
10/9/2003 1:18:41 PM
That would be "poohead", not "poolhead".   Giggle

It's interesting to me STILL that flame wars can occur in such a 
simple/direct thread.  :)

I used the xargs solution.  Seems to work.  I've come across it before, 
and had simply forgotten it.  Have to write these things down, now that 
I'm old and feeble (NOT!).  

Thanks for all the prompt and helpful responses folks!!!!

M


Barry Kimelman <raisin@delete-this-trash.mts.net> wrote in
news:MPG.19ef1c266d8eeca898969b@news.mts.net: 

> [This followup was posted to comp.os.linux.misc]
> 
> In article <Xns940E92E72FD06mairhtinofeannag@64.164.98.49>, 
> irishboyca@rocketmail.com says...
>> I want to search all the files on my computer for the occurance of a 
>> particular string.  Before you blast me, I know, this seems silly. 
>> But I have a reason.
>> 
>> I can use find ./ -name * to list all files, but how do I send them
>> through a "grep" to find any occurance of, say, "poohead" within the
>> files ???? 
>> 
>> 
>> Mairhtin O'Feannag
> 
> find ./ -name '*' -exec fgrep poolhead /dev/null '{}' \;
> 

0
irishboyca (42)
10/9/2003 4:57:00 PM
On Wed, 08 Oct 2003 21:25:48 +0000, Mairhtin O'Feannag wrote:

> I want to search all the files on my computer for the occurance of a
> particular string.  Before you blast me, I know, this seems silly.  But I
> have a reason.
> 
> I can use find ./ -name * to list all files, but how do I send them
> through a "grep" to find any occurance of, say, "poohead" within the files
> ????
> 
> 
> Mairhtin O'Feannag
grep -R poohead ./*
0
10/9/2003 7:12:45 PM
On Thu, 09 Oct 2003 11:50:06 GMT, Kirk Strauser <kirk@strauser.com> wrote:
> At 2003-10-09T01:59:22Z, Creideiki <creideiki@cfl.rr.com> writes:
> 
>> I usually do it like this
>>
>> find / -type f -exec grep 'poolhead' /dev/null \{\} \;
>>
>> having two files in the arguments for grep will make grep print out the
>> file name it finds the string in.  Of course it'll never find it in
>> /dev/null :)
> 
> So will the '-l' option to grep:
> 
>     $ grep -l foo test.mail
>     test.mail
> 
> And in any case you really want to use xargs instead of -exec .

Yeah, I hadn't known of xargs before now.

But what I was doing with the double file name was make the
find -exec grep act like grep with globbing, where it prints
the name of the file it finds the regular expression *and* prints
the line that matched it.  -l only prints the name.


     $ grep  foo /dev/null test.mail
     test.mail:someline with the string 'foo' in it


0
creideiki (13)
10/9/2003 7:45:18 PM
=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2003-10-09T19:45:18Z, Creideiki <creideiki@cfl.rr.com> writes:

> But what I was doing with the double file name was make the find -exec
> grep act like grep with globbing, where it prints the name of the file it
> finds the regular expression *and* prints the line that matched it.  -l
> only prints the name.

Oh!  Like the "-H" flag to GNU grep:

    $ grep -H tells wi.4
    wi.4:This tells the pccard system to use PCI interrupts for this odd be=
ast.

=2D --=20
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/hb2i5sRg+Y0CpvERAnvsAJ9QSJ2JB9BYCBPwjpwQzssZwHIB5ACgkdLk
H0kl6jpgFiSNNh8d64+UipQ=3D
=3D80E/
=2D----END PGP SIGNATURE-----
0
kirk (233)
10/9/2003 8:00:07 PM
> I want to search all the files on my computer for the occurance of a 
> particular string.  

The use of `exec` is one way to do this, but another is this:

find / -type f | xargs grep something

Xargs is one of the *very* cool commands that isn't used as much as it
should be (see lsof).  I am just learning it myself actually.  The
thing about xargs is that the command that comes after it is treated
as if it's being piped to.  So xargs builds a list of input from what
came before it, and then does the command that comes after it for each
item in that list.

You could, for example, find all files owned by a certain person, and
copy them to a directory, etc.

Enjoy.

-danielrm26
0
10/10/2003 1:22:32 AM
Reply: