command grep linux

The grep command in Linux is a powerful utility for searching through text using patterns called regular expressions. Here’s a brief overview of how to use the grep command:

Basic Usage:

  • To search for a simple pattern in a file:
  grep 'pattern' filename


  • -i: Ignore case when searching.
  • -v: Invert the match to show lines that do not contain the pattern.
  • -r: Recursively search through directories.
  • -n: Show line numbers along with the matching lines.
  • -l: Show only the names of files that contain the match.
  • -E: Interpret the pattern as an extended regular expression (more complex patterns).


  1. Search for the word “error” in a case-insensitive manner:
   grep -i 'error' filename
  1. List all lines that do not contain the word “error”:
   grep -v 'error' filename
  1. Recursively search for the pattern in a directory:
   grep -r 'pattern' /path/to/directory
  1. Show line numbers for matching lines:
   grep -n 'pattern' filename
  1. Use extended regular expressions to find lines with three consecutive digits:
   grep -E '(\d){3}' filename
  1. Find files containing the pattern and show their names:
   grep -l 'pattern' *
  1. Combine grep with other commands (e.g., ls) to filter output:
   ls -l | grep 'pattern'
  1. Use grep to search for a pattern and then pipe the output to another grep to exclude a specific pattern:
   grep 'include_pattern' filename | grep -v 'exclude_pattern'

grep is a versatile command that can be used in various ways to filter and search through text data in the Linux command line.

This entry was posted in Linux File & Directory, Linux Grep and tagged , , . Bookmark the permalink.