How do you exclude a specific file from ack (ack-grep)?
Asked Answered
K

5

65

With the normal grep command there is an --exclude option (covered in detail here: Use grep --exclude/--include syntax to not grep through certain files) that lets you ignore specific files when you are grepping.

Ack's --type option takes care of 95% of the cases where you'd want to exclude files, but it doesn't (as far as I can tell) handle the case of excluding a specific file. I've got a compiled JS file that has the contents of every other JS file in it (on a single line), so every time I grep for anything I get back the entire contents of that giant compiled file.

I'd hate to have to give up on ack and go back to grep over this, but it is really annoying. Please, someone tell me there's a way to exclude specific files from ack searches.

Kerrikerrie answered 24/7, 2012 at 17:21 Comment(0)
P
17

No, ack 1.x will not let you exclude a single file. In ack 2.0, you can. ack 2.0 was currently in its first alpha is now the current recommended version of ack.

As far as your specific case of the minified Javascript file, what version of ack are you using? ack 1.96 should ignore minified JavaScript based on extension. If the extension check doesn't work, you could probably hack ack itself, since it's a single Perl program. You can hack the is_searchable function.

In ack 2.0, we're looking at ignoring any line over, say, 500 characters because it's clearly not source code.

You may also want to join us on the ack-users mailing list. We're pretty responsive.

Paluas answered 24/7, 2012 at 21:32 Comment(2)
In fact, my next stop was going to be the ack mailing list, but I figured I'd check here first to make sure I wasn't missing something obvious :-) Thanks Andy.Kerrikerrie
P.S. I'm choosing the filename for my compiled JS file, so at the moment it's just built.js. I'll try renaming it to built.min.js and see if that helps.Kerrikerrie
B
120

To ignore a single file using ack 2.0, provide a filter spec to the --ignore-file flag:

--ignore-file=is:cljout.js

Boycie answered 14/6, 2013 at 2:7 Comment(8)
What is the is: doing here?Unni
Exact filename match. beyondgrep.com/documentation/ack-2.04-man.html#is_filenameRetardant
This works for me on filenames, but not if I want to give a specific file (i.e. path and filename).Bast
How to specify multiple?Loch
You can use match along with a regex to match filenames and ext for extensions. Exclude js files --ignore-file=ext:js. Exclude files with name foo and bar --ignore-file='match:/foo|bar/'. Escape the parameter with quotes in case of regex with | so that shell doesn't execute it as a pipe.Nativity
This whole is: thing is so not obvious and annoyingLoch
Doesn't make it easy to split on a path, e.g. it would be useful to use a different delimiter: --ignore-file='match:#path/to/thing/whatever.ext#'Cly
For those who find the --ignore-file syntax too awkward, you could use something like this: REGEX='find.*stuff'; ack "$REGEX" -l | ack -v bad_file | ack --files-from - "$REGEX" which will search and output just the filenames, then filter out filenames with the word 'bad_file', then do the search again only on the files that are leftRenferd
L
30

If you run ack --create-ackrc it will output a whole bunch of presets for common files like minified javascript, css and directories like node_modules and .git. Very useful!

Here's the minified js option:

# minified Javascript
--ignore-file=match:/[.-]min[.]js$/
--ignore-file=match:/[.]js[.]min$

And here's how to ignore a directory called foo:

--ignore-directory=is:foo

If you want to save this to .ackrc run

ack --create-ackrc >> ~/.ackrc

and then delete what you don't need from that file.

Also, you can create another .ackrc file in your project's root folder for project specific stuff. I put that in .gitignore so it doesn't clutter the repo.

Lactometer answered 30/6, 2017 at 3:28 Comment(1)
I believe --ignore-directory doesn't accept filters like is: like --ignore-file does. --ignore-directory accepts just a directory name.Passport
P
17

No, ack 1.x will not let you exclude a single file. In ack 2.0, you can. ack 2.0 was currently in its first alpha is now the current recommended version of ack.

As far as your specific case of the minified Javascript file, what version of ack are you using? ack 1.96 should ignore minified JavaScript based on extension. If the extension check doesn't work, you could probably hack ack itself, since it's a single Perl program. You can hack the is_searchable function.

In ack 2.0, we're looking at ignoring any line over, say, 500 characters because it's clearly not source code.

You may also want to join us on the ack-users mailing list. We're pretty responsive.

Paluas answered 24/7, 2012 at 21:32 Comment(2)
In fact, my next stop was going to be the ack mailing list, but I figured I'd check here first to make sure I wasn't missing something obvious :-) Thanks Andy.Kerrikerrie
P.S. I'm choosing the filename for my compiled JS file, so at the moment it's just built.js. I'll try renaming it to built.min.js and see if that helps.Kerrikerrie
B
3

Also good to know with ack2.0, you can specify these options in a .ackrc file if you have to frequently filter files.

Bellyful answered 27/6, 2016 at 22:27 Comment(0)
A
3

If you want to exclude specific folder, following command may help you:

ack keywords --ignore-dir folder_name

man ack will provide further details :)

--[no]ignore-dir=DIRNAME, --[no]ignore-directory=DIRNAME

--ignore-file=FILTERTYPE:FILTERARGS

Anywhere answered 13/5, 2018 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.