Grep Command in Linux: Search Text Files

Grep Command in Linux: Search Text Files

A log file can hold more lines than you can scan by eye when you need to find an error message. grep is a command-line tool that searches text and prints the lines that match.

I’ll show you how to search a file with grep and read its matches and exit status.

TL;DR: Search for Text with grep

grep prints each input line that matches a search expression, so you can search a file or filter output from another command.

grep -n -i 'error' service.log
  • Use -F for literal text and -E for extended regular expressions.
  • Add -i to ignore letter case and -n to show line numbers.
  • Status 0 means a match, 1 means no match, and 2 means an error.

What Is the grep Command?

grep is a command-line search tool that selects whole lines containing a search expression. With a filename, it scans that file.

The basic form is grep [options] expression [file…]. It prints complete matching lines. Regular expressions are the default, so a period can have special meaning.

Use -F for literal text or -E for operators such as the vertical bar in grep -E ‘error|failed’ app.log.

How to Use grep in Linux Step by Step

Start with a readable file and a short search term, then add only the options needed. I use a small log file here to make case-insensitive matching and line numbers visible.

Step 1: Search one file

Give grep a quoted search term followed by a filename.

grep 'error' service.log

This prints lines containing lowercase error. grep writes selected lines to standard output, leaving the original file unchanged.

To restrict a search to a whole word, use -w. That keeps a search for cat from selecting a line containing catalog.

Step 2: Add line numbers and ignore case

Use -n to prefix each line with its 1-based number and -i to ignore case. I ran this exact command against service.log:

grep -n -i 'error' service.log

The exact output was:

1:error: disk full
4:ERROR: connection refused
Terminal output showing grep matching error lines and their line numbers

The line numbers identify each match, and -i includes both uppercase and lowercase forms.

Step 3: Search several files or a directory tree

List filenames after the search term to scan several files.

A filename wildcard such as *.log is expanded by the shell before grep runs. This is useful for a group of logs, but a very broad wildcard can include files you did not intend to search. Narrow the file expression or name the paths directly when accuracy matters.

grep -n 'timeout' app.log worker.log
grep -R -n 'timeout' logs/

Recursive mode descends into directories, so choose a specific starting location. Use the Linux find command to locate paths by name before searching their contents.

Step 4: Filter command output

A pipe sends one command’s standard output to grep as standard input. The example selects listing lines that begin with d, which marks directory entries in long listing format.

ls -l | grep '^d'

This searches text produced by ls, not file contents. See the ls command guide for listing files.

Step 5: Choose the output you need

Choose an option based on what you need to inspect.

OptionEffect
-wMatch a whole word
-vSelect lines that do not match
-cCount matching lines per file
-lPrint names of files with a match

-c counts matching lines rather than every occurrence. Use -o when you need only the matched text.

The context options help when a matching line needs surrounding information. -A shows following lines, -B shows preceding lines, and -C shows context on both sides. If nearby matches overlap, grep avoids printing a line twice.

grep Results, Errors, and Useful Next Steps

grep’s exit status distinguishes a match, no match, and an error. Status 1 can be a normal result when a search finds nothing.

StatusMeaningWhat to check
0At least one line matchedReview the selected lines
1No line matchedCheck spelling, case, path, and expression type
2An error occurredCheck the filename, permissions, and diagnostic message

If a pipeline is still running, Ctrl+C interrupts it. grep exits on its own when the search completes.

A missing or unreadable file produces an error diagnostic. GNU-specific options may not exist in every implementation, so check the local manual with man grep.

Conclusion: Read the Match and the Exit Status

Begin with a quoted search term and a file, then add options such as -n or -i when needed.

For the full option list and regular-expression details, consult the GNU grep manual or run man grep on your system.

FAQ

These answers cover two common questions about using grep in a shell.

What does grep do in Linux?

grep searches text input for a pattern and prints the lines that match it.

How do I search without matching letter case?

Add the -i option, as in grep -i ‘error’ file.log.

How do I stop a grep command that is still running?

Press Ctrl+C to interrupt the foreground command. A grep search that has completed exits on its own.