Ack: Search directory tree for files with a particular extension
Asked Answered
S

3

15

I basically just want to do ack foo *.citrus and have ack drill down and find the string 'foo' in all Citrus files in the current directory and below. The trouble is that this won't work if there aren't any Citrus files in the current directory.

I tried messing with -G without success. Do I really need to add a file type in .ackrc just to limit the search to files with a given extension?

Speer answered 19/11, 2011 at 20:28 Comment(0)
E
12

By default, ack searches only in files with known types ( like *.java, *.cpp etc. ). It doesn't know about files *.citrus, so to search in such files you must use -a cmd line switch:

$ack -a -G '\.citrus$' foo
1.d/1.citrus
1:foo_bar
Etiquette answered 25/2, 2012 at 7:17 Comment(3)
or you can use this tip: #3871111Etiquette
The -a is overkill. --type-set is the way to go.Brandebrandea
This is logical, but less than optimally useful. Such a simple task -"search files with this extension" - requires you to tell it not only the extension using a regular extension (-G), but that you do want it to actually search those files (-a or --type-set - even this choice is controversial). You gotta love Unix.Speer
L
16

As suggested by Andy Lester, you can also create a typeset without taking the trouble to add it in your .ackrc file:

ack --type-set=cit=.citrus --cit "foo"
Livre answered 27/9, 2013 at 15:31 Comment(0)
E
12

By default, ack searches only in files with known types ( like *.java, *.cpp etc. ). It doesn't know about files *.citrus, so to search in such files you must use -a cmd line switch:

$ack -a -G '\.citrus$' foo
1.d/1.citrus
1:foo_bar
Etiquette answered 25/2, 2012 at 7:17 Comment(3)
or you can use this tip: #3871111Etiquette
The -a is overkill. --type-set is the way to go.Brandebrandea
This is logical, but less than optimally useful. Such a simple task -"search files with this extension" - requires you to tell it not only the extension using a regular extension (-G), but that you do want it to actually search those files (-a or --type-set - even this choice is controversial). You gotta love Unix.Speer
B
4

You don't have to set it in .ackrc if you don't want. You can also set ACK_OPTIONS in your environment, or specify --type-set arguments on the command line. ack doesn't care.

Brandebrandea answered 26/2, 2012 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.