Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for files & file names using silver searcher

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"

like image 237
Jikku Jose Avatar asked Sep 10 '13 07:09

Jikku Jose


People also ask

How can you quickly search for files and folders?

To search for files in File Explorer, open File Explorer and use the search box to the right of the address bar. Tap or click to open File Explorer. Search looks in all folders and subfolders within the library or folder you're viewing. When you tap or click inside the search box, the Search Tools tab appears.

How do I search for files on my screen?

Click the Start button to go to the Start screen, then start typing to search for a file. The search results will appear on the right side of the screen. Simply click a file or folder to open it.


3 Answers

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

like image 151
vincentleest Avatar answered Oct 09 '22 23:10

vincentleest


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.

like image 27
zfogg Avatar answered Oct 09 '22 22:10

zfogg


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]

like image 37
Greg Avatar answered Oct 09 '22 21:10

Greg