A fast, grep-like command-line tool written in Rust. Searches one or more files for a pattern and prints matching lines with colored highlights. Supports regex, recursive search, parallel processing, and most common grep flags.
- Colored output — matches highlighted in red; disabled automatically when piping
- Regex support (
-E) — full regular expression engine via theregexcrate - Case-insensitive search (
-iorIGNORE_CASEenv var) - Invert match (
-v) — print lines that do NOT match - Whole-word match (
-w) — match only complete words - Context lines (
-C N) — show N lines before/after each match - Count mode (
-c) — print match count per file instead of lines - Files-with-matches (
-l) — print only file names that contain a match - Max-count (
-m N) — stop after N matches per file - Recursive search (
-r) — walk directories; auto-uses current dir if no path given - Glob filters (
--include/--exclude) — filter files by name pattern - stdin support — reads from stdin when no files are provided
- Binary file safety — non-UTF-8 files are silently skipped
- Parallel processing — multiple files searched concurrently via Rayon
- Progress bar — shown when processing many files (stderr, auto-hidden in pipes)
- Correct exit codes —
0matches found,1no matches,2error
cargo build --release
# binary is at ./target/release/minigrepminigrep [OPTIONS] <PATTERN> [FILE]...
If no files are given, reads from stdin (unless -r is set, which defaults to .).
| Flag | Long form | Description |
|---|---|---|
-i |
--ignore-case |
Case-insensitive search |
-E |
--regexp |
Treat pattern as a regular expression |
-v |
--invert-match |
Print lines that do NOT match |
-w |
--word-regexp |
Match whole words only |
-c |
--count |
Print match count per file |
-l |
--files-with-matches |
Print only file names with matches |
-m N |
--max-count N |
Stop after N matches per file |
-C N |
--context N |
Show N lines of context around matches |
-r |
--recursive |
Recurse into directories |
--include GLOB |
Include only files matching this name glob | |
--exclude GLOB |
Exclude files matching this name glob | |
--color WHEN |
Color output: auto (default), always, never |
|
-h |
--help |
Print help |
| Variable | Description |
|---|---|
IGNORE_CASE |
Set to any value to enable case-insensitive mode |
# Basic search
minigrep "fn main" src/main.rs
# Regex: find all function definitions
minigrep -E "^pub fn \w+" src/lib.rs
# Case-insensitive, whole words only
minigrep -i -w "error" src/lib.rs
# Recursive search through all .rs files
minigrep -r --include "*.rs" "TODO" src/
# Recursive from current directory
minigrep -r "panic"
# Show 2 lines of context around each match
minigrep -C 2 "unwrap" src/main.rs
# Count matches per file
minigrep -c "fn " src/main.rs src/lib.rs
# Print only file names that contain matches
minigrep -l "unsafe" src/
# Stop after first match per file
minigrep -m 1 "error" src/main.rs src/lib.rs
# Invert: lines that don't contain "ok"
minigrep -v "ok" results.txt
# Read from stdin
cat poem.txt | minigrep "hope"
# Disable color for piping
minigrep --color=never "fn" src/main.rs | wc -l
# Env var for case-insensitive mode
IGNORE_CASE=1 minigrep "hello" file.txtNormal mode:
<file>:<line>: <content with match highlighted>
Context mode (: for match lines, - for context lines, -- between groups):
src/lib.rs:23: pub fn search(
src/lib.rs-24- re: &Regex,
--
src/lib.rs:47: pub fn search_with_context(
Count mode:
src/main.rs: 5
src/lib.rs: 12
Files-with-matches mode:
src/main.rs
src/lib.rs
| Code | Meaning |
|---|---|
0 |
At least one match found |
1 |
No matches found |
2 |
Error (bad pattern, unreadable file, etc.) |
| Crate | Purpose |
|---|---|
clap |
CLI argument parsing |
colored |
Terminal color output |
regex |
Regular expression engine |
rayon |
Parallel iteration over files |
walkdir |
Recursive directory traversal |
globset |
Glob pattern matching for --include/--exclude |
indicatif |
Progress bar during multi-file scan |
is-terminal |
Detect whether stdout/stderr is a TTY |
cargo test