Ubuntu grep Command Complete Guide

Overview

When analyzing large text files or system logs in a Linux environment, the grep command is an essential tool. In this article, we will explore key practical techniques for running grep in the Ubuntu terminal to quickly find desired strings and accurately interpret the output results.

Basic String Search and Case-Insensitive Option (-i)

Run the command mkdir -p ~/grep_demo && cd ~/grep_demo to create and move into an isolated workspace for practicing the grep command.

mkdir -p ~/grep_demo && cd ~/grep_demo

Run the command echo -e 'Ubuntu Linux Server\nubuntu desktop\nDebian GNU/Linux' > sample.txt to create a text file containing sample strings for search testing.

echo -e ‘Ubuntu Linux Server\nubuntu desktop\nDebian GNU/Linux’ > sample.txt

The grep command searches for a specific pattern or string in a file or text and prints the matching lines.
By default, it is strictly case-sensitive, so only lines that match Ubuntu exactly will be retrieved.
⚙️ [Key Options]

-i : Searches for matching patterns while ignoring case distinctions.
-v : Inverts the match to output only lines that do not match the specified pattern.
-n : Displays line numbers along with the matching content.
-c : Counts and outputs only the total number of matching lines.
-r : Recursively searches all files within subdirectories.
-l : Lists only the names of files that contain matching content.
-E : Searches using Extended Regular Expressions (ERE).

grep ‘Ubuntu’ sample.txt

Adding the -i option performs a case-insensitive search.
Therefore, lines containing both Ubuntu starting with an uppercase letter and ubuntu starting with a lowercase letter are all printed.

grep -i ‘ubuntu’ sample.txt

Displaying Line Numbers (-n) and Inverted Search for Non-Matching Lines (-v)

Run the command echo -e 'INFO: Service started\nERROR: Connection timeout\nINFO: Request handled\nERROR: Disk full' > app.log to generate dummy log data for log analysis practice.

echo -e ‘INFO: Service started\nERROR: Connection timeout\nINFO: Request handled\nERROR: Disk full’ > app.log

Using the -n option displays the line numbers of lines matching the search pattern.
The line number and content containing the ERROR string are output together, allowing you to quickly locate where issues occurred.

grep -n ‘ERROR’ app.log

Using the -v option inverts the search to output only lines that do not match the specified pattern.
You can filter out normal-status INFO entries and examine only error-related lines.

grep -v ‘INFO’ app.log

Recursive Search in Subdirectories (-r) and Outputting File Names Only (-l)

Run the command mkdir -p ~/grep_demo/logs/archive to create a subdirectory archive structure for recursive search practice.

mkdir -p ~/grep_demo/logs/archive

Run the command echo 'database error' > ~/grep_demo/logs/db.log to create a database error log file for search testing.

echo ‘database error’ > ~/grep_demo/logs/db.log

Run the command echo 'cache hit' > ~/grep_demo/logs/cache.log to create a cache log file for verifying normal logs.

echo ‘cache hit’ > ~/grep_demo/logs/cache.log

Run the command echo 'fatal error occurred' > ~/grep_demo/logs/archive/old.log to create an archived error log file for subdirectory traversal testing.

echo ‘fatal error occurred’ > ~/grep_demo/logs/archive/old.log

Using the -r option together with the -n option recursively traverses down into subdirectories while displaying line numbers for matching lines.
Across files located in the logs directory and the nested archive directory, the file paths, line numbers, and contents containing the error string are all printed.

grep -rn ’error’ ~/grep_demo/logs

Adding the -l option and running it as -rl outputs only the list of file paths containing the pattern instead of the matching contents.
This is useful when quickly identifying only the files where error occurred among numerous log files.

grep -rl ’error’ ~/grep_demo/logs

Checking Context Around Search Results (-A, -B, -C) and Color Highlighting

Run the command echo -e 'Step 1: Init\nStep 2: Load Config\nStep 3: Critical Failure\nStep 4: Cleanup\nStep 5: Exit' > process.log to create a sample log file for checking step-by-step process progress and error context.

echo -e ‘Step 1: Init\nStep 2: Load Config\nStep 3: Critical Failure\nStep 4: Cleanup\nStep 5: Exit’ > process.log

Using the -B and -A options prints a specified number of lines before (Before) and after (After) the matching line.
By highlighting the matched word Critical Failure with color using the --color=always option, you can check the preceding 1 line and the following 2 lines together to understand the workflow before and after the error occurred.

grep –color=always -B 1 -A 2 ‘Critical Failure’ process.log

Using the -C option prints the same specified number of lines in both directions (Context) surrounding the matching line.
The 1 line immediately before and 1 line immediately after the line containing Critical Failure are displayed simultaneously, allowing you to check the essential context more concisely.

grep –color=always -C 1 ‘Critical Failure’ process.log

So far, we have covered the key aspects of the grep command in an Ubuntu environment, from basic search options to practical log analysis applications.
Put the patterns you learned today into practice in your terminal workflows to take your operational efficiency to the next level.