Topic 2.2
grep and find: Searching Like a Pro
In one line
grep searches INSIDE files for matching text; find searches the FILESYSTEM for matching files — two different kinds of search that together cover almost everything you'll ever need to locate.
Think of it like this
Real-life example: grep is like scanning every page of a book for a specific word; find is like scanning a library's shelves for books matching certain criteria (title, author, size) without opening a single one. Different questions, different tools.
Key ideas
- 01
grep "pattern" file.txtprints every LINE in the file containing that pattern.grep -imakes the search case-insensitive,grep -rsearches recursively through an entire directory tree,grep -nshows line numbers, andgrep -vINVERTS the match — showing only lines that DON'T contain the pattern. - 02
grep -E(oregrep) enables extended regular expressions — real pattern matching beyond a literal string, likegrep -E "error|fail"matching lines containing EITHER word. Regex is a deep topic on its own; knowinggrepaccepts it, and a handful of basics (.,*,|,^,$), covers most real use. - 03
find <path> -name "*.log"searches a directory tree for files matching a name pattern.find <path> -type ffinds only regular files,-type donly directories.find <path> -mtime -1finds files modified in the last day — genuinely useful for 'what changed recently' investigations. - 04
findcan EXECUTE a command on every match:find . -name "*.tmp" -deleteremoves every matching file directly, andfind . -name "*.log" -exec grep -l ERROR {} \;runsgrepagainst every matching file, printing the names of ones that contain 'ERROR' — a genuinely powerful combination worth knowing exists, even if the exact syntax needs a lookup each time. - 05
grep and find combine naturally:
find . -name "*.log" | xargs grep -l ERRORpipes find's file list intoxargs, which then runsgrepagainst each one —xargsis the tool that turns a LIST of things (piped in as text) into ARGUMENTS for another command.
In your stack
- →
A very common real Java debugging move:
grep -rn "NullPointerException" /var/log/myapp/— recursively search every log file in a directory for a specific exception, with line numbers, to quickly find every occurrence across potentially dozens of rotated log files.
Code & diagrams
Try these against real directories on your machine — /etc is a safe, read-only playground.
# grep: search INSIDE files
grep -i "root" /etc/passwd
grep -n "bash" /etc/passwd
grep -v "nologin" /etc/passwd | head -5
# find: search for FILES matching criteria
find /etc -name "*.conf" -type f | head -10
find /etc -type d -name "ssh*"
# Combine them: find files, then search inside each
find /etc -name "*.conf" | xargs grep -l "root" 2>/dev/null | head -5
# find can act directly on matches
find /tmp -name "*.tmp" -type f
# find /tmp -name "*.tmp" -delete # (destructive — only run when you mean it)Explain it without notes
You need to know which files in a project were modified in the last 24 hours. Is that a job for grep or find, and why?
Why would you ever pipe find's output into xargs instead of just using find's own -exec flag?
Practice
Use find to locate every file larger than 10MB somewhere on your system (find / -size +10M -type f 2>/dev/null — the 2>/dev/null hides permission-denied noise from directories you can't read).
Use grep -rn to search an entire project directory for a specific function or variable name and count how many files reference it.
Trade-offs
- ↔
grep and find are fast and universally available on every Linux system with zero setup, but for genuinely large-scale code search across a big repository, purpose-built tools (
ripgrep/rg, an IDE's own search) are meaningfully faster — worth knowing plain grep/find work everywhere as a reliable fallback, even after you've adopted faster specialized tools day to day.
Done when you can
I know grep searches file contents and find searches the filesystem for matching files.
I can use grep with -i, -r, -n, and -v confidently.
I can build a find command that filters by name, type, or modification time.