search for a specific file type with ack
Asked Answered
ack
B

3

33

How can I use ack to search in files of a specific file type.

For example

ack -f .scss blah
acl -f .rb blah

Obviously the above does not work but how can I do this with ack?

Bursiform answered 8/8, 2014 at 9:32 Comment(0)
R
36

Use --type for this. To only search python files for example:

ack --type=python searchthis

List all the supported types with

ack --help=types  # ack 2.x
ack --help-types  # ack 3.x.x

If you want to create a custom type, add something like this to ~/.ackrc:

--type-add=custom:ext:rb
--type-set

Then you can use --type=custom with ack to just search file with the .rb extension.

Rent answered 14/8, 2014 at 9:32 Comment(0)
L
18
find . -name '*.scss' | ack -x 'blah'

-x Read the list of files to search from STDIN.

Lozier answered 9/3, 2018 at 15:44 Comment(0)
L
12

Short answer:

ack blah --css

Long answer:

You could add filetypes to your search to teach Ack to look only in those files. For example, lets associate css and sass extensions with a "stylesheets" short name:

ack --type-add=stylesheets:ext:css,sass

Now that you've teach Ack that you want to search on filetypes that you wanted to associate with the "stylesheets" short name, you can ask Ack to use that short name with:

ack --type-add=stylesheets:ext:css,sass --type=stylesheets blah

Or with the short way:

ack --type-add=stylesheets:ext:css,sass --stylesheets blah

Even in the short way, that's a lot to type for a search and that's the reason those kind of commands are better used on a configuration file like ~/.ackrc, but I prefer to use them with an alias in my ~/.bashrc.

But why does it works by default with --type=css, or --css? Because Ack have a default filenames groups that you can dump with ack --dump.

Using Ack version: 2.24

Latt answered 12/11, 2018 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.