Grep With Flags
Part of the Fundamentals section of Coddy's Terminal journey — lesson 35 of 82.
grep becomes even more powerful when you add flags to control how it searches and what it displays.
Most useful grep flags:
-i — case insensitive search. Matches regardless of uppercase or lowercase:
grep -i "error" server.log # matches error, Error, ERROR-n — show line numbers alongside each matching line:
grep -n "error" server.log
3:error: disk full
6:error: connection timeout-c — show only the count of matching lines, not the lines themselves:
grep -c "error" server.log
2-v — invert the search — show lines that do not match the pattern:
grep -v "info" server.log
error: disk full
warning: high memory usage
error: connection timeout-r — search recursively through all files in a directory:
grep -r "error" logs/ # search all files inside the logs folder-l — show only the filenames that contain a match, not the matching lines:
grep -l "error" *.logCombining flags:
You can combine multiple flags together:
grep -in "error" server.log # case insensitive + show line numbers
grep -vc "info" server.log # invert + count non-info linesChallenge
EasyUse grep with flags to inspect server.log in three different ways:
- Find all lines containing
errorand show their line numbers using-n - Count how many lines contain
infousing-c - Show all lines that do not contain
infousing-v
Hint: Run all three commands one after the other. Each flag changes how
greppresents its results.
Cheat sheet
Use flags to control how grep searches and displays results.
-i — case insensitive search:
grep -i "error" server.log # matches error, Error, ERROR-n — show line numbers:
grep -n "error" server.log-c — show count of matching lines:
grep -c "error" server.log-v — invert search (show non-matching lines):
grep -v "info" server.log-r — search recursively through directories:
grep -r "error" logs/-l — show only filenames containing matches:
grep -l "error" *.logFlags can be combined:
grep -in "error" server.log # case insensitive + line numbers
grep -vc "info" server.log # invert + countTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline