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.
Try 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 PipelinePractice on your own: Terminal playground