command grep examples in linux

The grep command in Linux is indispensable for filtering and searching through text. Here are some practical examples of how to use grep in different scenarios:

  1. Search for a string in a file:
    Find lines containing the word “hello” in example.txt.
   grep 'hello' example.txt
  1. Case-insensitive search:
    Search for “HELLO” without considering case sensitivity.
   grep -i 'HELLO' example.txt
  1. Recursive search in a directory:
    Search for “data” in all files within the Documents directory, including subdirectories.
   grep -r 'data' ~/Documents
  1. Exclude certain files or directories:
    Search for “error” recursively in the logs directory, excluding *.log.old files.
   grep -r --exclude='*.log.old' 'error' logs/
  1. Show line numbers:
    Display line numbers for each match of the pattern “error”.
   grep -n 'error' logfile.txt
  1. Print file names:
    Only print the names of files containing the word “error”.
   grep -l 'error' *.txt
  1. Count occurrences:
    Count how many times the word “error” appears in logfile.txt.
   grep -c 'error' logfile.txt
  1. Invert match:
    Show all lines that do not contain the word “error”.
   grep -v 'error' logfile.txt
  1. Use regular expressions:
    Find lines that start with a digit.
   grep '^[0-9]' data.txt
  1. Context control:
    Show the line with the match, as well as two lines before and after.
   grep -C 2 'important' document.txt
  1. Search for multiple patterns:
    Find lines that contain either “start” or “end”.
   grep -E 'start|end' file.txt
  1. Exclude a pattern while searching:
    Find lines with “start” but not “error”.
   grep 'start' file.txt | grep -v 'error'
  1. Search in compressed files:
    Look for the pattern “data” in a compressed log file.
   grep 'data' log.gz
  1. Use with other commands:
    Combine grep with ls to list only .txt files containing “report”.
   ls -l | grep 'report'
  1. Search for a pattern and print the byte offset:
    Show the pattern “match” and its position in bytes.
   grep -bo 'match' file.txt
  1. Search for a pattern and print the column number:
    Find the word “found” and print its column number in the line.
   grep -o -E '[a-z]+' file.txt | grep -ob 'found' | cut -d':' -f1
  1. Pipes and grep:
    Use grep to filter the output of another command, such as showing processes with “httpd”.
   ps aux | grep httpd
  1. Search for a pattern in a file and exclude a directory:
    Search for “pattern” in all .txt files, excluding the tmp directory.
   grep -r --exclude-dir='tmp' 'pattern' *.txt
  1. Search using Perl-compatible regular expressions:
    Use PCRE to find lines with an IP address pattern.
   grep -P '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b' file.txt
  1. Interactive search in a file:
    Use grep with the -i option in combination with less for an interactive search.
   grep -i 'search_term' largefile.txt | less -i

These examples showcase the flexibility and power of grep for text searching and data extraction in Linux environments.

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