31

Using Silver Searcher, how can I search for:

  1. (non-binary) files with a word or pattern AND
  2. all filenames, with a word or pattern including filenames of binary files.

Other preferences: would like to have case insensitive search and search through dotfiles.


Tried to alias using this without much luck:

alias search="ag -g $1 --smart-case --hidden && ag --smart-case --hidden $1"

Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
  • 2
    If you're using that alias in bash, you should note that [bash aliases don't take parameters](https://stackoverflow.com/q/7131670/1743811). – doubleDown Sep 14 '17 at 06:34

6 Answers6

60

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

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
vincentleest
  • 925
  • 1
  • 8
  • 18
  • 1
    Nice, not sure how I missed `-g` and `-G` back when I submitted my original answer. The OP's wording is kind of confusing but I think what he wants is to run a single command that searches all filenames as well as nonbinary file contents. Seems like `ag -g [search] && ag -l [search]` would work. – Greg Jul 24 '15 at 21:21
  • 1
    I suppose this is a better answer and I should mark this answer? Any thoughts? – Jikku Jose Jul 25 '15 at 03:16
  • @JikkuJose I added a new answer, which I believe solves your original question completely – Greg Jul 28 '15 at 19:25
  • 3
    @JikkuJose I think the community agrees that this is a better answer to your question. – vincentleest Jul 28 '15 at 20:24
  • 1
    `-g` also deserves a place in your answer. – leewz Mar 25 '16 at 23:12
  • Seems like correct answer, but does not work for me. ag does not seem to have a way to get version, so cannot know if it due to older version on brew/Mac. – N0thing Aug 20 '16 at 00:28
  • @N0thing try `ag --version` – vincentleest Aug 22 '16 at 19:57
  • if you have extra time and are not in a rush, you can as well do it with our good old slower brother: `grep -Hirn --include "*haystack*" needle *` – ccpizza Feb 11 '23 at 16:37
  • chatgpt got me close, but humans still win for the moment... +1 – mattb Jun 08 '23 at 13:41
10

Try this:

find . | ag "/.*SEARCHTERM[^/]*$"

The command find . will list all files.

We pipe the output of that to the command ag "/.*SEARCHTERM[^/]*$", which matches SEARCHTERM if it's in the filename, and not just the full path.

zfogg
  • 455
  • 5
  • 22
  • 3
    Had almost solved a variant using a simpler solution: ag -g . | ag 'search_term'; Yes, your pattern can be used to filter to the filename and not the path. – Jikku Jose Dec 18 '13 at 02:07
5

Try adding this to your aliases file. Tested with zsh but should work with bash. The problem you encountered in your example is that bash aliases can't take parameters, so you have to first define a function to use the parameter(s) and then assign your alias to that function.

searchfunction() {
  echo $(ag -g $1 --hidden)
  echo $(ag --hidden -l $1)
}

alias search=searchfunction

You could modify this example to suit your purpose in a few ways, eg

  • add/remove the -l flag depending on whether or not you want text results to show the text match or just the filename
  • add headers to separate text results and filename results
  • deduplicate results to account for files that match both on filename and text, etc.

[Edit: removed unnecessary --smart-case flag per Pablo Bianchi's comment]

Greg
  • 773
  • 9
  • 22
  • Any way to tweak this to work with 'sack/sag' (https://github.com/sampson-chen/sack)? That would require combining the two ag queries into one so the search cache from first command that sack relies on wouldn't get blown away. – Alexander Tsepkov Dec 28 '16 at 04:20
  • 2
    You can omit a parameter: `-S --smart-case` Match case-sensitively if there are any uppercase letters in PATTERN, case-insensitively otherwise. **Enabled by default.** – Pablo Bianchi Jul 15 '17 at 01:08
  • @PabloBianchi thanks. I had a (very) old version of ag, whose man page did not have the "enabled by default" note, but upon testing, it seems even old versions of ag did indeed use this flag by default. Updated my answer. – Greg Jul 19 '17 at 22:58
  • how to get this to output each result on a new line? – lacostenycoder Jan 16 '21 at 04:54
4

Found this question looking for the same answer myself. It doesn't seem like ag has any native capability to search file and directory names. The answers above from Zach Fogg and Jikku Jose both work, but piping find . can be very slow if you're working in a big directory.

I'd recommend using find directly, which is much faster than piping it through ag:

Linux (GNU version of find)

find -name [pattern]

OSX (BSD version of find)

find [pattern]

If you need more help with find, this guide from Digital Ocean is pretty good. I include this because the man pages for find are outrageously dense if you just want to figure out basic usage.

Greg
  • 773
  • 9
  • 22
4

To add to the previous answers, you can use an "Or" Regular Expression to search within files matching different file extensions.

For example to just search a string in C++ header files [.hpp] and Makefiles [.mk] ) :

ag -G '.*\.(hpp|mk)' my_string_to_search

djondal
  • 2,521
  • 3
  • 24
  • 40
1

After being unsatisfied with mdfind, find, locate, and other attempts, the following worked for me. It uses tree to get the initial list of files, ag to filter out directories, and then awk to print the matching files themselves.

I wound up using tree because it was more (and more easily) configurable than the other solutions I tried and is fast.

This is a fish function:

function ff --description 'Find files matching given string'
  tree . --prune --matchdirs -P "*$argv*" -I "webpack" -i -f --ignore-case -p |
    ag '\[[^d].*' |
    awk '{print $2}'
end

This gives output similar to the following:

~/temp/hello_world $ ff controller
./app/controllers/application_controller.rb
./config/initializers/application_controller_renderer.rb
~/temp/hello_world $
jchilders
  • 351
  • 1
  • 5
  • 10