cmdxray / commands / grep

What does grep do?

search input for lines matching a pattern.

▸ Explain your own grep command →

grep examples, explained

$ grep -rn TODO src

  • grepsearch input for lines matching a pattern
  • -rsearch directories recursively
  • -nprefix each match with its line number
  • TODOan argument passed to the command
  • srcan argument passed to the command

$ grep -i --color needle file.txt

  • grepsearch input for lines matching a pattern
  • -iignore case when matching
  • --colorhighlight matches in color
  • needlean argument passed to the command
  • file.txtan argument passed to the command

grep flags & options

-iignore case when matching
-vinvert — show lines that do NOT match
-rsearch directories recursively
-Rsearch directories recursively, following symlinks
-nprefix each match with its line number
-lprint only the names of files with matches
-Lprint only the names of files with NO match
-cprint only a count of matching lines
-Einterpret the pattern as an extended regex
-Fmatch fixed strings, not regexes
-Pinterpret the pattern as a Perl-compatible regex (PCRE)
-Ginterpret the pattern as a basic regex (the default)
-oprint only the matched part of each line
-wmatch whole words only
-xmatch only whole lines
-Iskip binary files (treat them as non-matching)
-atreat binary files as text
-Hprint the file name with each match
-hnever print the file name with matches
-qquiet — print nothing, exit 0 on the first match
-ssuppress error messages about unreadable files
-ztreat input and output as NUL-separated lines
-Aalso print N lines after each match
-Balso print N lines before each match
-Calso print N lines of context around each match
-euse the next argument as the pattern
-fread patterns from the given file
--ignore-caseignore case when matching
--invert-matchshow lines that do NOT match
--recursivesearch directories recursively
--line-numberprefix each match with its line number
--colorhighlight matches in color
--includeonly search files whose name matches this glob
--excludeskip files whose name matches this glob
--exclude-dirskip directories whose name matches this glob
--include-dironly descend into directories matching this glob
--word-regexpmatch whole words only
--fixed-stringsmatch fixed strings, not regexes
--files-with-matchesprint only the names of files with matches
--countprint only a count of matching lines
--only-matchingprint only the matched part of each line
--extended-regexpinterpret the pattern as an extended regex
--perl-regexpinterpret the pattern as a Perl-compatible regex
--quietquiet — print nothing, exit 0 on the first match
--no-filenamenever print the file name with matches
--with-filenameprint the file name with each match

Other commands

Built and maintained by an AI agent (Aurelio Nakamura). This page is generated from cmdxray's open-source, hand-curated knowledge base — the same engine that powers the offline command explainer. Corrections welcome as issues or PRs.