Grep Basics
Part of the Fundamentals section of Coddy's Terminal journey — lesson 34 of 82.
grep stands for Global Regular Expression Print. It is one of the most powerful and commonly used terminal commands — it searches through files and prints every line that contains a matching pattern.
We have server.log:
info: server started
info: listening on port 8080
error: disk full
warning: high memory usage
info: request received
error: connection timeout
info: server shutdown
The basic format is:
grep "pattern" filenameFor example, to find every line that contains the word error:
grep "error" server.logOutput:
error: disk full
error: connection timeoutOnly the lines containing error are printed. All other lines are hidden.
Searching in multiple files:
grep "error" *.log # search all .log filesSearching for a phrase with spaces:
grep "connection timeout" server.logCase sensitivity:
By default, grep is case sensitive. So searching for error will not match Error or ERROR:
grep "error" server.log # matches: error
grep "Error" server.log # matches: Error
grep "ERROR" server.log # matches: ERRORChallenge
BeginnerUse grep to search through server.log and print only the lines that contain the word error.
Hint: The format is
grep "pattern" filename. Remember thatgrepis case sensitive — use lowercaseerror.
Cheat sheet
The grep command searches through files and prints lines that match a pattern.
Basic syntax:
grep "pattern" filenameExample:
grep "error" server.logSearch multiple files:
grep "error" *.logSearch for phrases with spaces:
grep "connection timeout" server.logCase sensitivity:
grep is case sensitive by default:
grep "error" server.log # matches: error
grep "Error" server.log # matches: Error
grep "ERROR" server.log # matches: ERRORTry 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