From: Ben Bacarisse 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

In recent versions of bash you can use negated glob patterns:

!(pattern list)

Make sure extglob is turned on using shopt.

--
Ben.
From: News123 on
Ben Finney wrote:
> 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 only files and not directories under a certain path

find . -type f

I would -type f to all following lines

> 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.
>
N
From: bsh on
On May 10, 9:38 pm, Melvin <whereismel...(a)gmail.com> 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
>
> Thanks
> Unix baby

Notwithstanding the aforementioned GNU grep(1) "--exclude" option, I
should at least mention that in addition to the find(1) process to
construct a custom path list, that in modern shells the special
variable FIGNORE -- which should contain an colon-separated
list of extended filename patterns to specify excluded components
of filename generations (like "*") -- will accomplish what you want
without further ado.

It can be as simple as (not tested):

FIGNORE=your_excluded_file

=Brian
From: Stephane CHAZELAS on
2010-05-13, 13:53(-07), bsh:
[...]
> Notwithstanding the aforementioned GNU grep(1) "--exclude" option, I
> should at least mention that in addition to the find(1) process to
> construct a custom path list, that in modern shells the special
> variable FIGNORE -- which should contain an colon-separated
> list of extended filename patterns to specify excluded components
> of filename generations (like "*") -- will accomplish what you want
> without further ado.
>
> It can be as simple as (not tested):
>
> FIGNORE=your_excluded_file
[...]

Only in AT&T ksh93. And it's quite dangerous there as it gets it from
the environment:

$ FIGNORE='!(etc)' ksh -c 'echo /*'
/etc

In other shells (bash and zsh), FIGNORE is used for filename
completion only and is a list of suffixes (can be patterns in
zsh) just like csh's fignore. And it's also inherited from the
environment.

bash has GLOBIGNORE that works like AT&T ksh FIGNORE (without
the issue about the environment).

In zsh, as already mentionned you've got the "and not" globbing
operator:

setopt extendedglob
echo *.txt~*foo*

would return the .txt files as long as their name doesn't
contain "foo".

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