The grep command is used for searching patterns in files. It supports wildcards, regular expressions, and various options for filtering results.
grep [OPTIONS] PATTERN [FILE...]✅ Example: Search for "error" in a file
grep "error" logfile.txtgrep -w "error" logfile.txt✔ Matches error, but not errors or erroneous.
grep -i "error" logfile.txt✔ Matches error, ERROR, Error, etc.
grep -c "error" logfile.txt✔ Shows how many lines contain "error".
grep -n "error" logfile.txt✔ Displays line numbers of matching lines.
Wildcards are useful when searching across multiple files.
grep "error" *.txt✔ Searches for "error" in all .txt files.
grep "error" log*.txt✔ Searches in files like log1.txt, log2.txt, etc.
grep -r "error" /var/logs/✔ Searches "error" inside all files in /var/logs/ folder.
grep -r --exclude="*.log" "error" .✔ Searches "error" in all files except .log files.
grep -E "error|fail" logfile.txt✔ Matches lines containing "error" or "fail".
grep "^error" logfile.txt✔ Finds lines that start with "error".
grep "error$" logfile.txt✔ Finds lines that end with "error".
| Option | Description |
|---|---|
-w |
Match whole words only |
-i |
Ignore case |
-n |
Show line numbers |
-r |
Recursive search |
-c |
Count matching lines |
-l |
Show filenames with matches |
--color=auto |
Highlight matching words |
🔹 grep searches for patterns in files.
🔹 Wildcards (*, ?) help search in multiple files.
🔹 -w, -i, -n, and -r enhance search capabilities.
🔹 Regular expressions (-E) allow advanced pattern matching.
🚀 Master grep for fast and efficient text searching in Linux! 💡