LinuxCommandLibrary

ack

Search files for a pattern

TLDR

Search for files containing a string or regular expression in the current directory recursively

$ ack "[search_pattern]"
copy

Search for a case-insensitive pattern
$ ack [[-i|--ignore-case]] "[search_pattern]"
copy

Search for lines matching a pattern, printing only the matched text and not the rest of the line
$ ack [[-o|--output='$&']] "[search_pattern]"
copy

Limit search to files of a specific type
$ ack [[-t|--type]] [ruby] "[search_pattern]"
copy

Do not search in files of a specific type
$ ack [[-t|--type]] no[ruby] "[search_pattern]"
copy

Count the total number of matches found
$ ack [[-c|--count]] [[-h|--no-filename]] "[search_pattern]"
copy

Print the file names and the number of matches for each file only
$ ack [[-c|--count]] [[-l|--files-with-matches]] "[search_pattern]"
copy

List all the values that can be used with --type
$ ack --help-types
copy

SYNOPSIS

ack [options] PATTERN [FILE... DIRECTORY...]

PARAMETERS

-i, --ignore-case
    Ignore case distinctions in the search pattern.

-w, --word-regexp
    Match only whole words.

-v, --invert-match
    Select non-matching lines.

-l, --files-with-matches
    Only print the filenames containing matches, not the matching lines.

-n, --no-recurse
    Do not recurse into directories.

--type=TYPE
    Search only files of a specific type. For example, --type=perl.

--noTYPE
    Do not search files of a specific type. For example, --nohtml.

-f, --files-without-matches
    Only print the names of files that do not contain matches.

--column
    Show column numbers of first match

--group
    Group matches by file, even when searching multiple files

-A NUM, --after NUM
    Print NUM lines of context after matching lines.

-B NUM, --before NUM
    Print NUM lines of context before matching lines.

-C NUM, --context NUM
    Print NUM lines of context around matching lines.

DESCRIPTION

ack is a command-line tool designed for searching large amounts of text, particularly source code. It's a powerful alternative to grep, optimized for programmers with features like automatic file type filtering based on content and filename extensions, and smart handling of directory traversal. ack understands common programming languages and can automatically exclude version control directories and backup files, reducing noise in search results. It also provides concise output by default, showing only the lines that match the search pattern, making it easier to focus on relevant information. Unlike grep, ack doesn't require you to specify file types to search within, making it more convenient for quick code searches.

ack respects configuration files that enable you to set up defaults, such as which file types to include or exclude and where to find them.

Designed to improve developer workflow by allowing quicker and easier searching of codebases.

It's written in Perl and is highly configurable.

CAVEATS

ack's heuristics for file type detection might not always be perfect. Customizing the file type associations and ignore rules through configuration files is often necessary for optimal results. Older versions of ack might have different option names or behaviors.

CONFIGURATION FILES

ack reads configuration from multiple locations to allow for different scopes of customization. These locations include:
~/.ackrc: User-specific configuration.
/etc/ackrc: System-wide configuration.
ack uses these files to override defaults

EXIT STATUS

The ack command exits with status 0 if any match is found, 1 if no match is found, and greater than 1 in case of an error.

HISTORY

ack was created by Andy Lester as an improvement over grep for searching code. It has gained popularity among programmers due to its code-aware features and ease of use. Development has been focused on improving performance, adding support for more file types, and enhancing configuration options. Initially released in 2005, it quickly found a niche amongst developers and system administrators searching through source code and configuration files.

SEE ALSO

grep(1), find(1)

Copied to clipboard