Linux grep command

Linux grep command

Linux grep Command – Search for Text in Files

The grep (Global Regular Expression Print) command in Linux searches for specific text patterns within files or command outputs. It is one of the most powerful tools for searching and filtering text in the terminal.

Syntax of grep

grep [OPTIONS] PATTERN [FILE]
  • PATTERN → The text or regex pattern to search for.
  • FILE → The file(s) in which to search.

If no file is specified, grep reads from standard input.

Basic Usage of grep

1. Search for a Specific Word in a File

grep "word" filename

Example:

grep "error" log.txt

This will display all lines in log.txt that contain the word "error".

2. Case-Insensitive Search (-i)

To ignore case differences:

grep -i "error" log.txt

This will match "Error", "ERROR", "error", etc.

3. Search for an Exact Word (-w)

To match whole words only:

grep -w "is" example.txt

This will match "is" but not "this" or "inside".

4. Search in Multiple Files

grep "error" file1.txt file2.txt

This searches for "error" in both file1.txt and file2.txt.

5. Show Line Numbers (-n)

To display line numbers where the match is found:

grep -n "error" log.txt

Output:

15:error in module X 42:error: missing file

The numbers (15, 42) indicate the line positions.

6. Display Only Matching Text (-o)

grep -o "error" log.txt

Instead of printing whole lines, only the word "error" will be shown.

7. Count the Number of Matches (-c)

grep -c "error" log.txt

This returns the number of times "error" appears in the file.

8. Invert Match (Exclude Matching Lines) (-v)

To show lines that do not contain the pattern:

grep -v "error" log.txt

Useful for filtering out unwanted text.

Advanced grep Usage

9. Search Using Regular Expressions (-E)

The -E option allows extended regex (equivalent to egrep):

grep -E "error|warning|failed" log.txt

This searches for "error", "warning", or "failed".

10. Search Recursively in All Files (-r)

To search inside all files in a directory (and subdirectories):

grep -r "error" /var/logs/

This will scan all logs inside /var/logs/.

11. Show Only Filenames (-l)

grep -l "error" *.txt

This will list only filenames where the pattern is found, not the matching lines.

12. Highlight Matches (--color)

To highlight the matched text:

grep --color "error" log.txt

This improves readability in terminal searches.

Using grep with Other Commands

13. Filter Output from Another Command

ps aux | grep "firefox"

This finds running processes related to firefox.

14. Search for a User in /etc/passwd

grep "username" /etc/passwd

This checks if a user exists in the system.

15. Search for Lines Starting with a Word (^)

grep "^root" /etc/passwd

Finds lines that start with "root".

16. Search for Lines Ending with a Word ($)

grep "bash$" /etc/passwd

Finds lines that end with "bash".

Difference Between grep, fgrep, and egrep

CommandPurpose
grepStandard text search using basic regex.
fgrepSearches for fixed strings, without regex (deprecated, use grep -F).
egrepUses extended regex, equivalent to grep -E (deprecated).

Conclusion

The grep command is one of the most powerful tools for searching and filtering text in Linux. It is widely used in system administration, log analysis, and text processing.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close