patchgrep - Patches: patch #3521, Add options to exclude/include...


You are not allowed to post comments on this tracker with your current authentication level.

patch #3521: Add options to exclude/include directories

Submitted by:  Eric Haszlakiewicz <erh>
Submitted on:  Mon 15 Nov 2004 09:06:36 PM UTC  
Votes:  51  
Category: NonePriority: 5 - Normal
Status: Wont DoPrivacy: Public
Assigned to: NoneOpen/Closed: Closed

(Jump to the original submission Jump to the original submission)

Sun 05 Oct 2008 10:42:37 PM UTC, comment #9:

The latest release, grep-2.5.3, introduced the long awaited option- FTW.

directories that match PATTERN will be skipped.

Doug <dougw>
Fri 10 Feb 2006 02:31:53 PM UTC, comment #8:

Wow, I'm really disappointed by this decision. It seems like it's pretty capricious and based more on tradition and hacker elitism.

- Just use a find pipe. It's more powerful. Just remember all six options, to escape the special shell characters, and don't forget xargs for performance.
- User-friendliness is overrated. I know a programmer who uses version 1.0 of Korn shell and turns his nose up at zsh and bash. Tab completion is for sissies.
- We added -r due to user demand, but dammit we're sick and tired of giving users what they want. --exclude-dirs is out of the question. And the maintainers of diff are idiots for supporting such a thing.

I hope you get my point. This feature can be added so that it doesn't break backwards compatibility. It's really useful for the majority of people who don't eat and breathe find/xargs. Plus there's at least one other person who was motivated enough to file a bug report: see #11017.

Seriously: *why* don't you want to go down that path? Because something more difficult already exists that can do the job? That doesn't seem like a good reason to me, given that the cost of adding the feature is so low.

David Coppit <coppit>
Thu 10 Nov 2005 07:03:16 PM UTC, comment #7:

This will be a nice solution to exclude .svn directories from the grep matches too

find . -type f \! -path '.svn/' -exec grep -H <pattern> {} \;


Rick van der Zwet <rickzwet>
Wed 26 Jan 2005 02:50:43 PM UTC, comment #6:

I meant I'd use

grep -r --exclude .svn test .

once the bug #11017 is fixed.

Stepan Kasal <kasal>
Project Member
Wed 26 Jan 2005 02:49:08 PM UTC, comment #5:

Actually, for scripts, shell functions and aliases, I find the find|xargs solution much cleaner.

For interactive command line usage, I'd use

grep -r --exclude .svn test .

You don't have a regular file named .svn, do you?

Stepan Kasal <kasal>
Project Member
Mon 17 Jan 2005 09:11:20 PM UTC, comment #4:

So it is your opinion that:

find . -type d -name .svn -prune -false -o -type f -print0 | xargs -0r grep --color -H test

Is better than
grep --exclude-dir .svn test *

I find it a bit more tedious to type. Yes, we could use aliases. But why chain aliases to find to xargs to grep, when it is a simple functionality that grep could (and should, if it can exclude files, which it should if it can recursive search, which it should) do on it's own quite easily.

Tue 21 Dec 2004 01:06:32 PM UTC, comment #3:

To handle newlines correctly, use the action -print0 of find, like this:

find . -type d -name .svn -prune -o type f -print0 | xargs -0r grep -H foo

I believe find/xargs is the general and we should not try to emulate that withing grep --recursive.

grep -r should be used for the most common cases only.

Have a nice day,

Stepan Kasal <kasal>
Project Member
Tue 21 Dec 2004 12:01:17 PM UTC, comment #2:

$ grep --exclude-dir .svn foo .

I'm trying to mimick above with find. This comes close to above statement (grep -v .svn is not as excact as above):

$ find . -type f | grep -v '\.svn' | tr '\n' '\0' | xargs -0 grep foo

or your suggestion (preventing find to recurse directories which are thrown away by grep -v):

find . -type d -name .svn -prune -false -o -type f | tr '\n' '\0' | xargs -0 grep foo

Can you advise how to do this simpler without breaking on spaces in file names and directory names?

I used find/xargs for such searching until I recently discovered the merits of grep -r and find it very useful.

Tue 16 Nov 2004 09:37:07 AM UTC, comment #1:

I'm sorry, but I don't want to accept this.

Let me give a bit longer explanation:

Traditionally, recursive search was performed as:

find . -type f|grep pattern

The advantage of this approach is the unlimited versatility of this pipe; you can use all of the predicates of ``find'' to construct any condition you need.

For example, you can use

find . -type f -name '*.c' | grep pattern


find . -type f '!' -name '*.c' | grep pattern

To include or exclude files.

In particular, you can use

find . -type d -name .svn -prune -false -o -type f | grep pattern

to skip all directories named ``.svn''.
I admit this sounds complicated at first if you have never studied Prolog, but you can get into it. And then the possibilities are unlimited.

From this traditional point of view, the --recursive option of grep was always questioned. I know at least one hacker who deliberately ignores this option and uses find.

A short illustration of this position: does

grep --include='.c' --exclude='core'

search file core.c or not? If you use find, you never ask this question.

OTOH, the popularity of the ``rgrep'' tool has proved that there is market for quick recursive search; I guess this was why the -r option was introduced into grep.

The next natural step was introducing --exclude and --include. --exclude-dir would be the next natural step, I agree.

But I don't want to go long that path. We have to draw a line somewhere. And I decided to draw the line now and reject your patch. Sorry.

But I think that the ``simple recursive search'' should be as intuitive as possible. Ithink that one naturally tries --exclude=.svn first: and I propose that it should exclude directories too. I created bug #11017 to remember me.

On the contrary, --include doesn't apply to directory names, so --include='*.c' searches the whole subtree for C files.

I hope you won't mind too much.

Stepan Kasal

Stepan Kasal <kasal>
Project Member
Mon 15 Nov 2004 09:06:36 PM UTC, original submission:

This patch adds --exclude-dir and --include-dir options, similar to the --exclude and --include options except that they operate on directories instead of files.

Example usage:
grep --exclude-dir .svn foo .

Eric Haszlakiewicz <erh>


Attached Files
file #8046:  grep.diff added by erh (7KiB - application/octet-stream - Code and doc patch to add --exclude-dir and --include-dir options.)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by dougw (Posted a comment)

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 51 encouragements so far.

    Only logged-in users can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 5 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 06 Oct 2006 06:00:08 PM UTCarobertCarbon-Copy-=>Added arobert
    Fri 10 Feb 2006 02:34:08 PM UTCcoppitCarbon-Copy-=>Added coppit
    Wed 26 Jan 2005 02:53:19 PM UTCkasalStatusNone=>Wont Do
    Tue 16 Nov 2004 09:37:07 AM UTCkasalOpen/ClosedOpen=>Closed
    Mon 15 Nov 2004 09:06:36 PM UTCerhAttached File-=>Added grep.diff, #3882

    Back to the top

    Powered by Savane 3.1-cleanup1