Linux – grep command

grep is a command-line utility used for searching and filtering text files based on specified patterns or regular expressions. It displays the matching lines or filenames containing the pattern

CommandDescriptionExampleOutput
grep pattern fileSearch for a pattern in a single file.grep "hello" file.txtThis is a sample file containing hello world.
Another line with the word hello.
grep pattern file1 file2Search for a pattern in multiple files.grep "error" file1.txt file2.txtfile1.txt: An error occurred during the process.
file2.txt: Error: Connection timeout.
grep -r pattern directoryRecursively search for a pattern in a directory and its subdirectories.grep -r "TODO" ./src./src/main.js: // TODO: Implement the feature.
./src/components/todo.jsx: <h1>TODO List</h1>
grep -i pattern filePerform a case-insensitive search.grep -i "apple" fruits.txtThere’s an Apple tree in the garden.
I like apples and oranges.
grep -n pattern fileDisplay line numbers along with matching lines.grep -n "function" script.js1: function greet() {
4: const sayHello = function() {
grep -v pattern fileInvert the search and display non-matching lines.grep -v "success" logs.txtError: Failed to load resource.
Warning: Disk space low.
Info: Application started.
grep -l pattern file1 file2List filenames containing the pattern.grep -l “error” *.loglogfile.txt
grep -w pattern fileMatch whole words only.grep -w “cat” animals.txtThe cat chased a mouse.

`grep -E “pattern1pattern2″ file`grep -E “apple|orange” fruits.txtThere’s an Apple tree in the garden.
I like apples and oranges.
grep -A 2 pattern fileDisplay 2 lines after each match.grep -A 2 “ERROR” logfile.txtLine 72: ERROR – Null pointer exception.
Line 73: Method call that caused the error.
Line 74: Additional information about the error.
grep -B 1 pattern fileDisplay 1 line before each match.grep -B 1 “warning” log.txtLine 28: WARNING – This action is irreversible.
Line 29: Proceed with caution.
grep -C 2 pattern fileDisplay 2 lines before and after each match.grep -C 2 “important” notes.txtLine 14: This is an important message.
Line 15: Please take note of this information.
Line 16: Also, remember the following details.
grep -o pattern fileShow only the matched part of the line.grep -o “user[0-9]+” data.txtuser1234
user9876
user2468
grep -q pattern fileQuiet mode: suppress output, set exit status.grep -q "pattern" file && echo "Found"Found

File List

file.txt

This is a sample file containing hello world.
Another line with the word hello.
Some random text.

file1.txt

This is file1.
An error occurred during the process.
Some other text here.

file2.txt

This is file2.
Error: Connection timeout.
Additional lines in file2.

fruits.txt

There's an Apple tree in the garden.
I like apples and oranges.
I prefer bananas.

script.js

function greet() {
  console.log("Hello!");
}

const sayHello = function() {
  console.log("Saying hello!");
};

// Some comments here.

log.txt

Error: Failed to load resource.
Warning: Disk space low.
Info: Application started.

notes.txt

Line 14: This is an important message.
Line 15: Please take note of this information.
Line 16: Also, remember the following details.
Line 17: Another note.

data.txt

user1234 data1
user9876 data2
user2468 data3