When you work with Linux, you often need to find a specific line inside a file without opening it or scrolling through pages of text. This is exactly what the grep command is designed to do.
In this guide, you’ll learn how to use grep step by step in the terminal. We’ll start with the simplest working version and build confidence gradually. No shortcuts, no assumptions, and no unnecessary details.
What grep Does
The grep command searches for text inside files.
You give it:
-
A word or phrase to look for
-
A file to search
It then shows only the lines that match. That’s all you need to know to get started.
Creating a Simple File to Search
Before using grep, it helps to have a small file to work with so the behavior is easy to see.
Run this command:
printf "web01 running\nweb02 stopped\ndb01 running\nbackup01 running\n" > servers.txt
This creates a file called servers.txt and fills it with sample data.
You won’t see any output. In Linux, many commands stay silent when they succeed.
The Simplest grep Command
The basic structure of grep looks like this:
grep "word" filename
Now try it for real:
grep "running" servers.txt
Output:
web01 runningdb01 runningbackup01 running
grep scanned the file line by line and printed only the lines that contain the word running. Everything else was ignored.
This is useful when checking server lists or status files without opening them.
When No Matches Are Found
If grep doesn’t find a match, it stays quiet.
grep "offline" servers.txt
There is no output. This is expected behavior, not an error. Many scripts rely on this silence to confirm that something is missing.
Case Sensitivity in Linux
By default, grep is case sensitive. On Linux, uppercase and lowercase letters are treated as different characters.
grep "Running" servers.txt
This produces no output because the file contains lowercase running.
Ignoring Case Safely
To ignore case differences, add the -i option:
grep -i "Running" servers.txt
Output:
web01 runningdb01 runningbackup01 running
This makes searching safer when you’re unsure how text is formatted, especially in log files.
Showing Line Numbers
Sometimes you want to know where a match appears in a file. The -n option adds line numbers.
grep -n "running" servers.txt
Output:
1:web01 running3:db01 running4:backup01 running
The number before the colon shows the line number, which is helpful when opening the file in an editor.
Common Beginner Mistakes
Forgetting the filename
grep "running"
When starting out, grep always needs a filename.
Confusion about quotes
Quotes are the safest option, especially when searching for words with spaces or special characters:
grep "running" servers.txt
Thinking silence means failure
In Linux, no output often means success. It simply means nothing matched.
A Practical Real-World Example
A common use of grep is checking log files for specific messages. On many systems, logs live in the /var/log directory.
grep -i "started" /var/log/syslog
This quickly confirms whether a service reported that it started. If the file doesn’t exist or requires extra permissions, that can be handled later.
A Small Next Step
Once the basics feel comfortable, you can search multiple files at once:
grep "running" *.txt
*.txt means all files ending in .txt in the current directory. This is handled by the shell, not by grep itself. Don’t move past this until the core command feels natural.
Conclusion
grep is one of the most useful tools in Linux, and you don’t need to master everything at once. Start with simple searches, pay attention to the output, and practice a few times.
With repetition, this command will quickly feel natural and reliable.