Search files and command output fast — recursively, case-insensitively, with regex. Tap any command to copy it.
grep 'pattern' file.txtFind lines matching a patterngrep -i 'pattern' file.txtCase-insensitive searchgrep -r 'pattern' .Search recursively through a directorygrep -rn 'pattern' .Recursive, with file names and line numbersgrep -w 'word' file.txtMatch whole words onlygrep -v 'pattern' file.txtShow lines that do NOT matchgrep -c 'pattern' file.txtCount the matching linesgrep -l 'pattern' *.txtList only the files that contain a matchgrep -A 3 'pattern' file.txtShow 3 lines after each matchgrep -B 3 'pattern' file.txtShow 3 lines before each matchgrep -C 3 'pattern' file.txtShow 3 lines around each matchgrep -E 'foo|bar' file.txtExtended regex (alternation, +, ?)grep -o 'pattern' file.txtPrint only the matched text, not the linegrep -P '\d+' file.txtPerl-compatible regex (GNU grep)grep '^start' file.txtLines that begin with 'start'grep 'end$' file.txtLines that end with 'end'ps aux | grep nodeFilter another command's outputgrep -rn 'TODO' --include='*.js'Search only matching file typesgrep -rn 'pattern' --exclude-dir=node_modulesSkip a directory while searchinggrep ("global regular expression print") searches text for lines matching a pattern, and three flags cover most real use. -i makes the search case-insensitive, -r searches recursively through every file in a directory, and -n prints the line number of each match. Combined as grep -rni 'pattern' . you get a fast, forgiving search across a whole project — the command most developers reach for dozens of times a day.
When a search returns too much, a few flags refine it. -w matches whole words only (so "cat" won't match "category"), -v inverts the match to show lines that don't contain the pattern, and -c just counts matches instead of printing them. The context flags -A, -B and -C (after, before, and both) show surrounding lines, which is invaluable when a single matching line isn't enough to understand what's going on.
grep's real power is regular expressions. Anchors like ^ (start of line) and $ (end of line) pin matches to position, -E enables extended regex for things like foo|bar alternation, and -o prints just the matched portion rather than the whole line — handy for extracting values. grep also shines at the end of a pipe: ps aux | grep node filters another command's output to just the lines you care about. For the full pattern syntax, see the regex reference, and pair grep with sed to edit the lines you find.