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
| Option | Description |
|---|---|
| - | |
-i | Ignore case |
-v | Invert match |
-w | Match whole words |
-Q | Literal string (no regex) |
Output Control
| Option | Description |
|---|---|
-n | Show line numbers |
-H | Always show filenames |
--no-filename | Hide filenames |
-c | Count matches |
-l | List filenames with matches |
-L | List filenames without matches |
-o | Show only matching text |
-m NUM | Stop after NUM matches |
Context Control
| Option | Description |
|---|---|
| -- | -- |
-A NUM | Show lines after match |
-B NUM | Show lines before match |
-C NUM | Show lines around match |
File Type Filters
ack provides built-in file type filtering, which makes it powerful for codebases.
| Option | Description |
|---|---|
| -- | - |
--php | Search PHP files only |
--css | Search CSS files only |
--js | Search JavaScript files only |
--html | Search HTML files only |
--xml | Search XML files only |
--ignore-dir=DIR | Exclude directory |
--ignore-file=GLOB | Exclude matching files |
--type-add | Define new file type |
-f | Print filenames only (no content search) |
Output & Display Control
| Option | Description |
|---|---|
| -- | -- |
--color | Highlight matches |
--no-color | Disable highlighting |
--nopager | Do not pipe to pager |
--pager=COMMAND | Use custom pager |
--version | Show version |
--help | Show 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
2. Case-insensitive log search
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
10. Literal search
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
-lfor file discovery before deeper analysis. - Exclude heavy directories (
vendor,node_modules) with--ignore-dir. - Use
-Qfor literal searches. - Combine with context options (
-C) for debugging. - Use
-fwhen you only need file lists.
Troubleshooting Matrix
| Problem | Cause | Solution |
|---|---|---|
| - | - | |
| No results | Case mismatch | Add -i |
| Too many results | Searching all file types | Use file type filters |
| Slow search | Large directory tree | Use --ignore-dir |
| Unexpected regex behavior | Pattern interpreted as regex | Use -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"