How to supercharge string search through a directory hierarchy on a Linux/Unix

Click for: original source

Usually, I use the grep command/egrep command for code searching on my box. Recently, I come across another cool tool called ag. It is an attempt to make something better than ack, which itself is better than grep command. Let us see how to install and use the ag tool on Unix-like operating systems. By Vivek Gite.

The ag command can recursively search for PATTERN in PATH. Like grep or ack, but faster. Ag uses Pthreads to take advantage of multiple CPU cores and search files in parallel. Files are mmap()ed instead of read into a buffer. Literal string searching uses Boyer-Moore strstr.

# Search for 'curl' in files with suffix .sh, .bash, .csh, .tcsh, .ksh, .zsh and .fish shell scripts only.
ag --shell 'curl' ~/bin/

# ag can search any number of files simultaneously.
ag UNIX foo bar foobar

# ag can only report the number of times that the pattern has been matched for each file. Just pass the -c option:
ag -i -c unix foo bar foobar

# One can search contents of compressed files too. Just pass the -z option:
ag -z 'main()' file.zip xkcd.tar.gz

# No need to use the egrep command with 'regex':
ag '[Cu]url' ~/bin/

The article provide the answer to the question why use ag tool:

  • It is faster than both grep and ack
  • It ignores file patterns found in your ~/.gitignore and ~/.hgignore
  • You can add custom ignore patterns to a ~/.ignore file
  • Ag uses Pthreads to take advantage of multiple CPU cores and search files in parallel

It also provide example how to search for various patterns and installation instruction for various Linux flavours and Mac. Good read!

[Read More]

Tags linux devops software cloud performance