fgrep Command Examples in Linux

There are 3 different forms of the famous grep command. The list below also differentiates between their use cases.

  • fgrep: Does a fast search for simple patterns. Use this command to quickly locate patterns without any wildcard characters, useful when searching for an ordinary word.
  • grep: Pattern searches using ordinary regular expressions.
  • egrep: Pattern searches using more powerful extended regular expressions.

fgrep (fast grep) searches one or more files for lines that match the specified text string. Exit status is 0 if any lines match, 1 if not, and 2 for errors. fgrep is faster than normal grep searches but less flexible: it can only find fixed text, not regular expressions.

The fgrep command is just like grep, but instead of accepting a regular expression, it accepts a list of fixed strings, separated by newlines. It’s the same as grep -F. For example, if you have a dictionary file full of strings, one per line:

# cat my_dictionary_file
aardvark
aback
abandon
...

you can conveniently search for those strings in a set of input files:

# fgrep -f my_dictionary_file inputfile1 inputfile2

Normally, you’ll use the lowercase -f option to make fgrep read the fixed strings from a file. You can also read the fixed strings on the command line using quoting, but it’s a bit trickier. To search for the strings one, two, and three in a file, you’d type:

# fgrep 'one             ### Note we are typing newline characters
two
three' myfile

fgrep is convenient when searching for non-alphanumeric characters like * and { because they are taken literally, not as regular expression characters.

fgrep Command Examples

1. To Interpret PATTERN as an extended regular expression:

# fgrep --extended-regexp PATTERN
# fgrep -E PATTERN 

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

# fgrep -F PATTERN
# fgrep --fixed-strings PATTERN 

3. To Interpret PATTERN as a basic regular expression:

# fgrep -G PATTERN
# fgrep --basic-regexp PATTERN 

4. To Interpret PATTERN as a Perl regular expression:

# fgrep -P PATTERN
# fgrep --perl-regexp PATTERN 

5. To Use PATTERN as the pattern:

# fgrep -e PATTERN, 
# fgrep --regexp=PATTERN

6. To Obtain patterns from FILE, one per line:

# fgrep -f FILE, --file=FILE 

7. To Ignore case distinctions in both the PATTERN and the input files:

# fgrep -i PATTERN
# fgrep --ignore-case PATTERN

8. To Invert the sense of matching, to select non-matching lines:

# fgrep -v PATTERN
# fgrep --invert-match PATTERN 

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

# fgrep -w PATTERN
# fgrep --word-regexp PATTERN 

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

# fgrep -x PATTERN
# fgrep --line-regexp PATTERN 

11. To ignore the case:

# fgrep -y PATTERN

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

# fgrep -c PATTERN
# fgrep --count PATTERN

13. To display in color:

# fgrep --color PATTERN 

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

# fgrep -L PATTERN
# fgrep --files-without-match PATTERN

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

# fgrep -l PATTERN
# fgrep --files-with-matches PATTERN 

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

# fgrep -q PATTERN
# fgrep --quiet PATTERN
# fgrep --silent PATTERN 

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

# fgrep -m NUM PATTERN
# fgrep --max-count=NUM PATTERN

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

# fgrep -o PATTERN
# fgrep --only-matching PATTERN

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

# fgrep -s PATTERN
# fgrep --no-messages PATTERN

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

# fgrep -b PATTERN
# fgrep --byte-offset PATTERN 

21. To Print the file name for each match:

# fgrep -H PATTERN
# fgrep --with-filename PATTERN 

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

# fgrep -h PATTERN
# fgrep --no-filename PATTERN 

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

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

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

# fgrep -n PATTERN
# fgrep --line-number PATTERN

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

# fgrep -T PATTERN
# fgrep --initial-tab PATTERN 

26. To Report Unix-style byte offsets:

# fgrep -u PATTERN
# fgrep --unix-byte-offsets PATTERN 

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

# fgrep -Z PATTERN
# fgrep --null PATTERN 

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

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

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

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

30. To Print NUM lines of output context:

# fgrep -C NUM PATTERN
# fgrep --context=NUM PATTERN

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

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

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

# fgrep --binary-files=TYPE PATTERN 

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

# fgrep -D ACTION PATTERN
# fgrep --devices=ACTION PATTERN

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

# fgrep -d ACTION PATTERN
# fgrep --directories=ACTION PATTERN 

35. To Skip files whose base name matches GLOB:

# fgrep --exclude=GLOB PATTERN 

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

# fgrep --exclude-from=FILE PATTERN

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

# fgrep --exclude-dir=DIR PATTERN 

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

# fgrep -I PATTERN 

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

# fgrep --include=GLOB 

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

# fgrep -r PATTERN
# fgrep -R PATTERN 

41. To Use line buffering on output:

# fgrep --line-buffered PATTERN 

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

# fgrep --mmap PATTERN 

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

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

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

# fgrep -z PATTERN
# fgrep --null-data PATTERN 

45. To display the help:

# fgrep -h 

46. To print the version number of the grep:

# fgrep -V 
Related Post