egrep - Linux


egrep is a command-line utility in Unix and Linux systems, designed for text searching using patterns. The primary function of egrep is to search a specified file or files and output the lines that contain matches to the specified pattern. egrep uses extended regular expressions, which provide more flexibility and functionality compared to basic regular expressions used by grep.


egrep [options] pattern [file...]
  • pattern: This denotes the regular expression that egrep searches for. It must be quoted if it includes shell metacharacters.
  • file...: One or more files in which the search is performed. If no file is specified, egrep searches the standard input.


  • -v or --invert-match: Display all lines that do not match the pattern.
  • -i or --ignore-case: Ignore case distinctions in both the pattern and the input files.
  • -c or --count: Only print a count of matching lines per file.
  • -l or --files-with-matches: Print only the names of files with selected lines.
  • -n or --line-number: Prefix each line of output with the 1-based line number within its input file.
  • -m or --max-count: Stops reading a file after a specified number of matching lines.
  • -e PATTERN: Specifies the pattern used during the search. Useful when searching for patterns that start with a hyphen.
  • -f FILE: Takes patterns from a file, one per line.
  • -r or --recursive: Read all files under each directory, recursively.


  1. Basic Search:
    Search for the pattern “hello” in the file text.txt.

    egrep "hello" text.txt
  2. Case Insensitive Search:
    Search for “hello” in text.txt, ignoring case.

    egrep -i "hello" text.txt
  3. Invert Match:
    Display lines that do not contain “hello”.

    egrep -v "hello" text.txt
  4. Count Matches:
    Count the number of lines that contain “hello”.

    egrep -c "hello" text.txt
  5. Recursive Search:
    Search recursively in the directory docs for the word “reference”.

    egrep -r "reference" docs/

Common Issues

  • Pattern Not Found: If your pattern uses characters interpreted by the shell, it may not work as expected. Always quote patterns to avoid this issue.
  • Binary File Matches: By default, egrep might search through binary files as well. Use the --binary-files option to control this behavior.
  • Performance Issues: Complex patterns or large input files can cause slow search speeds. Optimizing your pattern or using incremental searches might help.


egrep can be combined with other commands to form powerful command pipelines:

egrep -i "error" /var/log/syslog | less

This command chain searches for “error” in the syslog file, making the output easier to read using less.

  • grep: Basic pattern matching using basic regular expressions.
  • fgrep: Search for fixed strings instead of regular expressions.
  • awk: A programming language that can perform pattern scanning and processing.
  • sed: A stream editor that can perform more complex text transformations.

For more detail on using egrep, consult the grep man page (man grep), as it covers basic grep, egrep, and fgrep functionalities.