grep: command not found

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 process 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.

Syntax

The syntax of the grep command is:

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

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”.

If you want to reverse the search (output lines that don’t match the pattern), use the -v parameter:

$ grep -v t file1
one
four
five
$

If you need to find the line numbers where the matching patterns are found, use the -n parameter:

$ grep -n t file1
2:two
3:three
$

If you just need to see a count of how many lines contain the matching pattern, use the -c parameter:

$ grep -c t file1
2
$

If you need to specify more than one matching pattern, use the -e parameter to specify each individual pattern:

$ grep -e t -e f file1
two
three
four
five
$

If you encounter the below error while running the grep command:

grep: command not found

you may try installing the below package as per your choice of distribution:

OS Distribution Command
OS X brew install grep
Debian apt-get install grep
Ubuntu apt-get install grep
Alpine apk add grep
Arch Linux pacman -S grep
Kali Linux apt-get install grep
CentOS yum install grep
Fedora dnf install grep
Raspbian apt-get install grep

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 

3. To interpret PATTERN as a basic regular expression:

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

4. To interpret PATTERN as a Perl regular expression:

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

5. To use PATTERN as the pattern:

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

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

# grep -f FILE, --file=FILE

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

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

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

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

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

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

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

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

11. To ignore the case:

# grep -y PATTERN 

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

# grep -c PATTERN
# grep --count PATTERN 

13. To display in color:

# grep --color PATTERN 

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

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

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

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

16. 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 

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

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

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

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

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

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

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

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

21. To Print the file name for each match:

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

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

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

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

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

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

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

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

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

26. To Report Unix-style byte offsets:

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

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

# grep -Z PATTERN
# grep --null PATTERN

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

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

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

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

30. To Print NUM lines of output context:

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

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

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

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

# grep --binary-files=TYPE PATTERN 

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

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

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

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

35. To skip files whose base name matches GLOB:

# grep --exclude=GLOB PATTERN 

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

# grep --exclude-from=FILE PATTERN 

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

# grep --exclude-dir=DIR PATTERN

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

# grep -I PATTERN

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

# grep --include=GLOB 

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

# grep -r PATTERN
# grep -R PATTERN 

41. To Use line buffering on output:

# grep --line-buffered PATTERN 

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

# grep --mmap PATTERN 

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

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

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

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

45. To display the help:

# grep -h 

46. To print the version number of the grep:

# grep -V 
Related Post