The Silver Searcher is a reputable command line code-searching tool that is attributed to be cross-platform, open-source, and free. It shares most of its functional attributes with grep’s plain-text search features. The primary difference between these two tools is that Silver Searcher offers a faster performance overhead.
[ You might also like: Ripgrep – The Fastest Command Line Search Tool for Linux ]
This article is for programmers or wannabe programmers and Linux users that spend most of their time in front of a source code editor, OS terminal, or any other programmable environment. You will not only benefit from Silver searcher’s speed but also its specific file ignoring algorithm for file patterns like “.gitignore” and “.hgignore” extensions.
Install Silver Search in Linux
Depending on the Linux operating system distribution you are under, you can install the Silver Search tool from one of the following command selections.
$ sudo apt-get install silversearcher-ag [On Debian, Ubuntu and Mint] $ sudo yum install the_silver_searcher [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/the_silver_searcher [On Gentoo Linux] $ sudo pacman -S the_silver_searcher [On Arch Linux] $ sudo zypper install the_silver_searcher [On OpenSUSE]
Silver Searcher Usage Examples
To use this tool, you first need to be on your Linux terminal environment. The flexibility of this tool is that to match a specific search string, you can be on the same path as that file or directory or you can specify the path to the file or directory from where you want to query a search string result.
Find Specific String Matches in Directory and Sub-Directories
Silver search will filter and highlight the specified string match results from your current folders and directories to their sub-folders and sub-directories.
$ ag photo
Find String Matches in Specific Directory
The specified directory is queried for the matching string name and the results highlighted.
$ ag tech /home/dnyce/Desktop
Find String Matches in AckMate Format
Find the string matches in a directory and output them in an AckMate format.
$ ag --ackmate tech /home/dnyce/Desktop
Find String Matches and Print Associate Columns
Find the string matches in a directory and print column numbers in results.
$ ag --column tech /home/dnyce/Desktop
Find String Matches and Output Associated Word
Find the string matches in a directory and output only associated whole words.
$ ag -w tech /home/dnyce/Desktop
In the above case, the Silver searcher will output matches with “tech” as a complete word. This “tech” match in words like “technology” and “technician” will be ignored.
Find String in Text Files Only
The search matches only come from text files and no other file type. Protected files are inaccessible
$ ag -t photo .
Find String in All File Types
Find the string matches in all file types.
$ ag -a photo ~/Downloads
Matching binary files are also highlighted as a match.
Find String in Hidden Files
You can also find string matches in all file types with inclusion in hidden files.
$ ag -u -w .ignore
Find String in Compressed Files
If you need to query inside compressed or zipped files, a Silver searcher will take care of things. Your string match search should point to the relative path of that compressed file.
$ ag -z -a photo ~/Downloads/pintrest
Find String with Symbolic Links
The -f
flag triggers an output with these symlinks.
$ ag -tf root /etc/
Find String Matches with Certain Directory Depth
The default directory depth search of Silver search from the point a string search is triggered is 25. It searches 25 levels deep into a directory for possible string matches.
We can extend or reduce this depth search value with the --depth
parameter as demonstrated below.
$ ag -tf --depth 3 root /etc/
As you can see, the search for the string-match “root” has gone 3 directories deep.
Silver Search Editor Integration
You can use this search tool from your Vim (ack.vim) editor after implementing the following integration step.
Trace the .vimrc
file and save in it either of the following lines.
let g:ackprg = 'ag –vimgrep' or let g:ackprg = 'ag --nogroup --nocolor --column'
If you need more exposure and options to use this fast, extensive, and powerful command-line search tool, use the man ag
command on your Linux system terminal for more alternatives to its usage. This article has provided a balanced guide on how to navigate around the Silver search utility.