← Presentation home · Part 5 of 12

Ack - Searching Like A Pro

Part 5 · Ack - Searching Like A Pro

No slide image for this part. Notes below.

← Previous Next →

Deep dive

Ack - Searching Like A Pro

What is ack?

ack is a search tool designed for programmers. It's like grep, but:

- Ignores binary files automatically
- Understands file types (--perl, --python, --js)

Think of it as "grep for code" but it works great for any text search.

Basic Syntax

ack [options] PATTERN [directory]

If you don't specify a directory, ack searches the current directory and all subdirectories.

Your First Search

Let's use our practice folder:

cd ~/regex-practice

Search for a literal word:

ack "Hello"

Output shows the filename, line number, and matching line with the match highlighted.

Case Sensitivity

By default, ack is case-sensitive:

ack "hello" # Won't find "Hello" ack "Hello" # Finds "Hello"

Use -i for case-insensitive search:

ack -i "hello" # Finds "hello", "Hello", "HELLO"

This is your most common flag. Use it often.

Searching for Regex Patterns

ack uses Perl-style regex by default. All those patterns we learned work here:

ack "\d+"           # Lines containing digits
ack "TODO|FIXME"    # Lines with TODO or FIXME

ack "^Error" # Lines starting with "Error"

ack "\.txt$"        # Lines ending with .txt

Remember: you're searching the CONTENTS of files, not filenames.

Finding Files by Content

Some real examples:

Find all files mentioning "error":

ack -i "error"

Find configuration that sets a port:

  ack "port\s*=\s*\d+"

Find TODO comments:

  ack "TODO:"

Find function definitions (very rough):

  ack "function\s+\w+"

Word Boundaries Matter

Without word boundaries:

ack "log"

This matches: log, login, catalog, dialogue, blog...

With word boundaries:

ack "\blog\b"

This matches only "log" as a standalone word.

Useful Output Options

Show only filenames (not the matching lines):

ack -l "pattern"

Count matches per file:

ack -c "pattern"

Show context (lines before/after):

ack -B 2 -A 2 "pattern" # 2 lines before and after ack -C 3 "pattern" # 3 lines before AND after

Show line numbers (on by default, but explicit):

ack -n "pattern"

Inverted Matching

Find lines that DON'T match a pattern:

ack -v "pattern"

Find files that DON'T contain a pattern:

ack -L "pattern"

This is super useful. "Show me all files that don't have X."

Filtering by File Type

ack knows about programming languages:

ack --perl "pattern"      # Only search .pl, .pm, .t files
ack --python "pattern"    # Only .py files
ack --js "pattern"        # Only .js files
ack --html "pattern"      # Only .html files
ack --css "pattern"       # Only .css files
ack --shell "pattern"     # Only .sh, .bash files

See all known types:

ack --help-types

Create custom file type:

ack --type-set=mytype:ext:foo,bar "pattern"

Limiting by Filename Pattern

Search only files matching a pattern:

ack -G "\.txt$" "pattern"     # Only in .txt files

ack -G "config" "pattern" # Only in files with "config" in name ack -G "^test" "pattern" # Only in files starting with "test"

The -G flag takes a regex for the filename.

Combining Options

Options combine naturally:

ack -i -l "error" # Case-insensitive, filenames only ack -i --perl "use strict" # Case-insensitive, Perl files only

ack -c -G "\.log$" "ERROR"  # Count errors in .log files

Quick Examples for AI Workflows

Find files with vision model responses:

ack "NUDE_CHEST|WEAPON"

Find Python files calling an API:

  ack --python "requests\.(get|post)"

Find all image references:

  ack -i "\.(png|jpg|jpeg|gif)\b"

Find files with base64 data:

  ack "[A-Za-z0-9+/]{50,}"

Find lines with timestamps:

  ack "\d{4}-\d{2}-\d{2}"