Bash Search Text

grep scans text line by line for a pattern and prints just the matching lines, making it the fastest way to search logs, code, and data files from the command line.

What Is grep?

grep stands for global regular expression print. It reads input one line at a time and prints only the lines that match the pattern you give it, leaving everything else out. The examples below use team.csv, a small comma-separated file of employee records with the columns name, department, age, and salary.

Example

$ cat team.csv
name,department,age,salary
Alice Johnson,Engineering,29,85000
Bob Smith,Marketing,34,62000
carol white,engineering,41,95000
David Lee,Sales,26,54000
Eve Davis,Marketing,38,71000
Frank Miller,Engineering,45,110000
grace hopper,Sales,31,67000
$ grep Engineering team.csv
Alice Johnson,Engineering,29,85000
Frank Miller,Engineering,45,110000

Case-Insensitive Matching with -i

Notice that plain grep above missed carol white's row, because her department was entered as lowercase engineering and grep compares text exactly as written by default. The -i flag tells grep to ignore case entirely, so uppercase and lowercase letters are treated as identical.

Example

$ grep -i Engineering team.csv
Alice Johnson,Engineering,29,85000
carol white,engineering,41,95000
Frank Miller,Engineering,45,110000

Recursive and Inverted Searches: -r and -v

Real projects rarely keep all their data in one file. The -r flag makes grep walk into every file inside a directory and its subdirectories, printing the filename alongside each match. The -v flag flips the logic entirely: instead of showing matching lines, it shows every line that does not match, which is useful for filtering out noise or excluding a category.

Example

$ ls data/
interns.csv team.csv
$ grep -r Sales data/
data/team.csv:David Lee,Sales,26,54000
data/team.csv:grace hopper,Sales,31,67000
data/interns.csv:Priya Nair,Sales,22,38000
$ grep -v Sales team.csv
name,department,age,salary
Alice Johnson,Engineering,29,85000
Bob Smith,Marketing,34,62000
carol white,engineering,41,95000
Eve Davis,Marketing,38,71000
Frank Miller,Engineering,45,110000
  • Add -n to print line numbers alongside matches, e.g. grep -n Sales team.csv shows exactly which line each hit came from.
  • Add -c to print a single count of matching lines instead of the lines themselves.
  • Add -l to print only the names of files that contain a match, which pairs well with -r across many files.
  • Add -w to match whole words only, so grep -w Sales will not accidentally match Salesforce.
  • Combine short flags together, like grep -riv pattern dir/, to mix case-insensitive, recursive, and inverted matching in a single command.
FlagMeaning
-iIgnore case when matching
-rSearch every file in a directory recursively
-vInvert the match, printing lines that do not match
-nShow the line number of each match
-cPrint only a count of matching lines
-wMatch whole words only
Note: grep is often the first tool in a longer pipeline. Try grep -i sales team.csv | wc -l to count matches, or grep -v Sales team.csv | sort to filter and then reorder in one line.