grep Command Examples in Linux

The grep command, in its most basic form, is a search tool. Unlike find or locate, it is not limited to finding file names; it is most often used to search the contents of a file for a particular string of text. As output, grep displays each full line of the file that your search pattern was found in. In this way, you can use grep to both processes a text file and read the contents that are most pertinent to you. For example, you may want to audit a user’s login events by looking at an access log. Instead of reading the entire log or stepping through a search term in a text editor, you can simply print all of the relevant lines to the screen with the grep command.

grep command examples in Linux

Syntax

The syntax of the grep command is:

# grep [options] {search pattern} {file names}

grep Command Options

The grep command has many options. Several common ones are described in the following table.

Using grep to find files

In addition to searching the contents of files, you can use grep to search a directory in order to locate a certain file. The ls -l | grep audit command returns a long listing of any files in the current directory whose name contains “audit”.

egrep Command

The egrep command is essentially the same as the grep -E command. However, egrep is deprecated, as grep -E is the preferred syntax.

grep Command Examples

1. To interpret PATTERN as an extended regular expression:

# grep --extended-regexp PATTERN
# grep -E PATTERN

2. To interpret PATTERN as a list of fixed strings:

# grep -F PATTERN
# grep --fixed-strings PATTERN

4. To interpret PATTERN as a basic regular expression:

# grep -G PATTERN
# grep --basic-regexp PATTERN

5. To interpret PATTERN as a Perl regular expression:

# grep -P PATTERN
# grep --perl-regexp PATTERN

6. To use PATTERN as the pattern:

# grep -e PATTERN,
# grep --regexp=PATTERN

7. To obtain patterns from FILE, one per line:

# grep -f FILE, --file=FILE

8. To ignore case distinctions in both the PATTERN and the input files:

# grep -i PATTERN
# grep --ignore-case PATTERN

9. To invert the sense of matching, to select non-matching lines:

# grep -v PATTERN
# grep --invert-match PATTERN

10. To Select only those lines containing matches that form whole words:

# grep -w PATTERN
# grep --word-regexp PATTERN

11. To Select only those matches that exactly match the whole line:

# grep -x PATTERN
# grep --line-regexp PATTERN

12. To ignore the case:

# grep -y PATTERN

13. To Suppress normal output; instead print a count of matching lines:

# grep -c PATTERN
# grep --count PATTERN

14. To display in color:

# grep --color PATTERN

15. To Suppress normal output; instead print the name of each input file, from out will not be expected:

# grep -L
# grep --files-without-match

16. To Suppress normal output; instead print the name of each input file from which output have been printed:

# grep -l
# grep --files-with-matches

17. To Quiet; do not write anything to standard output Exit immediately with zero status if any match is found:

# grep -q
# grep --quiet
# grep --silent

18. To Stop reading a file after NUM matching lines:

# grep -m NUM
# grep --max-count=NUM

19. To Print only the matched (non-empty) parts of a matching line:

# grep -o PATTERN
# grep --only-matching PATTERN

20. To Suppress error messages about nonexistent or unreadable files:

# grep -s PATTERN
# grep --no-messages PATTERN

21. To Print the 0-based byte offset within the input file before each line of output:

# grep -b PATTERN
# grep --byte-offset PATTERN

22. To Print the file name for each match:

# grep -H PATTERN
# grep --with-filename PATTERN

23. To Suppress the prefixing of file names on output:

# grep -h PATTERN
# grep --no-filename PATTERN

24. To Display input actually coming from standard input as input coming from file LABEL:

# grep -cd PATTERN | grep --label=mysearch -H PATTERN

25. To Prefix each line of output with the 1-based line number within its input file:

# grep -n PATTERN
# grep --line-number PATTERN

26. To Make sure that the first character of actual line content lies on a tab stop:

# grep -T PATTERN
# grep --initial-tab PATTERN

27. To Report Unix-style byte offsets:

# grep -u PATTERN
# grep --unix-byte-offsets PATTERN

28. To Output a zero byte instead of the character that normally follows a file name:

# grep -Z PATTERN
# grep --null PATTERN

29. To Print NUM lines of trailing context after matching lines:

# grep -A NUM PATTERN
# grep --after-context=NUM PATTERN

30. To Print NUM lines of leading context before matching lines:

# grep -B NUM PATTERN
# grep --before-context=NUM PATTERN

31. To Print NUM lines of output context:

# grep -C NUM PATTERN
# grep --context=NUM PATTERN

32. To Process a binary file as if it were text:

# grep -a PATTERN /tmp/bin
# grep -text PATTERN /tmp/bin

33. To assume that the file is of type TYPE:

# grep --binary-files=TYPE PATTERN

34. To If an input file is a device, FIFO or socket, use ACTION to process it:

# grep -D ACTION PATTERN
# grep --devices=ACTION PATTERN

35. To If an input file is a directory, use ACTION to process it:

# grep -d ACTION PATTERN
# grep --directories=ACTION PATTERN

36. To skip files whose base name matches GLOB:

# grep --exclude=GLOB PATTERN

37. To Skip files whose base name matches any of the file-name globs read from FILE:

# grep --exclude-from=FILE PATTERN

38. To Exclude directories matching the pattern DIR from recursive searches:

# grep --exclude-dir=DIR PATTERN

39. To Process a binary file as if it did not contain matching data:

# grep -I PATTERN

40. To Search only files whose base name matches GLOB:

# grep --include=GLOB

41. To Read all files under each directory, recursively:

# grep -r PATTERN
# grep -R PATTERN

42. To Use line buffering on output:

# grep --line-buffered PATTERN

43. To If possible, use the mmap system call to read input, instead of the default read:

# grep --mmap PATTERN

44. To Treat the file(s) as binary:

# grep -U /tmp/file PATTERN
# grep --binary /tmp/file PATTERN

45. To Treat the input as a set of lines:

# grep -z PATTERN
# grep --null-data PATTERN

46. To display the help:

# grep -h

47. To print the version number of the grep:

# grep -V