grep
find the lines that matchgrep reads its input line by line and prints only the lines that contain the pattern. The pattern can be a plain word or a regular expression, and the input can be a file, a whole directory tree, or the output of another command.
A log has ten thousand lines; grep hands you the twelve that matter.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
Line 14 and line 57 of app.log contain "error". The search is case-sensitive, so "Error" and "ERROR" were not matched.
Switches can be joined: -ic is -i and -c. Five lines match once upper and lower case are treated alike — three more than before.
Every file under src/ is read. Each match is prefixed with the file it was found in, then the line number.
The pipe feeds ps into grep. -v inverts a match: it drops the line for the grep process itself, which would otherwise match "nginx" too.
Switches
A switch changes how the command behaves. These are the ones used day to day; man grep lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-i- Ignore case: match upper and lower case alike.
-v- Invert: print the lines that do not match.
-n- Prefix each match with its line number.
-r- Recursive: search every file under a directory.
-c- Count: print how many lines matched, not the lines themselves.
-l- List only the names of the files that contain a match.
-w- Match whole words only, so "log" does not match "login".
-E- Extended regular expressions: |, + and ? work without backslashes.
-A NUM- Also print NUM lines after each match. -B is before, -C is both.
-o- Print only the part of the line that matched.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
grep -v "^#" /etc/ssh/sshd_config | grep -v "^$"Shows a configuration file without its comments and blank lines.
grep -rl "password" /etc 2>/dev/nullLists only the names of the files that mention a word.
grep -E "error|warn|fail" app.logMatches any of several words, using an extended regular expression.
grep -C 2 "timeout" app.logPrints two lines of context before and after each match.
In cybersecurity
grep is how logs are read in practice — by defenders looking for an intrusion and by intruders looking for secrets.
"grep 'Failed password' /var/log/auth.log" lists failed SSH logins; piping it on to sort and uniq -c shows which addresses are guessing.
A recursive grep for "password", "secret" or "BEGIN PRIVATE KEY" is the first thing run on a compromised host — and worth running on your own code before it is published.
A check built on grep can be dodged by different case, spacing or encoding. Good for finding things; weak as a security control.