ack-grep – A grep-like program specifically for large source trees

Sponsored Link
Ack is designed as a replacement for 99% of the uses of grep. ack is intelligent about the files it searches. It knows about certain file types, based on both the extension on the file and, in some cases, the contents of the file.

Ack also ignores backup files and files under .cvs and .svn directories.

Install ack-grep in Ubuntu

sudo apt-get install ack-grep

Using ack-grep

Usage: ack [OPTION]... PATTERN [FILE]

Search for PATTERN in each source file in the tree from cwd on down.If [FILES] is specified, then only those files/directories are checked.ack may also search STDIN, but only if no FILE are specified, or if one of FILES is "-".

Default switches may be specified in ACK_OPTIONS environment variable or an .ackrc file. If you want no dependency on the environment, turn it off with --noenv.

Example: ack -i select

-i, --ignore-case     Ignore case distinctions in PATTERN
--[no]smart-case      Ignore case distinctions in PATTERN,only if PATTERN contains no upper caseIgnored if -i is specified
-v, --invert-match    Invert match: select non-matching lines
-w, --word-regexp     Force PATTERN to match only whole words
-Q, --literal Quote all metacharacters; PATTERN is literal

Search output:

--line=NUM  Only print line(s) NUM of each file
-l, -- files-with-matches Only print filenames containing matches
-L, --files-without-match Only print filenames with no match
-o                    Show only the part of a line matching PATTERN (turns off text highlighting)
--passthru  Print all lines, whether matching or not
--output=expr    Output the evaluation of expr for each line (turns off text highlighting)
--match PATTERN Specify PATTERN explicitly.
-m, --max-count=NUM   Stop searching in each file after NUM matches
-1                    Stop searching after one match of any kind
-H, --with-filename   Print the filename for each match
-h, --no-filename     Suppress the prefixing filename on output
-c, --count    Show number of lines matching per file

-A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines.
-B NUM, --before-context=NUM Print NUM lines of leading context before matching lines.
-C [NUM], --context[=NUM] Print NUM lines (default 2) of output context.
--print0      Print null byte as separator between filenames,only works with -f, -g, -l, -L or -c.

File presentation:

--pager=COMMAND Pipes all ack output through COMMAND.Ignored if output is redirected.
--nopager             Do not send output through a pager.  Cancels any setting in ~/.ackrc, ACK_PAGER or ACK_PAGER_COLOR.
--[no]heading        Print a filename heading above each file's results.(default: on when used interactively)
--[no]break           Print a break between results from different files.(default: on when used interactively)
--group      Same as --heading --break
--nogroup   Same as --noheading --nobreak
--[no]color           Highlight the matching text (default: on unless output is redirected, or on Windows)
--[no]colour          Same as --[no]color
--flush               Flush output immediately, even when ack is used non-interactively (when output goes to a pipe or file).

File finding:

-f                    Only print the files found, without searching.The PATTERN must not be specified.
-g REGEX          Same as -f, but only print files matching REGEX.
--sort-files   Sort the found files lexically.

File inclusion/exclusion:

-a, --all-types       All file types searched;Ignores CVS, .svn and other ignored directories
-u, --unrestricted  All files and directories searched
--[no]ignore-dir=name Add/Remove directory from the list of ignored dirs
-n               No descending into subdirectories
-G REGEX     Only search files that match REGEX
--perl    Include only Perl files.
--type=perl   Include only Perl files.
--noperl              Exclude Perl files.
--type=noperl         Exclude Perl files.
See "ack --help type" for supported filetypes.

--type-set TYPE=.EXTENSION[,.EXT2[,...]] Files with the given EXTENSION(s) are recognized as being of type TYPE. This replaces an existing definition for type TYPE.
--type-add TYPE=.EXTENSION[,.EXT2[,...]] Files with the given EXTENSION(s) are recognized as being of (the existing) type TYPE

--[no]follow          Follow symlinks.  Default is off.

Directories ignored by default:

autom4te.cache, blib, _build, .bzr, .cdv, cover_db, CVS, _darcs, ~.dep,, .git, .hg, ~.nib, .pc, ~.plst, RCS, SCCS, _sgbak and .svn

Files not checked for type:

/~$/           -- Unix backup files
/#.+#$/        -- Emacs swap files
/[._].*\.swp$/ -- Vi(m) swap files
/core\.\d+$/   -- core dumps

Sponsored Link

You may also like...

7 Responses

  1. Looks Good…

    Thanks for sharing

    @rungss on Twitter

  2. Greg says:

    What’s the difference with “find FIND_OPTIONS -print0 | xargs -0 grep GREP_OPTIONS” ?

  3. Rich McA says:

    Greg, the normal find | xargs grep pipe doesn’t have the smarts of ack-grep, it will look through version control files, object files, etc. Sometimes that’s what you want, but if you’re just looking for the place in the big source tree that a particular message is emitted, ack-grep is going to be closer to what you need.

    What I can’t figure out is if there is a combination of options that will get the “normal” grep -n output of ::. I’d like that so I could use the emacs “M-x grep” command to get the hits in a buffer and walk through them with “C-x `”.

    I guess I could use “ack-grep -l $PATTERN | xargs grep -n /dev/null $PATTERN” but that seems wasteful.

  4. Rich McA says:

    Eek, got blindsided by unexpected html-ish processing. above should read

    … “normal” grep -n output of &lt.filename&gt.:&lt.linenumber&gt.:&ln.line contents&.gt.

  5. sebg says:

    you should use code tags or fixed width font for usage detail, it’s quite unreadable in current state. It’s a pity as this command seems very useful (nice find !!)

  6. admin says:


    Thanks for your comment and i have updated article with good format

  7. sebg says:

    you should really consider fixed width font (or suppress “ack-grep –help” output to replace it with examples), because variable width font destroys all the beautiful lined up comments about each option 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *