grep searches text and prints only the lines that contain a pattern:
grep INFO logs/server.logMatches are highlighted in red. grep is case-sensitive, so grep error misses lines that say ERROR. That's what the options are for:
| Option | Does |
|---|---|
-i | ignore case: error, Error and ERROR all match |
-n | show line numbers |
-c | count the matching lines instead of printing them |
-v | invert: print the lines that don't match |
-w | whole words only |
grep -in error logs/server.loggrep -c WARN logs/server.logSeveral files, and pipes
Give grep several files (or a wildcard) and it puts each file's name in front of its matches:
grep flour recipes/*.txtgrep is also the most common filter in pipelines. ls | grep .txt keeps the names with .txt, and grep WARN logs/server.log | wc -l counts warnings the long way.
Put the pattern in quotes when it has spaces or special characters: grep "not found" logs/server.log.