Bash grep


title: Bash grep

Bash command: grep

The grep command is used to find matching text in input file(s). Default output lists lines from the input file(s) which contain a match to the provided pattern. Options may be used to alter matching behavior or to provide a different output scheme.

Usage

grep [options] [pattern] [file_names]

Common options:

  • -i, Ignore case when mathing the provided pattern
  • -v, Show results which do not match the provided pattern.
  • -l, Instead of outputting matching lines, output the file paths which contain matching text.
  • -r, Search all files in the provided directories. Directory name(s) or path(s) is used in place of the file name(s) (Search defaults to the current working directory if none is provided)
  • -c, Output a count of the matching lines.
  • -E, Use extended regular expressions to define the pattern to be matched. The command alias egrep is the same as grep -E

Examples:

Search a server log file for lines containing the text “ERROR”:

grep ERROR server.log

Using a pipe to combine commands, list files and folders in the current working directory that contain the text “code”:

ls | grep code

Search for IP 127.0.0.1 in the /etc/hosts file

grep "127.0.0.1" /etc/hosts

Search for oom (out of memory) in /var/log/messages

grep -i oom /var/log/messages

### More Information

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.