11

I would like to include files with a specific name -- not an extension -- in my ack search. Is this possible with ack?

If I use the -G option, this would exclude all other file types. (So I can't put it in my .ackrc file.)

I tried to use --type-set mytype=filename.txt but this only works for extensions, so this would search for files including the pattern .filename.txt, thus not find filename.txt. (That's also what the ack --help types shows: --mytype .filename.txt, not --mytype filename.txt.)

Someone any ideas?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Marvin
  • 482
  • 5
  • 19

4 Answers4

12

man ack says that the files to be searched in can be given through standard input.
So this should work:

find . -name filename.txt | ack PATTERN -

Unfortunately, it doesn't. It gives ack: Ignoring 1 argument on the command-line while acting as a filter., which apparently is a bug in ack. When this bug will be fixed, we should be able to use

find . -name filename.txt | ack --nofilter PATTERN -

Marvin
  • 482
  • 5
  • 19
6

You also do it like if you're using zsh:

ack 'Pattern' **/filename.txt

slm
  • 15,396
  • 12
  • 109
  • 124
iltempo
  • 15,718
  • 8
  • 61
  • 72
  • In my case, this doesn't check all the files `filename.txt` in the subtree, only some of them. – Marvin Dec 11 '12 at 09:15
  • 1
    This seems to depend on the shell you are using. For bash you can go on searching like this: `*/filename.txt, */*/filename.txt,...` – Marvin Mar 21 '13 at 17:36
3

What you're asking is "can I make a filetype that ack recognizes based on a filename", and the answer is "No, not in ack 1.x, but you can in ack 2.0". ack 2.0 is in alpha release, and we hope to have a beta by Christmas.

As @Christian pointed out above, you can specify the given filename on the command line, but that bypasses filetype checking entirely.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
1

I know this is a late reply, but could you simply specify the filename when you run ack?

ack 'My Text' filename.txt
Christian
  • 3,917
  • 2
  • 23
  • 40
  • -> `ack filename.txt: No such file or directory` unless there is a `filename.txt` in the current directory, but that would still ignore all the other files with this name in the subdirectories. – Marvin Jul 05 '11 at 08:01