ShareYourGroupShareYourGroupCommands

Linux and Mac commands

grep: search inside files

grep prints the lines of a file that contain a word or pattern. It answers "where does this appear?" for one file or ten thousand. It reads and never changes anything.

Linux or Mac terminal
asha@pc:~/demo$ grep ERROR log.txt
09:05 ERROR Printer not found
09:07 ERROR Printer not found

The pattern comes first, then the file.

The everyday options

OptionEffect
-iignore capital and small letters
-nshow line numbers
-ccount matching lines
-vinvert: show lines that do not match
-wmatch whole words only
-llist only the names of files that contain a match
-rsearch all files in a folder and below
-A 2, -B 2also show 2 lines after, or before, each match
-Eextended patterns, such as a|b for either
Linux or Mac terminal
asha@pc:~/demo$ grep -n -i printer log.txt
3:09:05 ERROR Printer not found
5:09:07 ERROR Printer not found
asha@pc:~/demo$ grep -c ERROR log.txt
2
asha@pc:~/demo$ grep -v INFO log.txt
09:05 ERROR Printer not found
09:07 ERROR Printer not found
09:10 WARN  Low disk space

A phrase

A pattern with spaces needs quotation marks.

Linux or Mac terminal
asha@pc:~/demo$ grep "Low disk" log.txt
09:10 WARN  Low disk space

Several files

Give several names or a wildcard. Each match is shown with its file name.

Linux or Mac terminal
asha@pc:~/demo$ grep -i report *.txt
log.txt:09:02 INFO  Opened report.txt
log.txt:09:15 INFO  Saved report.txt
report.txt:Sales report for October

A whole folder

-r searches every file below a folder. Adding -l gives just the file names, and -n the line numbers.

Linux or Mac terminal
asha@pc:~/demo$ grep -r -l -i report .
./log.txt
./old/draft.txt
./report.txt
asha@pc:~/demo$ grep -rn "draft" .
./old/draft.txt:1:First draft of the report

The dot means "the current folder". Limit the search to certain files with --include:

Linux or Mac terminal
asha@pc:~/demo$ grep -rn --include="*.txt" -i total .
./report.txt:5:Total: 18700

Whole words

Without -w, the pattern matches anywhere, even inside longer words.

Linux or Mac terminal
asha@pc:~/demo$ grep -c port log.txt
2
asha@pc:~/demo$ grep -c -w port log.txt
0

The first counts lines containing "port", which includes "report". The second counts only the word by itself.

Either of two words

-E switches on extended patterns, where a bar means "or".

Linux or Mac terminal
asha@pc:~/demo$ grep -E "ERROR|WARN" log.txt
09:05 ERROR Printer not found
09:07 ERROR Printer not found
09:10 WARN  Low disk space

Context

When a match alone is not enough, show the lines around it.

Linux or Mac terminal
asha@pc:~/demo$ grep -A 1 ERROR log.txt
09:05 ERROR Printer not found
09:06 INFO  Retrying
09:07 ERROR Printer not found
09:10 WARN  Low disk space

Patterns

grep understands regular expressions. The most useful pieces:

PatternMatches
^wordlines that start with word
word$lines that end with word
.any one character
[0-9]any digit
[A-Z]any capital letter
a*a repeated none or more times
^$empty lines
Linux or Mac terminal
asha@pc:~/demo$ grep "^09:0" log.txt
09:00 INFO  Program started
09:02 INFO  Opened report.txt
09:05 ERROR Printer not found
09:06 INFO  Retrying
09:07 ERROR Printer not found
asha@pc:~/demo$ grep "closed$" log.txt
09:16 INFO  Program closed
asha@pc:~/demo$ grep -c "^[A-Z]" marks.csv
5

Put patterns in quotation marks, so that the shell does not try to interpret the special characters itself. To search for text that contains these characters literally, use -F, for fixed text: grep -F "a.b" file.

Searching the output of other commands

grep reads from a pipe as well as from files. This is how it is used most.

Linux or Mac terminal
asha@pc:~/demo$ ls -l | grep csv
-rw-r--r-- 1 asha asha  53 Nov  1 15:00 marks.csv

Common uses: ps aux | grep firefox to find a running program, history | grep ssh to find an earlier command, and dmesg | grep -i usb to look at system messages.

Counting and combining

-c counts lines. To count matches in many files and total them, pipe through wc -l.

Linux or Mac terminal
asha@pc:~/demo$ grep -h ERROR log.txt | wc -l
2

Saving the result

Linux or Mac terminal
asha@pc:~/demo$ grep ERROR log.txt > errors.txt
asha@pc:~/demo$ cat errors.txt
09:05 ERROR Printer not found
09:07 ERROR Printer not found

Speed and binary files

grep is fast. On very large folders, exclude what you do not need with --exclude-dir, such as --exclude-dir=.git. If it reports "Binary file matches", the file is not text. -I skips such files.

Common mistakes

The Windows equivalent is findstr.

Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.

This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.