LinuxCommandLibrary

ack

Search files for a pattern

TLDR

Search for files containing a string or regex 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...]

PARAMETERS

-i, --ignore-case
    Ignores case distinctions in the PATTERN.

-v, --invert-match
    Selects non-matching lines; prints lines that do not contain the PATTERN.

-w, --word-regexp
    Selects only those lines containing matches that form whole words.

-l, --files-with-matches
    Suppresses normal output; instead, prints the name of each input file from which output would normally have been printed.

-c, --count
    Suppresses normal output; instead, prints a count of matching lines for each input file.

-H, --no-filename
    Suppresses the prefixing of filenames on output.

--type=
    Searches only files of the specified type (e.g., --type=perl, --type=html). Use --show-types to list available types.

--no-type=
    Excludes files of the specified type from the search.

--ignore-dir=


    Excludes the specified directory from the search. Can be used multiple times.

--no-recurse
    Does not recurse into subdirectories. Searches only files in the current directory or explicitly specified files.

--pager=
    Sends output through a specified pager program, e.g., less -R. Defaults to less -R if ACK_PAGER or PAGER environment variables are set.

--sort-files
    Sorts the files by name before searching. Useful for consistent output.

DESCRIPTION

ack is a command-line tool designed specifically for developers to quickly search plain text files, particularly source code. While similar to grep, ack distinguishes itself by having sensible defaults tailored for programming projects.

By default, ack recursively searches directories, ignores common version control directories (like .git, .svn, .hg), and automatically skips binary files, backup files, and other non-source code files. It also intelligently determines file types based on extensions and shebang lines, allowing users to search only specific code types (e.g., --type=perl or --type=java).

ack's output is often more readable than grep's, grouping matches by file and providing syntax highlighting. Its primary goal is to make common code search tasks faster and more convenient, reducing the need for complex command-line arguments often required with grep for similar functionality. It's written in Perl and highly extensible.

CAVEATS

ack requires Perl to be installed on the system, as it's written in Perl. On some Linux distributions (e.g., Debian/Ubuntu), the command might be installed as ack-grep to avoid conflicts with a different utility named ack (the 'Analyse Control K' command). While generally faster for code searches due to its intelligent filtering, for extremely large non-code text files or very simple searches, grep or tools like ripgrep might offer better performance.

DEFAULT BEHAVIOR

ack's default behavior is to recursively search the current directory for files, automatically ignoring common version control system directories (like .git, .svn, .hg), common build directories, and common temporary/backup files. It also attempts to identify file types and only search relevant files.

FILE TYPE FILTERING

ack maintains an internal list of file types and their associated file extensions or shebang lines. This allows users to narrow searches to specific languages (e.g., --type=python) or exclude them (--no-type=xml). Users can also define custom file types in their .ackrc file.

CUSTOMIZATION WITH .ACKRC

ack can be extensively customized using a configuration file, typically named .ackrc, located in the user's home directory. This file allows users to set default options, define custom file types, and manage ignored directories, making ack behave exactly as preferred for their projects.

HISTORY

ack was created by Andy Lester and first publicly released in 2007. Its development was motivated by the desire to provide a more developer-friendly and intelligent search tool than grep for common programming tasks. It quickly gained popularity within the Perl and broader open-source communities for its sensible defaults and ease of use. ack's success and design choices have influenced the development of subsequent code search tools, such as the highly performant ripgrep (rg).

SEE ALSO

grep(1), ripgrep(1), find(1), xargs(1)

Copied to clipboard