Skip to main content

ack — Advanced Code Search Tool

Introduction

ack is a grep-like search tool optimized specifically for searching source code. It is designed for developers and sysadmins who frequently search through structured codebases.

Tool Snapshot
  • Core Function: Developer-focused search tool optimized for source code.
  • Primary Benefit: Built-in file type filters (PHP, JS, CSS) and automatic VCS exclusion.
  • Where to Use: Code audits, searching across WordPress plugins/themes, and web dev.
  • Logic: Perl-compatible regex with code-aware filtering.

Compared to grep, ack:

  • Automatically ignores version control directories (.git, .svn)
  • Skips binary files by default
  • Includes built-in file type filters (PHP, CSS, JS, HTML, XML, etc.)
  • Provides cleaner, code-focused output
  • Requires less manual filtering

ack is particularly useful in WordPress environments where searching across plugins, themes, and custom code is common.

Installation

sudo apt install ack

Verify installation:

ack --version

Syntax & Expression Rules

The command follows a logical structure that reads almost like a sentence:

ack [OPTION] PATTERN [FILE...]
  • [OPTION]: Flags to modify behavior (e.g., --php, -i).
  • [PATTERN]: The text or regex to search for.
  • [FILE]: One or more files or directories to search (defaults to current directory).

Core Search Options

Basic Matching

OptionDescription
-
-iIgnore case
-vInvert match
-wMatch whole words
-QLiteral string (no regex)

Output Control

OptionDescription
-nShow line numbers
-HAlways show filenames
--no-filenameHide filenames
-cCount matches
-lList filenames with matches
-LList filenames without matches
-oShow only matching text
-m NUMStop after NUM matches

Context Control

OptionDescription
----
-A NUMShow lines after match
-B NUMShow lines before match
-C NUMShow lines around match

File Type Filters

ack provides built-in file type filtering, which makes it powerful for codebases.

OptionDescription
---
--phpSearch PHP files only
--cssSearch CSS files only
--jsSearch JavaScript files only
--htmlSearch HTML files only
--xmlSearch XML files only
--ignore-dir=DIRExclude directory
--ignore-file=GLOBExclude matching files
--type-addDefine new file type
-fPrint filenames only (no content search)

Output & Display Control

OptionDescription
----
--colorHighlight matches
--no-colorDisable highlighting
--nopagerDo not pipe to pager
--pager=COMMANDUse custom pager
--versionShow version
--helpShow help

Pattern Behavior

ack uses Perl-compatible regular expressions by default.

Examples of supported features:

  • ^ start of line
  • $ end of line
  • . any character
  • *, +, ?
  • |
  • ()
  • Lookaheads/lookbehinds
  • \d, \w, \s

Use -Q for literal matching if regex interpretation is not desired.

WordPress / VPS Examples

1. Search database credentials

ack "DB_NAME" wp-config.php
ack -i "error" /var/log/nginx/error.log

3. Search only PHP files

ack --php "wp_enqueue_script" wp-content/

4. Search only CSS files

ack --css "color:" wp-content/themes/

5. Search PHP and JavaScript

ack --php --js "ajaxurl" wp-content/

6. List files containing pattern

ack -l "add_action" wp-content/plugins/

7. List files without pattern

ack -L "eval(" wp-content/plugins/

8. Count matches

ack -c "define" wp-config.php

9. Show line numbers

ack -n "DB_USER" wp-config.php
ack -Q "a+b" file.txt

11. Match whole word

ack -w "admin" users.txt

12. Invert match

ack -v "cache" plugins.txt

13. Match beginning of line

ack "^define" wp-config.php

14. OR pattern match

ack "cat|dog" animals.txt

15. Ignore directory

ack "define" wp-content/ --ignore-dir=cache

16. Highlight matches

ack --color "error" error.log

17. Disable color

ack --no-color "error" error.log

18. Malware scan

ack "eval(" wp-content/

19. Search for function definition

ack "function my_custom_hook" wp-content/themes/

20. Show context

ack -C2 "DB_NAME" wp-config.php

21. Show lines after

ack -A2 "DB_USER" wp-config.php

22. Show lines before

ack -B2 "DB_USER" wp-config.php

23. List filenames only (PHP)

ack -f --php

24. Extract URLs only

ack -o "https://[a-zA-Z0-9./]*" index.php

25. Stop after first match

ack -m1 "error" error.log

26. Search piped input

cat plugins.txt | ack "seo"

Benefits

  • Optimized for searching source code
  • Automatically ignores irrelevant directories
  • Built-in file type filters
  • Uses powerful regex by default
  • Cleaner output compared to raw grep

Best Practices

  • Use file type filters (--php, --js, etc.) to narrow scope.
  • Use -l for file discovery before deeper analysis.
  • Exclude heavy directories (vendor, node_modules) with --ignore-dir.
  • Use -Q for literal searches.
  • Combine with context options (-C) for debugging.
  • Use -f when you only need file lists.

Troubleshooting Matrix

ProblemCauseSolution
--
No resultsCase mismatchAdd -i
Too many resultsSearching all file typesUse file type filters
Slow searchLarge directory treeUse --ignore-dir
Unexpected regex behaviorPattern interpreted as regexUse -Q

Cheat Sheet

ack "pattern" file
ack -i "error" log.txt
ack --php "function" dir/
ack -l "pattern" dir/
ack -c "pattern" file
ack -C2 "pattern" file
ack "eval\\(" wp-content/
ack --css "color:" themes/
ack --ignore-dir=cache "test"