Getting ack to search through .htaccess or other dotfiles
Asked Answered
C

3

8

Is there any way to get ack to search through a file whose filename starts with a . (e.g. .htaccess), without resorting to the --all or --unrestricted options?

I've tried adding the following to my ~/.ackrc file to no avail:

--type-set=apache=.htaccess
Cadmus answered 24/1, 2011 at 1:36 Comment(0)
C
5

It would appear that ack doesn't recognize a filename that's all extension; that is, when you specify an extension of ".htaccess", ack only looks for files with at least one character before that extension.

To get around this, you can use -u/--unrestricted in combination with the -G regex flag (to restrict the search to files whose names match a regex). For example:

$ ack -u -G '\.htaccess' pattern
Christianson answered 24/1, 2011 at 3:43 Comment(2)
To configure in .ackrc, place -u and -G/\.(bash_aliases|bash_history|bashrc)$ (for example) on separate lines. The missing space after -G is significant. The advantage is that you don't need to add these parameters and it doesn't search through .orig files and the like. The downside is of course when you don't want to use the -u option.Scrutator
Note: this doesn't seem to work with ack 2 - ack -u -G '\.htaccess' pattern # Option '-u' is not valid in ack 2, and without the -u it says -G is not a valid option eitherFini
F
1

Updated @Sean's answer for Ack 2

since they removed the two flags that @Sean used

ack 2 release notes say:

The -G option has been removed. Two patterns on the command line was ambiguous. In this command line:

ack1 -G filepattern -i -w searchpattern

which pattern does -i and -w modify? Now, with ack 2.0, you can use the new -x to pipe filenames from one invocation of ack into another.

ack2 -g -i filepattern | ack2 -x -w searchpattern

and the -u (unrestricted) flag is not needed any more. So what you want is:

ack -g "\.htaccess" | ack -x pattern

Or, for that matter, just use find to generate the file list (especially useful to find a file in a dotfolder):

find **/.hidden/complex-path/* | ack -x pattern
Fini answered 1/2, 2016 at 6:18 Comment(0)
D
0

You can't do it in ack 1.x. ack 2.0 will have more flexible ways of specifying files.

Dispersoid answered 24/1, 2011 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.