Can ack search only through files that match a specific 'glob' pattern (eg: search for foo in all files named "bar*.c"). The command
ack foo "bar*.c"
only works in the current directory.
Note: I know it's possible with find -exec:
find . -name "bar*.c" -type f -exec ack foo {} +
But I would like a small and simple ack command, because find doesn't skip version control directories.
Best Answer
Searching directories
Based on the synopsis shown in the man page I would say yes it can process a directory, but looking at the switches it cannot look for just a file based on a pattern. For that you'll have to enlist
find
. The commandack
does include the option--files-from=FILE
so that it can be fed a list of files fromfind
.synopsis
usage
There is the
--ignore-file=
option which may give you what you want but seems a bit of a pain to actually use.Searching specific types of files
The only other way I can conceive of doing just this via
ack
is to use its--type
switch:To see what types are available:
format. For example, both --type=perl and --perl work. --[no]cpp .cpp .cc .cxx .m .hpp .hh .h .hxx --[no]objcpp .mm .h --[no]perl .pl .pm .pod .t .psgi; first line matches /^#!.*\bperl/ --[no]perltest .t
Examples
Find all the Perl files, based on both the filename (*.pm, *.pl, *.t and *.pod) and the shebang line.
Find all the C++ files:
Searching for foo in bar*.c
So then how can you accomplish what you want? Well you'll have to likely use
find
to do this:You can also use
ack
's ability to search for files that match a given pattern in their filenames (using-g <pattern>
), and then pass this list to a second invocation ofack
using-x
or--files-from=-
..Using
-x
:Using
-files-from=-
:In either case we're matching the filenames that you want using this regex:
This matches files whose name is
bar.*c
and end after the.c
using the end of line anchor,$
. We also look to make sure that the names have a boundary character using\b
. This will fail for files that contain boundary characters such as$bar.c
or%bar.c
for example.