AI Hints
Linux · Lesson 43 of 120

grep: Searching Inside Files

Find lines that match.

⏱ 2 min read▶ Video at 55:07🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Find searches for files. But often you need to search inside files, for a word, an error, or a name. That's the job of grep. It's one of the most loved commands in Linux.

grep: Searching Inside Files
From the video: grep: Searching Inside Files
  • find searches for files
  • grep searches inside them
  • One of the most loved commands

Find the lines

Grep takes a word to look for, and a file. It prints every line that contains that word, and nothing else. Grep Linux notes dot TXT finds one line. Grep ERROR, in the server log, prints every error the server had. Out of sixty lines, just the seven that matter.

Terminal
grep Linux notes.txt
Learn Linux
grep ERROR logs/server.log
2026-01-05 09:00:23 ERROR priya failed /home
2026-01-05 09:07:00 ERROR jo failed /shop
2026-01-05 09:09:18 ERROR priya failed /home
2026-01-05 09:11:08 ERROR priya failed /home
2026-01-05 09:17:21 ERROR alex failed /shop
2026-01-05 09:17:56 ERROR alex failed /home
2026-01-05 09:23:28 ERROR priya failed /cart

Searching for a phrase

If what you're searching for has spaces, put it in quotes. Here we look for slow page, to see which pages were slow. Without quotes, grep would think page was a file name.

Terminal
grep "slow page" logs/server.log
2026-01-05 09:03:58 WARN jo slow page /cart
2026-01-05 09:04:48 WARN alex slow page /home
2026-01-05 09:07:18 WARN alex slow page /shop
2026-01-05 09:08:10 WARN alex slow page /cart
2026-01-05 09:13:40 WARN alex slow page /home
2026-01-05 09:14:26 WARN sam slow page /cart
2026-01-05 09:15:50 WARN alex slow page /about
2026-01-05 09:19:32 WARN priya slow page /cart
2026-01-05 09:26:18 WARN jo slow page /home

Where the name comes from

The name grep comes from an old editor command, global regular expression print. In plain words, search everywhere for a pattern, and print the matches. The pattern can be a simple word, like we've used, or something much more clever, which we'll touch on later.

keyvalue
gglobal: everywhere
reregular expression: a pattern
pprint the matches
Todaya simple word is enough

Summary

Grep prints the lines of a file that contain a word. Use quotes for phrases. Next, options that make grep even more useful.

Key takeaways
  • grep word file
  • Quotes for phrases
  • Only matching lines
Next up: grep Options — -i, -n, -c, -v and -r