From: Melvin on
Hi,

Is there a way in which a particular file can be excluded from grep
operations. Say, a directory has 15 files/directories and I want to
using => grep -r "pattern" *
Can I exclude a single file from this

Thanks
Unix baby
From: Stephane CHAZELAS on
2010-05-10, 21:38(-07), Melvin:
> Hi,
>
> Is there a way in which a particular file can be excluded from grep
> operations. Say, a directory has 15 files/directories and I want to
> using => grep -r "pattern" *
> Can I exclude a single file from this
[...]

find . -type f ! -path ./this/file -exec \
grep pattern /dev/null {} +


Or with zsh

setopt extendedglob
grep pattern **/*~this/file(D.)

(D is to include hidden (dot) files, . is to only include
regular files).


--
Stéphane
From: Ben Finney on
Melvin <whereismelvin(a)gmail.com> writes:

> Is there a way in which a particular file can be excluded from grep
> operations. Say, a directory has 15 files/directories and I want to
> using => grep -r "pattern" *
> Can I exclude a single file from this

Much simpler to construct the list of files how you want, and give that
list to 'grep'.

List each file name one per line:

find .

List files, one per line, found whose path exactly matches one name:

find . -name './foo/bar/unwantedfile'

List files, one per line, whose path does *not* match one name:

find . -not -name './foo/bar/unwantedfile'

Use the output of the above command as part of the grep command line:

grep "pattern" $(find . -not -name './foo/bar/unwantedfile')

Alternatively, if the list of filenames will be quite long, use 'xargs'
to invoke 'grep' on smaller chunks of the list:

find . -not -name './foo/bar/unwantedfile' | xargs grep "pattern"

If you have files whose names may contain characters special in the
shell, you'll need to delimit the list with null characters instead:

find . -not -name './foo/bar/unwantedfile' -print0 | xargs --null grep "pattern"

In fact, when making such a command-line, one should default to allowing
for filenames containing special characters unless there is a good
reason not to do so.

--
\ “When I get new information, I change my position. What, sir, |
`\ do you do with new information?” —John Maynard Keynes |
_o__) |
Ben Finney
From: Ben Finney on
Ben Finney <ben+unix(a)benfinney.id.au> writes:

> find . -name './foo/bar/unwantedfile'
> find . -not -name './foo/bar/unwantedfile'

My mistake; the '-name' option (matching only the basename of the entry)
in all these command won't work, it needs to be '-path'.

--
\ “I have never imputed to Nature a purpose or a goal, or |
`\ anything that could be understood as anthropomorphic.” —Albert |
_o__) Einstein, unsent letter, 1955 |
Ben Finney
From: pk on
Melvin wrote:

> Hi,
>
> Is there a way in which a particular file can be excluded from grep
> operations. Say, a directory has 15 files/directories and I want to
> using => grep -r "pattern" *
> Can I exclude a single file from this

IIRC GNU grep has some --exclude options that should do that.

 |  Next  |  Last
Pages: 1 2
Prev: Why it does not work?
Next: su - user question