Skip to main content

Depth and Glob Filtering

Depth Control (--depth)

By default ag searches the entire directory tree. Limit depth for faster, more focused searches:

# Only files directly in /etc (no subdirectories)
ag --depth 0 "pattern" /etc

# Search up to 2 levels deep
ag --depth 2 "listen" /etc/nginx

--depth 0 is equivalent to searching non-recursively.

Path Pattern Glob (-G)

-G filters which files get searched based on a regex applied to the full file path. It is like rg's --glob but uses regex instead of glob syntax.

# Only search files whose path contains "routes"
ag -G "routes" "GET|POST|PUT"

# Only files ending in .conf
ag -G "\.conf$" "server_name"

# Only test files
ag -G "(test|spec)" "mock"

# Exclude minified files (negate with a smart regex)
ag -G "^((?!\.min\.).)*$" "function"

Ignoring Specific Directories (--ignore-dir)

# Skip node_modules (even if not in .gitignore)
ag --ignore-dir=node_modules "pattern"

# Skip multiple directories
ag --ignore-dir=node_modules --ignore-dir=.cache --ignore-dir=dist "pattern"

Ignoring Files by Pattern (--ignore)

# Skip all .lock files
ag --ignore="*.lock" "pattern"

# Skip minified JS
ag --ignore="*.min.js" --ignore="*.bundle.js" "pattern"