Re: Another fgrep query

From: Tapani Tarvainen (tt+gn20030730T130537_at_it.jyu.fi)
Date: 07/30/03


Date: 30 Jul 2003 14:43:41 +0300


"Dave Hedgehog" <x@x.com> writes:

> I have a directory which contains loads (probably thousands) of different
> files.
> I want to search for a string but when I try an fgrep it bombs out because
> the 'arg list is too long'.
> I know roughly what date the data I am looking for was saved so is there a
> way I can limit my fgrep to only search files which were saved on a certain
> day.
> I'm sure there probably is but I don't have the knowledge to do it.

First, you can avoid the 'arg list too long' problem with xargs:

ls | xargs fgrep 'string' /dev/null

That will fail if there are spaces in filenames, though.
Alternatively you can do

ls | while read f; do fgrep 'string' /dev/null "$f" ;done

That can handle spaces in filenames (not newlines though),
but it is very slow. The /dev/null is needed to force
printing the filename (not needed if you use -l option).

Also, both of those will check all files and thus waste time
compared to selecting files by timestamp first.

Probably easiest way to select files by age is find.
E.g., assuming you want to search only files that are
older than 7 days but younger than 14 days, you can do

find . \( -type f -mtime +14 -print \) -o ! -name . -prune |
 xargs fgrep 'string' /dev/null

If there are no subdirectories you can simplify that:

find . -type f -mtime +7 -mtime -14 | xargs fgrep 'string' /dev/null

The "-type f" is just to prevent grepping ".".

These also fail if there are spaces in filenames; if that
is possible, try

find . -type f -mtime +7 -mtime -14 -exec fgrep 'string' /dev/null {} \;

slower though that is, or if you have a Gnu system,

find . -type f -mtime +7 -mtime -14 -print0 |
 xargs -0 fgrep 'string' /dev/null

-- 
Tapani Tarvainen


Relevant Pages

  • Re: Security implications of using open() on untrusted strings.
    ... Jorgen Grahn wrote: ... regexery however I would like to check filenames and I would like this ... Although it's a desktop app I'm a little squeamish ... Compare with a language where if the string ...
    (comp.lang.python)
  • Re: Extracting patterned filenames from [glob] without a loop - possible?
    ... > obtain the correct pattern to make it work. ... A couple obvious problems relate to the fact you are using string ... This is relatively safe since lists can easily be converted ... filenames without 'horrifcally clogging' a system. ...
    (comp.lang.tcl)
  • Re: Security implications of using open() on untrusted strings.
    ... regexery however I would like to check filenames and I would like this ... Although it's a desktop app I'm a little squeamish ... piping raw user input into a filesystem function like that and this app ... Compare with a language where if the string ...
    (comp.lang.python)
  • Re: How can I make a copy of my collection?
    ... Public Class FileNames ... Public fullName As String ... This app is simply to help me try to conform some of the files names ... And then even sub folders for file types. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to determine which process to kill ???
    ... >the filenames that were used, like, in a string array or something: ... >Private Function createTempFile() As String ... >some system related process or Windows Service or your AntiVirus agent. ...
    (microsoft.public.dotnet.languages.vb)