Finding a specific file type with ack

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

for example

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

      

Obviously this doesn't work, but how can I do it with ack?

+4


source to share


3 answers


Use --type for this. To find python files like:

ack --type=python searchthis

      

List all supported types with

ack --help=types

      



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

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

      

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

+7


source


find . -name '*.scss' | ack -x 'blah'

      



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

+5


source


The short answer is:

ack blah --css

      

Long answer:

You can add file types to your search to teach Ack to view only those files. For example, let's link the css and sass extensions with the short name "stylesheets":

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

      

Now that you've taught Ack how to search for the types of files you want to associate with the short name "stylesheets", you can ask Ack to use that short name with:

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

      

Or in a short way:

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

      

Even in short, it's a lot to type for search and for this reason such commands are better used in a config file, for example ~/.ackrc

, but I prefer to use them with an alias in mine ~/.bashrc

.

But why does it default to --type=css

or --css

? Because Ack has default file name groups that you can ack --dump

use ack --dump

.

Using Ack version: 2.24

+1


source







All Articles