LinuxCommandLibrary

ag

TLDR

Search for a pattern recursively

$ ag [pattern]
copy
Search in specific file types
$ ag --python [pattern]
copy
Search with case insensitivity
$ ag -i [pattern]
copy
Show only file names containing matches
$ ag -l [pattern]
copy
Search for literal string (no regex)
$ ag -Q "[literal string]"
copy
Search with context lines
$ ag -C [3] [pattern]
copy

SYNOPSIS

ag [options] pattern [path...]

DESCRIPTION

ag (The Silver Searcher) is a code-searching tool similar to ack but faster. It searches through source code recursively, automatically ignoring files listed in .gitignore and .hgignore, and skipping hidden files and binary files.
The tool is optimized for speed, using multiple threads and memory-mapped I/O. It supports Perl-compatible regular expressions and provides colored output with context lines.

PARAMETERS

-i, --ignore-case

Case insensitive search
-s, --case-sensitive
Case sensitive search (default)
-v, --invert-match
Invert the match
-w, --word-regexp
Match whole words only
-l, --files-with-matches
Print only file names with matches
-L, --files-without-matches
Print file names without matches
-c, --count
Print count of matches per file
-C n, --context=n
Print n lines of context
-A n, --after=n
Print n lines after match
-B n, --before=n
Print n lines before match
-Q, --literal
Treat pattern as literal string
--python, --js, --html, etc.
Search only specific file types
-g pattern
Search file names matching pattern
--hidden
Search hidden files
-u, --unrestricted
Search all files (ignore .gitignore)
-z, --search-zip
Search inside compressed files

CAVEATS

Not as fast as ripgrep on very large codebases. Some regex features may differ from grep. File type detection is based on extensions, not content.

HISTORY

ag was created by Geoff Greer and first released in 2011. It was designed as a faster alternative to ack, achieving significant speed improvements through parallelization and smarter file filtering. It inspired later tools like ripgrep.

SEE ALSO

rg(1), ack(1), grep(1), git-grep(1)

Copied to clipboard