Andy Ray
Andy Ray

Reputation: 32066

How do I make ack ignore a file in a directory?

I have a file in a directory a/b/c.js

I can't ignore c.js with --ignore-file=match:c.js because I want to match other file named c.js

I can't ignore dir a/b with --ignore-dir=a/b because there are other files I care about in a/b

I've tried:

--ignore-file=a/b/c.js       "Invalid filter specification"

I've tried:

--ignore-file=match:/a\/b\/c.js/

Doesn't work, and I'm guessing that it's because ack doesn't read the file path as part of the match, just the name.

I've tried:

--ignore-dir=match:/a\/b\/c.js/     " Non-is filters are not yet supported for --ignore-dir (what?)"

I can't find anything useful in the Ack manual soup. How do I ignore a file in a directory?

Upvotes: 4

Views: 949

Answers (2)

Andy Ray
Andy Ray

Reputation: 32066

The answer is it is impossible.

Upvotes: 3

Sylvain Leroux
Sylvain Leroux

Reputation: 51980

You might use find ... ! -path ... to easely search for files not matching a given path.

Further, find ... -exec ... \{} \+ allows to launch a command with the list of files found appended.

I don't have ack at hand, but using grep as a place-holder, this will lead to:

sh$ echo toto > a/b/c.js
sh$ echo toto > a/c.js
sh$ echo toto > a/b/x.js
sh$ find . \! -path './a/b/c.js' -type f -exec grep toto \{} \+
#        ^           ^                         ^^^^
#      pattern should match              replace by a call to `ack`
#       your search path
./a/c.js:toto
./a/b/x.js:toto

EDIT: there is a potential pitfall with the ./ at the start of the path pattern. you might prefer using find ... ! -samefile ... to reject found files having the same inode as the given file:

sh$ find . \! -samefile a/b/c.js -type f -exec grep toto \{} \+
#                      ^^^^^^^^
#                  no dot here; this is a *file*
#                  can't use glob patterns (*, ?, ...)
./a/c.js:toto
./a/b/x.js:toto

From man find:

   ! expr True  if  expr  is false.  This character will also usually need
          protection from interpretation by the shell.

   -path pattern
          File  name matches shell pattern pattern.  The metacharacters do
          not treat `/' or `.' specially; so, for example,
                    find . -path "./sr*sc"
          [...]

   -samefile name
          File refers to the same inode as name.   When -L is  in  effect,
          this can include symbolic links.

Upvotes: 1

Related Questions