Skip to content

Search: grep & rg

grep and rg for pattern matching and recursive search.


Search for patterns in files or filter pipeline objects by matching against their text representation.

grep [OPTIONS] PATTERN [FILE...]

grep operates in two distinct modes depending on how you call it:

When objects are piped in and no file arguments are given, grep matches against each object’s BashText representation and emits the original typed object on match.

Terminal window
ls -la | grep '.ps1'
-rw-r--r-- 1 you you 141234 Apr 2 10:30 PsBash.psm1
-rw-r--r-- 1 you you 28400 Apr 2 10:28 PsBash.Tests.ps1

The output looks like text, but each result is still an LsEntry object:

Terminal window
$result = ls -la | grep '.ps1'
$result[0].GetType().Name # PSCustomObject (LsEntry type)
$result[0].Name # "PsBash.psm1"
$result[0].SizeBytes # 141234
$result[0].Permissions # "-rw-r--r--"

This is the pipeline bridge pattern: grep reads .BashText for matching but passes through the original object unchanged. Types survive the entire pipeline:

Terminal window
$big = ls -la | grep '.ps1' | sort -k5 -rn | head 1
$big[0].SizeBytes # 141234 -- still a real integer
FlagDescription
-iCase-insensitive matching
-vInvert match (select non-matching lines)
-nPrefix each line with its line number
-cPrint count of matching lines only
-rRecursive search through directories
-lPrint only names of files with matches
-EExtended regular expressions (ERE syntax)
-A NUMShow NUM lines after each match
-B NUMShow NUM lines before each match
-C NUMShow NUM lines before and after each match

In file mode, each result is a PsBash.GrepMatch object:

PropertyTypeDescription
FileNamestringAbsolute path to the matched file
LineNumberint1-based line number of the match
LinestringFull text of the matched line
BashTextstringFormatted output string (includes filename/line number prefixes)

Case-insensitive search:

Terminal window
grep -i 'error' /var/log/app.log

Regex with extended syntax:

Terminal window
grep -E 'warn|error|fatal' server.log

Inverted match (exclude lines):

Terminal window
cat config.yml | grep -v '^#'

Count matches per file:

Terminal window
grep -rc 'import' src/
src/main.ps1:3
src/utils.ps1:1

Context lines around matches:

Terminal window
grep -B2 -A2 'throw' src/PsBash.psm1

List files containing a pattern:

Terminal window
grep -rl 'Invoke-BashGrep' src/

Recursive search from a directory:

Terminal window
grep -r 'function' src/

Pipeline bridge with further processing:

Terminal window
# Find large .log files -- types preserved through grep
$logs = ls -la | grep '\.log'
$logs | ForEach-Object { $_.SizeBytes } | Measure-Object -Sum

Recursive search across files, modeled after ripgrep. Searches recursively by default, skips .git directories, and excludes dotfiles unless --hidden is specified.

rg [OPTIONS] PATTERN [PATH...]
FlagDescription
-iCase-insensitive matching
-wMatch whole words only (wraps pattern in \b...\b)
-cPrint count of matching lines only
-lPrint only names of files with matches
-nShow line numbers (default)
-NSuppress line numbers
-oPrint only the matched portion of each line
-vInvert match (select non-matching lines)
-FTreat pattern as a fixed string, not a regex
-g GLOBFilter files by glob pattern (e.g., -g '*.ps1')
-A NUMShow NUM lines after each match
-B NUMShow NUM lines before each match
-C NUMShow NUM lines before and after each match
--hiddenInclude dotfiles and hidden directories

Each result is a PsBash.RgMatch object:

PropertyTypeDescription
FileNamestringAbsolute path to the matched file
LineNumberint1-based line number of the match
LinestringFull text of the matched line
BashTextstringFormatted output string (includes filename/line number prefixes)

Search the current directory recursively:

Terminal window
rg 'function'
src/PsBash.psm1:7:function Get-BashPlatform {
src/PsBash.psm1:20:function New-BashObject {
src/PsBash.psm1:43:function ConvertFrom-BashArgs {

Filter by file type with glob:

Terminal window
rg -g '*.ps1' 'Import-Module'

Case-insensitive whole-word search:

Terminal window
rg -iw 'error'

Fixed string search (no regex interpretation):

Terminal window
rg -F '$_.Name'

Only show matched text:

Terminal window
rg -o '\d+\.\d+\.\d+' package.json
package.json:3:0.1.0

Context lines around matches:

Terminal window
rg -C3 'throw' src/

Count matches per file:

Terminal window
rg -c 'function' src/
src/PsBash.psm1:69
src/Help.psm1:2

List files containing a pattern:

Terminal window
rg -l 'Invoke-Bash'

Include hidden files:

Terminal window
rg --hidden 'alias' .

Suppress line numbers for clean output:

Terminal window
rg -N 'version' package.json
greprg
RecursiveRequires -r flagRecursive by default
DotfilesIncludes everything with -rExcludes by default, --hidden to include
.git directoryIncluded with -rAlways skipped
Glob filteringNot supported-g '*.ext'
Only-matchingNot supported-o flag
Fixed stringsNot supported-F flag
Word boundariesNot supported-w flag
Line numbersOff by default, -n to enableOn by default, -N to disable
Pipeline bridgePasses original objectsPasses original objects