DEV Community

Cover image for grep Command in Linux
Chielo Chiamaka
Chielo Chiamaka

Posted on

5

grep Command in Linux

When you're working with Linux log files, it's easy to rely on basic grep searches and call it a day.

But grep has powerful options that can help you find exactly what you're looking for, faster and with more precision.

Here are essential grep options and commands to help you analyze your logs more effectively and detect potential threats faster.

Table of Contents

Common grep Options Overview

Option Description
-c Count only the number of matching lines
-i Ignore case when matching (case-insensitive search)
-v Invert match (show lines that do not match the pattern)
-n Show the line number of each matching line
-l List file names that contain the match
-L List file names that do not contain the match
^ Anchor to match lines starting with the keyword (e.g., ^root)
$ Anchor to match lines ending with the keyword (e.g., nologin$)
> Redirect output to a new file (overwrites the file)
>> Append output to the end of an existing file (does not overwrite)

1. Search for a Keyword in a File

grep <Keyword> <Source>
Enter fullscreen mode Exit fullscreen mode

Purpose: Find all lines that contain "files".

Expected Output:

What it does:
Scans the grep_test.txt file and returns lines that include the word "files".

Search for keyword in a file

Explanation:
This shows every line where "files" appears. It’s useful for checking which users or settings involve root privileges.

2. Search for a Keyword in Multiple Files

grep <keyword> <source1> <source2>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

Keyword in multiple files

Explanation:
Each result shows the filename, line number, and the matching line. This is helpful for scanning system logs for error messages.

3. Show files that do NOT contain "server"

grep -L <keyword> <source
Enter fullscreen mode Exit fullscreen mode

Purpose:
List files that do NOT contain the word "server".

Expected Output:

Files that do not contain

If all files contain "server", you’ll see no output.
If one file doesn’t, you'll see:

log2.txt

4. Show line numbers with the match

grep -n <keyword> <source>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

Line numbers with match

Each match is prefixed with:

filename

line number

the line content containing the match

5. Search case-insensitively

grep -i <keyword> <source>
Enter fullscreen mode Exit fullscreen mode

Purpose:
Search ignoring case (so server, SERVER, and Server are all valid matches).

Expected Output

#5

If any file had:

SERVER CRASHED.

It would show that too.

6. Show Match Count (-c)

This option tells grep to show how many times the pattern appears in the file, rather than displaying the matching lines themselves.

grep -c <keyword> <source>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

#6

7. List Filenames with Matches (-l)

This option shows only the names of files that contain the matching keyword, not the actual matching lines.

grep -l <keyword> <source>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

#7

This is helpful when you just want to know which files have the keyword, without seeing the lines themselves.

8. Show Inverse Results (-v)

This option tells grep to show lines that do not match the given pattern.

grep -v <keyword> <source>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

#8

The lines that do not contain "server" are displayed. This is useful if you want to exclude certain patterns from your search.

9. Search for Lines Starting with a Keyword (^)

The caret ^ is used to search for lines that start with the specified pattern.

grep "^<keyword>" <source>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

#9

The ^ symbol means the search looks for lines that begin with the given pattern. Here, it finds the line where "Restart" is at the start of the line.

10. Search for Lines Ending with a Keyword ($)

The dollar sign $ is used to search for lines that end with the specified pattern.

grep "<keyword>$" <source>
Enter fullscreen mode Exit fullscreen mode

Expected Output:

#10

The $ symbol signifies that the search looks for lines that end with the specified pattern

11. Redirect Search Results into a File (>)

This option allows you to redirect the output of the grep command to a new file, overwriting any existing content.

grep <keyword> <source> > <destination_file>
Enter fullscreen mode Exit fullscreen mode

Output:

#11

No output to the screen because it's redirected to server.txt.

The content of errors.txt will be the lines from log1.txt containing the word "server".

The > operator redirects the search results into the specified file, overwriting the file content.

12. Append Results into a File (>>)

This option allows you to append the results of the grep command to an existing file, rather than overwriting it.

grep <keyword> <source> >> <destination_file>
Enter fullscreen mode Exit fullscreen mode

Output:

#12

The >> operator appends the output to the file, ensuring that existing content is preserved while adding new results

Conclusion

Whether you're filtering logs, scanning config files, or troubleshooting issues, the right grep option can save you time and give you clarity.

Let’s connect on LinkedIn

(https://www.linkedin.com/in/chiamaka-chielo?utm_source=share&utm_campaign=share_via&utm_content=profile&utm_medium=android_app)

As I automate my journey into RHCE and Ansible, I’d love to connect with fellow learners and professionals. Feel free to reach out and join me as I share tips, resources, and insights throughout this 30-day challenge.

cloudwhistler #30daysLinuxchallenge

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)