LinuxCommandLibrary

ag

Search files for a string

TLDR

Find files containing "foo", and print the line matches in context

$ ag foo
copy

Find files containing "foo" in a specific directory
$ ag foo [path/to/directory]
copy

Find files containing "foo", but only list the filenames
$ ag [[-l|--files-with-matches]] foo
copy

Find files containing "FOO" case-insensitively, and print only the match, rather than the whole line
$ ag [[-i|--ignore-case]] [[-o|--only-matching]] FOO
copy

Find "foo" in files with a name matching "bar"
$ ag foo [[-G|--file-search-regex]] bar
copy

Find files whose contents match a regex
$ ag '[^ba(r|z)$]'
copy

Find files with a name matching "foo"
$ ag [[-g|--filename-pattern]] foo
copy

SYNOPSIS

ag [options] [pattern] [path]
ag [options] -f [file1] [file2] ...

PARAMETERS

pattern
    The regular expression to search for. If omitted, ag reads from standard input.

path
    The directory or files to search. If omitted, ag searches the current directory recursively.

-i, --ignore-case
    Ignores case distinctions in patterns and data.

-w, --word-regexp
    Forces the pattern to match only whole words.

-l, --files-with-matches
    Prints only the names of files containing matches, one per line.

-v, --invert-match
    Inverts the match; selects non-matching lines.

-C , --context
    Prints lines of context around matches.

--nocolor
    Disables colorized output.

--no-skip-vcs-ignores
    Does not skip files ignored by Version Control Systems (e.g., .gitignore).

-g
    Searches for files whose names match the given (regexp).

DESCRIPTION

ag (The Silver Searcher) is a command-line utility designed for searching source code. It emerged as a faster and more developer-friendly alternative to traditional `grep` for large codebases. Its primary design goal is speed, achieved by automatically skipping files irrelevant to code, such as those specified in .gitignore or .hgignore, and avoiding common binary file types. This focus on intelligent defaults makes it incredibly efficient for quick text searches within development projects. It supports powerful features including regular expressions, file type filtering, context display, and colorized output, significantly enhancing the developer's search experience.

CAVEATS

While highly optimized for speed, ag can be more memory-intensive than grep for very large result sets. Its default behavior of ignoring VCS-ignored files and binary files is a feature, but users need to be aware of this default if a broader search is required.

SMART DEFAULTS

ag automatically ignores files specified in .gitignore, .hgignore, and other VCS ignore files, as well as common binary file types. This intelligent filtering is a core reason for its speed and relevance to code search.

PERFORMANCE OPTIMIZATION

Achieves its high performance through optimized C code, efficient file traversal, memory mapping, and skipping irrelevant files and directories, focusing solely on what's relevant to a programmer's search.

REGEX ENGINE

Leverages PCRE (Perl Compatible Regular Expressions) for robust and powerful pattern matching capabilities, allowing complex search queries.

HISTORY

ag (The Silver Searcher) was created by Andrew Gallant in 2012 as a faster alternative to ack, which itself aimed to improve upon grep for codebases. Its intelligent defaults and remarkable speed quickly made it a popular choice among developers for navigating and searching large programming projects, paving the way for even faster tools like ripgrep.

SEE ALSO

grep(1), ack(1), rg(1)

Copied to clipboard