Re: Another fgrep query
From: Tapani Tarvainen (tt+gn20030730T130537_at_it.jyu.fi)
Date: 07/30/03
- Next message: Arijit Mukherjee: "Editing String in std-in"
- Previous message: Stein Arne Storslett: "Re: pls need some guidance"
- In reply to: Dave Hedgehog: "Another fgrep query"
- Next in thread: Ed Morton: "Re: Another fgrep query"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Arijit Mukherjee: "Editing String in std-in"
- Previous message: Stein Arne Storslett: "Re: pls need some guidance"
- In reply to: Dave Hedgehog: "Another fgrep query"
- Next in thread: Ed Morton: "Re: Another fgrep query"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|