The Silver Searcher – A Code Searching Tool for Programmers

[‘

n

The Silver Searcher is a free and open source, cross platform source code searching tool similar to ack (a grep-like tool for programmers) but faster. It runs on Unix-like systems and Windows operating systems.

n

The major difference between the silver searcher and ack is that the former is designed for speed, and benchmark tests prove that it is indeed faster.

n

If you spend a lot of time reading and searching through your code, then you need this tool. It aims at being fast and ignoring files that you don’t want to be searched. In this guide, we will show how to install and use The Silver Searcher in Linux.

n

How to Install and Use The Silver Searcher in Linux

n

The silver searcher package is available on most Linux distributions, you can easily install it via your package manager as shown.

n

$ sudo apt install silversearcher-agttttt#Debian/Ubuntu rn$ sudo yum install epel-release the_silver_searchertt        #RHEL/CentOSrn$ sudo dnf install silversearcher-agttttt#Fedora 22+rn$ sudo zypper install the_silver_searchertttt#openSUSErn$ sudo pacman -S the_silver_searcher           tttt#Arch rn

n

After installing it, you can run the ag command line tool with the following syntax.

n

$ ag file-type options PATTERN /path/to/filern

n

To see a list of all supported file types, use the following command.

n

$ ag  --list-file-typesrn

n

This example shows how to recursively search for all scripts that contain the word “root” under the directory ~/bin/.

n

$ ag root ./bin/rn

n

Search a Pattern in Files
Search a Pattern in Files

n

To print the filenames matching PATTERN and the number of matches in each file, other than the number of matching lines, use the -c switch as shown.

n

$ ag -c root ./bin/rn

n

Print Number of Matches
Print Number of Matches

n

To match case-sensitively, add the -s flag as shown.

n

$ ag -cs ROOT ./bin/rn$ ag -cs root ./bin/rn

n

Match Case Sensitive
Match Case Sensitive

n

To print statistics of of a search operation such as files scanned, time taken, etc., use the the --stats option.

n

$ ag -c root --stats ./bin/rn

n

Print Search Operations Summary
Print Search Operations Summary

n

The -w flag tells ag to only match whole words similar to grep command.

n

$ ag -w root ./bin/rn

n

You can show column numbers in results using the --column option.

n

$ ag --column root ./bin/rn

n

Show Column Numbers in Output
Show Column Numbers in Output

n

You can also use ag to search through purely text files, using the -t switch and the -a switch is used to search all types of files. In addition, the -u switch enables searching though all files, including hidden files.

n

$ ag -t root /etc/rnORrn$ ag -a root /etc/rnORrn$ ag -u root /etc/rn

n

Ag also supports searching through the contents of compressed files, using the -z flag.

n

$ ag -z root wondershaper.gzrn

n

Search Content in Compressed Files
Search Content in Compressed Files

n

You can also enable following of symbolic links (symlinks in short) with the -f flag.

n

$ ag -tf root /etc/ rn

n

By default, ag searches 25 directories deep, you can set the depth of the search using the --depth switch, for example.

n

$ ag --depth 40 -tf root /etc/rn

n

For more information, see the silver searcher’s man page for a complete list of usage options.

n

$ man agrn

n

To find out, how the silver searcher works, see its Github repository: https://github.com/ggreer/the_silver_searcher.

n

That’s it! The Silver Searcher is a fast, useful tool for searching through files that make sense to search. It is intended for programmers for quickly searching though large source-code base. You can give it a try and share your thoughts, with us via the comment form below.

n

‘]