how to use grep command to find text in a file

  • The most widely used command to search for any files or any text/content from/within the file/s, or whole directory structure, i.e to print lines matching a pattern
  • It is very handy utility and every Linux admin must have command over it.
  • It comes from “grep” package.

Examples:

1. To interpret PATTERN as an extended regular expression

# grep –extended-regexp PATTERN filename
# grep -E PATTERN
filename

2. To interpret PATTERN as a list of fixed strings

# grep -F PATTERN filename
# grep –fixed-strings PATTERN
filename

3. To interpret PATTERN as a basic regular expression

# grep -G PATTERN filename
# grep –basic-regexp PATTERN
filename

4. To interpret  PATTERN as a Perl regular expression

# grep -P PATTERN filename
# grep –perl-regexp PATTERN filename

5. To use PATTERN as the pattern

# grep -e PATTERN filename
# grep –regexp=PATTERN
filename

6. To obtain patterns from a FILE,

# grep -f FILE filename
# grep –file=FILE filename

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

# grep -i PATTERN filename
# grep –ignore-case PATTERN filename

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

# grep -v PATTERN filename
# grep –invert-match PATTERN filename

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

# grep -w PATTERN filename
# grep –word-regexp PATTERN filename

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

# grep -x PATTERN filename
# grep –line-regexp PATTERN filename

11. To ignore the case

# grep -y PATTERN filename

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

# grep -c PATTERN filename
# grep –count PATTERN filename

13. To display in color

# grep –color PATTERN filename

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

# grep -L filename
# grep –files-without-match filename

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

# grep -l filename
# grep –files-with-matches filename

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

# grep -q filename
# grep –quiet filename
# grep –silent filename

17. To Stop reading a file after NUM matching lines

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

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

# grep -o PATTERN filename
# grep –only-matching PATTERN filename

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

# grep -s PATTERN filename
# grep –no-messages PATTERN filename

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

# grep -b PATTERN filename
# grep –byte-offset PATTERN filename

21. To Print the file name for each match.

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

22. To Suppress  the  prefixing of file names on output

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

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

# grep -cd PATTERN filename
#grep –label=mysearch -H PATTERN filename

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 filename
# grep –initial-tab PATTERN filename

26. To Report  Unix-style  byte  offsets

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

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

# grep -Z PATTERN filename
# grep –null PATTERN filename

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

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

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

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

30. To Print  NUM  lines  of  output  context

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

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

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

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

# grep –binary-files=TYPE PATTERN filename

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

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

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

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

35. To skip files whose base name matches GLOB

# grep –exclude=GLOB PATTERN filename

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

# grep –exclude-from=FILE PATTERN filename

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

# grep –exclude-dir=DIR PATTERN filename

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

# grep -I PATTERN filename

39. To Search  only  files  whose  base  name  matches  GLOB

# grep –include=GLOB filename

40. To Read all files under each directory, recursively

# grep -r PATTERN filename
# grep -R PATTERN filename

41. To Use line buffering on output

# grep –line-buffered PATTERN filename

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

# grep –mmap PATTERN filename

43. To Treat the file(s) as binary

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

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

grep command to search a string in directory

When ever you want to search any string in a specific directory then, you can do it with this command as below. Where “r” is for recursively serching under the directory and “i” is for searching text in both cases (small case as well as large case).

# grep -ri string /path/

5 thoughts on “how to use grep command to find text in a file

Leave a Reply

Your email address will not be published. Required fields are marked *