According to the man page of ag
-G --file-search-regex PATTERN
Only search files whose names match PATTERN.
You can use the -G
option to perform searches on files matching a pattern.
So, to answer your question:
root@apache107:~/rpm-4.12.0.1# ag -G cpio.c size
rpm2cpio.c
21: off_t payload_size;
73: /* Retrieve payload size and compression type. */
76: payload_size = headerGetNumber(h, RPMTAG_LONGARCHIVESIZE);
the above command searches for the word size
in all files that matches the pattern cpio.c
Reference:
man page of ag
version 0.28.0
Note 1:
If you are looking for a string in certain file types, say all C sources code, there is an undocumented feature in ag
to help you quickly restrict searches to certain file types.
The commands below both look for foo
in all php files:
find . -name \*.php -exec grep foo {}
ag --php foo
While find + grep
looks for all .php
files, the --php
switch in the ag
command actually looks for the following file extensions:
.php .phpt .php3 .php4 .php5 .phtml
You can use --cpp
for C++ source files, --hh
for .h
files, --js
for JavaScript etc etc. A full list can be found here
Note 2:
Keep in mind that when using language specific flags such as --php
alongside the --file-search-regex
option, then the --file-search-regex
appears to be ignored at least per this example below
$ mkdir ag-test
$ cd ag-test
$ echo hello > blah.php
$ echo hello > mmkay.php
$ echo hello > special_something.txt
$ echo hello > special_also.php
$ echo nothing > nope.php
$ echo nothing > nada.php
$ echo nothing > special.php
# Only --php
$ ag --php -l hello .|cat
special_also.php
mmkay.php
blah.php
# Only --file-search-regex
$ ag --file-search-regex special -l hello .|cat
special_also.php
special_something.txt
# Both
$ ag --php --file-search-regex special -l hello .|cat
special_also.php
mmkay.php
blah.php