Re: find and searching for specific expression in files

2009-05-31 Thread 郑天宇
On Sat, May 30, 2009 at 11:25:12AM +0200, Zbigniew Szalbot wrote:
> Hello,
> 
> Can you please give me a hint how to use find to search for a specific
> text within files?

If you just want the filenames which contain the string you are
searching for, use the "-l" argument to tell grep to print out the
filenames.

For example:

grep -rl "string" .

will print out all the filenames containning "string".


> 
> 
> -- 
> Zbigniew Szalbot
> 
> ___
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"

 郑天宇
2009年05月31日
-- 
zheng.org


pgpN3TtCeoBit.pgp
Description: PGP signature


Re: find and searching for specific expression in files

2009-05-30 Thread Karl Vogel
>> On Sat, 30 May 2009 11:25:12 +0200, 
>> "Zbigniew Szalbot"  said:

Z> Can you please give me a hint how to use find to search for a specific
Z> text within files?

   People have mentioned using xargs in combination with find, but if
   you're dealing with Windows files on a server, be prepared for every
   kind of crap character in the filename you can imagine.  Use nulls
   to delimit the filenames, i.e.:

  find . -mtime +7 -print0 | xargs -0 grep -i foo

   The GNU versions of find and xargs support the "0" options as well.

-- 
Karl Vogel  I don't speak for the USAF or my company

iPod changed my life.
Earbuds made me look so cool!
Now I am stone deaf.  --geek haiku
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Wojciech Puchar

 | while read x; do  "$x"; done

which should get around the list length limitations and
provides for doing "extras" between the "do" and the "done".
Specifically:

find /path/to/files/ -mtime -2 -print | \
while read x; do grep "expression" "$x"; done


same as -exec

works but forks a process for each single file - slow

-exec and + or xargs do the job
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Walt Pawley
At 6:44 PM +0200 5/30/09, Wojciech Puchar wrote:
>> the famous back-tics.
>>
>>  % grep "expression" `find /path/to/files/ -mtime -2 -print`
>>
>> Of course, there are surely easier, faster and better means,
>> but from this one, I know it just works. :-) Furthermore, I
>
>unless filelist exceed max lenght of arguments and unfortunately it
>happens often

I use bash as my default shell and have become rather enamored
with the construct

 | while read x; do  "$x"; done

which should get around the list length limitations and
provides for doing "extras" between the "do" and the "done".
Specifically:

find /path/to/files/ -mtime -2 -print | \
while read x; do grep "expression" "$x"; done
-- 

Walter M. Pawley 
Wump Research & Company
676 River Bend Road, Roseburg, OR 97471
 541-672-8975
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Wojciech Puchar

the famous back-tics.

% grep "expression" `find /path/to/files/ -mtime -2 -print`

Of course, there are surely easier, faster and better means,
but from this one, I know it just works. :-) Furthermore, I


unless filelist exceed max lenght of arguments and unfortunately it 
happens often

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Polytropon
On Sat, 30 May 2009 11:25:12 +0200, "Zbigniew Szalbot"  
wrote:
> Hello,
> 
> Can you please give me a hint how to use find to search for a specific
> text within files?

One valid solution is to combine find (to find the files) and
grep (to search in them). For the combination, you can use
the famous back-tics.

% grep "expression" `find /path/to/files/ -mtime -2 -print`

Of course, there are surely easier, faster and better means,
but from this one, I know it just works. :-) Furthermore, I
think -print is optional here.

If you want to use the Midnight Commander, use Meta-? for a
combined dialog:

+- Find File --+
|  |
| Start at: ___[^] |
|  |
| Filename: ___[^] |
|  |
| Content:  ___[^] |
|  |
| [ ] case Sensitive   |
|  |
| [< OK >]  [ Tree ][ Cancel ] |
+--+

That's what I mostly use.




-- 
Polytropon
>From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Mel Flynn
On Saturday 30 May 2009 18:14:49 RW wrote:
> On Sat, 30 May 2009 14:12:50 +0200
>
> Mel Flynn  wrote:
> > On Saturday 30 May 2009 13:56:22 Valentin Bud wrote:
> > > 2009/5/30 Zbigniew Szalbot 
> > >
> > >  You can use egrep -r * (grep -e) to search for specific text
> > > pattern while you are in a directory with many sub directories. The
> > > output is nice because it tells you the file in which the text
> > > pattern was found :).
> >
> > Discouraged because:
> > - it's possible to hit maxarglen if the root directory has many
> > subdirectories.
> > - Will not search hidden directories in the root directory because of
> > the shell glob
>
> You can replace "egrep -r  *" with "egrep -r  ."
> i.e. recurse from the current directory, rather than search or recurse
> on everything that matches *. That avoids the first two problems, and
> most of the time the third doesn't matter

OP (and myself) have a different concept of 'most of the time'. But this may 
be cause I'm already so used to this concept that my fingers have it store 
locally and I could've used grep -r or the overall win is minimal (I often use 
-name '*.h', and arguably in small trees it wouldn't matter).

>
> > - cannot be combined with other search criteria such as the file's
> > timestamp.

-- 
Mel
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Wojciech Puchar


Cut off the message a bit later and you will see that using a '+' to terminate
the exec primitive emulates xargs behavior:


thanks. i didn't know that



On Saturday 30 May 2009 14:12:50 Mel Flynn wrote:

I use + rather then ; so that one
invocation for grep is done whenever maxarglen is hit (like if you used
xargs(1)), rather then one grep per file.


--
Mel



___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread RW
On Sat, 30 May 2009 14:12:50 +0200
Mel Flynn  wrote:

> On Saturday 30 May 2009 13:56:22 Valentin Bud wrote:
> > 2009/5/30 Zbigniew Szalbot 

> >  You can use egrep -r * (grep -e) to search for specific text
> > pattern while you are in a directory with many sub directories. The
> > output is nice because it tells you the file in which the text
> > pattern was found :).
> 
> Discouraged because:
> - it's possible to hit maxarglen if the root directory has many 
> subdirectories.
> - Will not search hidden directories in the root directory because of
> the shell glob

You can replace "egrep -r  *" with "egrep -r  ."
i.e. recurse from the current directory, rather than search or recurse
on everything that matches *. That avoids the first two problems, and
most of the time the third doesn't matter

> - cannot be combined with other search criteria such as the file's
> timestamp.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Mel Flynn
On Saturday 30 May 2009 17:57:14 Wojciech Puchar wrote:
> >> because it tells you the file in which the text pattern was found :).
> >
> > Discouraged because:
> > - it's possible to hit maxarglen if the root directory has many
> > subdirectories.
>
> xargs is usefull too. i would it as forking for each
> file will make processing really slow. xargs can cut input data into given
> chunks with -n option, so grep will be called for say 100 files at once.

Cut off the message a bit later and you will see that using a '+' to terminate 
the exec primitive emulates xargs behavior:

On Saturday 30 May 2009 14:12:50 Mel Flynn wrote:
> I use + rather then ; so that one
>invocation for grep is done whenever maxarglen is hit (like if you used
>xargs(1)), rather then one grep per file.

-- 
Mel
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Wojciech Puchar

because it tells you the file in which the text pattern was found :).


Discouraged because:
- it's possible to hit maxarglen if the root directory has many
subdirectories.


xargs is usefull too. i would it as forking for each 
file will make processing really slow. xargs can cut input data into given 
chunks with -n option, so grep will be called for say 100 files at once.


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Wojciech Puchar

I am using find in the following manner:

find /path/to/files/ -mtime -2 -ls |less
to find files which have been recently modified. But I would like to
extend the search to find specific expression within files. -name is used
to specify file name. How can I search for strings within text?


no matter how you generate the list of files to search, use grep then on 
that list

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Valentin Bud
On Sat, May 30, 2009 at 3:12 PM, Mel Flynn <
mel.flynn+fbsd.questi...@mailing.thruhere.net
> wrote:

> On Saturday 30 May 2009 13:56:22 Valentin Bud wrote:
> > 2009/5/30 Zbigniew Szalbot 
> >
> > > >> Can you please give me a hint how to use find to search for a
> specific
> > > >> text within files?
> > > >
> > > > Generally, you don't - find(1) does not examine the contents of files
> > > > by itself, just their directory information.  You normally use
> grep(1)
> > > > to search within a file.
> > >
> > > Ahhh - I use grep on daily basis. Now why didn't I think of it? I got
> so
> > > fixed on the idea of using find that I completely forgot about grep
> > >
> > > Sorry for the noise and thank you very much for your help!
> > >
> > > --
> > > Zbigniew Szalbot
> > >
> > > ___
> > > freebsd-questions@freebsd.org mailing list
> > > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > > To unsubscribe, send any mail to "
> > > freebsd-questions-unsubscr...@freebsd.org"
> >
> > Hello Mr. Zbigniew Szalbot,
> >
> >  You can use egrep -r * (grep -e) to search for specific text pattern
> while
> > you are in a directory with many sub directories. The output is nice
> > because it tells you the file in which the text pattern was found :).
>
> Discouraged because:
> - it's possible to hit maxarglen if the root directory has many
> subdirectories.


Never occured so i didn't have a clue about it :|.


>
> - Will not search hidden directories in the root directory because of the
> shell glob
> - cannot be combined with other search criteria such as the file's
> timestamp.
>
> find . -type f -mtime 2 -exec grep '^Subject: \[SPAM\]' {} +
>
> will find all messages in a maildir modified within the last 2 minutes
> where
> the subject has been flagged as spam. I use + rather then ; so that one
> invocation for grep is done whenever maxarglen is hit (like if you used
> xargs(1)), rather then one grep per file.
> --
> Mel


This list is amazing because everyday you learn something new. Thanks.

a great day,
v
-- 
network warrior since 2005
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Mel Flynn
On Saturday 30 May 2009 13:56:22 Valentin Bud wrote:
> 2009/5/30 Zbigniew Szalbot 
>
> > >> Can you please give me a hint how to use find to search for a specific
> > >> text within files?
> > >
> > > Generally, you don't - find(1) does not examine the contents of files
> > > by itself, just their directory information.  You normally use grep(1)
> > > to search within a file.
> >
> > Ahhh - I use grep on daily basis. Now why didn't I think of it? I got so
> > fixed on the idea of using find that I completely forgot about grep
> >
> > Sorry for the noise and thank you very much for your help!
> >
> > --
> > Zbigniew Szalbot
> >
> > ___
> > freebsd-questions@freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > To unsubscribe, send any mail to "
> > freebsd-questions-unsubscr...@freebsd.org"
>
> Hello Mr. Zbigniew Szalbot,
>
>  You can use egrep -r * (grep -e) to search for specific text pattern while
> you are in a directory with many sub directories. The output is nice
> because it tells you the file in which the text pattern was found :).

Discouraged because:
- it's possible to hit maxarglen if the root directory has many 
subdirectories.
- Will not search hidden directories in the root directory because of the 
shell glob
- cannot be combined with other search criteria such as the file's timestamp.

find . -type f -mtime 2 -exec grep '^Subject: \[SPAM\]' {} +

will find all messages in a maildir modified within the last 2 minutes where 
the subject has been flagged as spam. I use + rather then ; so that one 
invocation for grep is done whenever maxarglen is hit (like if you used 
xargs(1)), rather then one grep per file.
-- 
Mel
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Valentin Bud
2009/5/30 Zbigniew Szalbot 

> >> Can you please give me a hint how to use find to search for a specific
> >> text within files?
> >
> > Generally, you don't - find(1) does not examine the contents of files by
> > itself, just their directory information.  You normally use grep(1) to
> > search within a file.
>
> Ahhh - I use grep on daily basis. Now why didn't I think of it? I got so
> fixed on the idea of using find that I completely forgot about grep
>
> Sorry for the noise and thank you very much for your help!
>
> --
> Zbigniew Szalbot
>
> ___
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "
> freebsd-questions-unsubscr...@freebsd.org"
>
Hello Mr. Zbigniew Szalbot,

 You can use egrep -r * (grep -e) to search for specific text pattern while
you are in a directory with many sub directories. The output is nice because
it tells you the file in which the text pattern was found :).

a great day,
v


-- 
network warrior since 2005
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Zbigniew Szalbot
>> Can you please give me a hint how to use find to search for a specific
>> text within files?
>
> Generally, you don't - find(1) does not examine the contents of files by
> itself, just their directory information.  You normally use grep(1) to
> search within a file.

Ahhh - I use grep on daily basis. Now why didn't I think of it? I got so
fixed on the idea of using find that I completely forgot about grep

Sorry for the noise and thank you very much for your help!

-- 
Zbigniew Szalbot

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: find and searching for specific expression in files

2009-05-30 Thread Erik Trulsson
On Sat, May 30, 2009 at 11:25:12AM +0200, Zbigniew Szalbot wrote:
> Hello,
> 
> Can you please give me a hint how to use find to search for a specific
> text within files?

Generally, you don't - find(1) does not examine the contents of files by
itself, just their directory information.  You normally use grep(1) to
search within a file.

> 
> I am using find in the following manner:
> 
> find /path/to/files/ -mtime -2 -ls |less
> to find files which have been recently modified. But I would like to
> extend the search to find specific expression within files. -name is used
> to specify file name. How can I search for strings within text?
> 
> It is probably in the man but I somehow overlook it. :(
> 
> Thank you very much in advance!

I guess you could use the '-exec' expression in find(1) to execute grep(1)
to search for a string in the files examined.  Or you could use the output
of find(1) as a list of files that are given as arguments to grep(1).



-- 

Erik Trulsson
ertr1...@student.uu.se
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"