The grep command is widely used in Unix/Linux systems to search for patterns in text files. It’s useful for filtering lines that match a specific text or regular expression.

Basic syntax

grep [options] "pattern" file
  • pattern: The text or regular expression you want to search for.
  • file: The file where you want to perform the search.

Common examples:

  1. Search for a specific word in a file:
    grep "word" file.txt
  2. Case-insensitive search:
    grep -i "word" file.txt
  3. Search across multiple files:
    grep "word" *.txt
  4. Search for lines that do not contain a pattern:
    grep -v "word" file.txt
  5. Count how many times a word appears in a file:
    grep -c "word" file.txt
  6. Show the line number where the word appears:
    grep -n "word" file.txt

Advanced usage:

You can combine grep with other commands, such as cat or find, for more complex searches. For example:

  • Find files containing a specific word in a directory:
    find . -type f | xargs grep "word"