bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: grep -r --exclude problem


From: Bob Proulx
Subject: Re: grep -r --exclude problem
Date: Thu, 1 Jun 2006 08:47:54 -0600
User-agent: Mutt/1.5.9i

Com MN PG P E B Consultant 3 wrote:
> I would like to do a grep -r, but exclude all files with
> extensions .bak, .~, .sav. I tried a glob pattern
> 
>   grep -r --exclude='*.{bak,~,sav}' ...
> ...
> But I think it would be a good idea if grep would include
> bash-style, {...}-globbing, into its include/exclude options,
> because denoting all the pattern combination via multiple --exclude
> soon gets tiresome.

My personal opinion is that including directory traversal (file
finding) operations into GNU grep was a bad idea.  It is a slippery
slope because more and more 'find' like operations will be asked for
in grep and by implication in every command line utility.  It is much
better to use 'find' for finding files because then the syntax is
consistent across all of the command line utilities.

Here is a portable way to use find to do what you want.

  find . \( -name '*.bak' -o -name '*.sav' -o -name '*~' \) -prune -o -print \
    | xargs grep PATTERN

You might still consider it tiresome to enter them all the same as in
grep though.  In which case you would use the GNU find -regex option.
And also find -print0 and xargs -r0 implement zero terminated strings
to handle filenames with newlines and other funny characters.

  find . -regex '.*.\(bak\|sav\|~\)$'  -prune -o -print0 \
    | xargs -r0 grep PATTERN

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]