Search for files & file names using silver searcher

41.3k views Asked by At

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"

6

There are 6 answers

1
zfogg On

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.

4
Greg On

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]

0
djondal On

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

0
jchilders On

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 $
9
vincentleest On

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
3
Greg On

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.