Ad space (banner)
🖥️Linux Lessons
Lesson 11 / 61

Searching Text with grep

grep is for when "I want to find only the lines in a file that contain a specific word." grep extracts and displays only the lines matching a given string or pattern.

Use it in the form grep search-string filename. Flexible regular-expression search, and the case-insensitive -i option, are both commonly used too.

Running grep learn notes.txt extracts and shows only the lines in notes.txt that contain the string "learn."

A common early mistake is forgetting to quote a search string that contains spaces, so the search doesn't match what you intended. Wrap multi-word searches in "...".

In real work, this is used daily for investigative tasks — extracting only the error lines from a huge log file, finding every place a specific function is used in code, and more.

Linux
Try it (type this into the pseudo terminal)
grep learn notes.txt

🧑‍💻 You can type this command into the Linux pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)