Menu
Coddy logo textTech

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" filename

For example, to find every line that contains the word error:

grep "error" server.log

Output:

error: disk full
error: connection timeout

Only the lines containing error are printed. All other lines are hidden.

Searching in multiple files:

grep "error" *.log   # search all .log files

Searching for a phrase with spaces:

grep "connection timeout" server.log

Case 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: ERROR
challenge icon

Challenge

Beginner

Use grep to search through server.log and print only the lines that contain the word error.

Hint: The format is grep "pattern" filename. Remember that grep is case sensitive — use lowercase error.

Cheat sheet

The grep command searches through files and prints lines that match a pattern.

Basic syntax:

grep "pattern" filename

Example:

grep "error" server.log

Search multiple files:

grep "error" *.log

Search for phrases with spaces:

grep "connection timeout" server.log

Case sensitivity:

grep is case sensitive by default:

grep "error" server.log    # matches: error
grep "Error" server.log    # matches: Error
grep "ERROR" server.log    # matches: ERROR

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals