Where to start First steps track · 15 stages
Linux Labs

grep — searching text

cheat sheet · GNU grep https://lab.dimsanych.ru/en/wiki/grep/

Cheat sheet

  • grep -i word file case-insensitive
  • grep -rn word dir/ recursive + line numbers
  • grep -v word file lines without the word
  • grep -C2 word file ±2 lines of context
  • grep -rn --include='*.conf' word /etc
  • grep -oP 'regex' file match only, Perl regex
  • cmd | grep -F "literal" a literal string

Options

Flag Meaning
-i case-insensitive
-r / -R recursive through directories (R also follows symlinks)
-n show line numbers
-v invert (lines WITHOUT the pattern)
-c count matches
-l / -L only filenames with a match / without one
-w / -x a whole word / a whole line
-E / -F / -P extended regex / a fixed string / Perl regex
-A N/-B N/-C N N lines after / before / around
-o only the matched part
--include/--exclude filter filenames with -r
Loading…