I have started using ack which is much faster than grep. However using ack I want to search for file name rather than file contents. Is there a way to do that?
Asked
Active
Viewed 1.0k times
4 Answers
25
ack -g REGEX
Print files where the relative path + filename matches REGEX
Danil Kutkevich
- 351
- 3
- 6
-
1add `[^/]*$` to your REGEX to search in the filename only – simon Sep 03 '17 at 01:14
17
Use the right tool for the right job. If you want to search for filename, use 'find':
$ # search for all *.txt file in current directory
$ find . -iname "*.txt"
Lie Ryan
- 4,507
- 3
- 22
- 26
-
1very true. "once you hold a hammer in your hand everything starts to look like a nail" :) – akira Apr 25 '10 at 15:05
-
2Well, ack (and grep) are nicer because you don't have to be explicit about regexes unless you want to. I usually just do a `find . | ack whatev.ext` – Ehtesh Choudhury May 25 '12 at 15:49
-
-
1find and ack behave differently since ack filters out a lot of results by default (contents of version control directories, compiled objects in a source repository) that would be included by find. The best hammer depends on the results you want. – Joshua Goldberg Jan 08 '15 at 22:05
-
Actually, it's apparently [kind of tricky](http://unix.stackexchange.com/a/84039/77539) to get ack version 2 to behave like find. – Joshua Goldberg Jan 08 '15 at 22:10
-
1
9
I agree it makes sense to use find if you're just searching for *.txt files. However, ack has powerful file-type detectiong features, so you can use
ack -f --perl
which will find all the Perl files, based on both the filename (*.pm, *.pl, *.t and *.pod) and the shebang line.
Andy Lester
- 1,253
- 6
- 15