grep
Print every line that matches a pattern, with flags to search a whole tree, ignore case, count, or show the lines around each match.
grep -rn "<pattern>" <directory>Angle brackets are yours to fill in. Linux, macOS.
Examples for grep
Search for text in every file in a directory
grep -rn "<text>" .-r searches the current directory and everything under it; -n adds the line number so you can open the file at the right place. Add -i to ignore case.
greptool-rnrecursive, with line numbers"<text>"argument .here and belowSearch without matching case
grep -i "<text>" <file>Error, error and ERROR all match. Without -i, grep matches the case exactly.
greptool-iignore case"<text>"argument<file>pathList only the files that contain a match
grep -rl "<text>" .Prints each matching file once, without the lines. Useful to pass to another command, such as an editor or sed.
greptool-rlrecursive, file names only"<text>"argument .pathShow the lines that do not match
grep -v "<text>" <file>Removes every matching line from the output. Often used to filter noise out of a log or another command's output.
greptool-vinvert the match"<text>"argument<file>pathShow the lines around each match
grep -C 3 "<text>" <file>Prints 3 lines before and after each match, with -- between separate groups. Use -A 3 for only the lines after, as with a stack trace below an error.
greptool-C 33 lines of context"<text>"argument<file>pathSearch only one type of file and skip node_modules
grep -rn --include="*.ts" --exclude-dir=node_modules "<text>" .--include limits the search to file names that match the glob; quote it so the shell does not expand it first. --exclude-dir skips a directory by name at any depth.
greptool-rnflag--include="*.ts"only .ts files--exclude-dir=node_modulesflag with value"<text>"argument.Match one of several words
grep -E "error|warning" <file>With -E, | means or. Without it, grep looks for the literal text error|warning. grep -e error -e warning does the same without a regular expression.
greptool-Eextended regex"error|warning"either word<file>pathFilter another command's output
ps aux | grep <name>grep reads from the pipe when you give it no file. The grep process itself often appears in the result, because its own command line contains <name>.
ps auxtool|pipegreptool<name>argumentCount the matching lines
grep -c "<text>" <file>Prints a number instead of the lines. It counts lines, not matches: a line that contains the text twice counts once.
greptool-ccount lines"<text>"argument<file>path
Flags
- -r --recursive
- Search every file under a directory.
- -i --ignore-case
- Match upper and lower case the same.
- -n --line-number
- Print the line number before each match.
- -l --files-with-matches
- Print only the names of files that contain a match.
- -v --invert-match
- Print the lines that do not match.
- -w --word-regexp
- Match only whole words, so cat does not match category.
- -c --count
- Print how many lines match, per file, instead of the lines.
- -E --extended-regexp
- Use extended regular expressions, where | + ? and () work without backslashes.
- -F --fixed-strings
- Treat the pattern as plain text, so dots and brackets are not special.
- -C --context <n>
- Show <n> lines before and after each match. -A is only after, -B only before.
- --include <glob>
- With -r, search only files whose names match <glob>, such as *.ts.
- --exclude-dir <dir>
- With -r, skip directories with this name, such as node_modules.