Site TIPS PAGE🔎 SEARCH  Ξ INDEX  MAIN MENU  UP ONE LEVEL
 OJB's Web Site. Version 2.1. Mac Tip.You are here: tip tips mac owen2 
Tip

Up to OJB's Mac Tips List

Command Line GREP Basics

Basics of using grep on the command line.

Search for text in a file and return the lines containing the text:
grep "TEXT" FILE
Where TEXT is the text to search for.
And FILE is the file to search in.
Note: If text has no spaces or other odd characters the quotes aren't required.

Of course, you can also use grep on the output of another command using a pipe:
COMMAND | grep "TEXT"
Where COMMAND is the other command which produces the output for grep to work on.
For example:
ls -1 | grep ".jpg"
Will list all the JPEG files in the current directory (with suffix ".jpg").
Where ls -1 displays files, one per line and | (pipe) sends output to the next command.

By default grep is case sensitive. To ignore case, use the -i option:
grep -i "TEXT" FILE

To reverse the search (find lines which don't include the text, use:
grep -v "TEXT" FILE
For example, to find all files which are not JPEGs, use:
ls -1 | grep -v -i ".jpg"
Where ls -1 displays the files in the current directory.
And | pipes the output of ls to grep.
And -v means reverse the search.
And -i means ignore case (see above).

To search in multiple files, use:
grep "TEXT" *
Where TEXT is the text to search for
And * means any file in the current directory.
Note: grep only works on text files so the output of other files might be weird.
To search only text files, use:
grep "TEXT" *.txt
To search in files inside directories use the -r (recursive search) option.
To recursively search symbolic links as well use -R.

To search for words, use:
grep -w "TEXT" FILE
Note: the -w option means search for TEXT as a word in the FILE.
For example:
grep -w "info" FILE
Will find where the word "info" appears, but not "information".

To search for lines, use:
grep -x "TEXT" FILE
Note: the -x option means search for TEXT as a whole line in the FILE.
For example:
grep -x "info" FILE
Will find any line which is exactly equal to "info".

To search for multiple items, use:
grep -E "TEXT1|TEXT2" FILE
Will find lines containing either TEXT1 or TEXT2 in FILE.
Note: the -E option specifies extended grep.

To count the number of lines matched instead of displaying them, use:
grep -c "TEXT" FILE
Displays a number showing the number of times TEXT was found in FILE

To search for any of several characters use:
grep -e "[AB]" FILE
Where A is the first character and B is the second (can have as many as needed).
For example, to find where number 1 or 2 exists:
grep -e "[12]" FILE

To show other lines before/after the one found, use:
grep -A AFTER -B BEFORE "TEXT" FILE
Finds lines containing TEXT in FILE and shows AFTER lines after and BEFORE lines before it.
A simpler option to show the same number of lines, either side:
grep -C LINES "TEXT" FILE
Finds lines containing TEXT in FILE and shows LINES lines after and before it.

To show the files containing the text in a multi file search, use:
grep -l "TEXT" FILES
For example:
grep "info" *.txt
Shows the files and matched lines where TEXT is FOUND in all text files.

To search for lines beginning with some text use:
grep "^TEXT" FILE
To search for lines ending with some text use:
grep "TEXT$" FILE
Note: the "^" character means beginning of line, and "$" means end of line.



I usually write a blog post about once a week. The latest post can be viewed here: Sheeple: Are they succumbing to fake news, misinformation, and propaganda. (posted 2025-09-24 at 17:12:31). I do podcasts too!. You can listen to my latest podcast, here: OJB's Podcast 2025-08-07 What is Anything?: Use language in an honest way to communicate instead of obfuscating..
 Site ©2025 by OJBRSS FeedWhy Macs are BestMade & Served on Mac 
Site Features: Blog RSS Feeds Podcasts Feedback Log07 Jun 2024. Hits: 738,737,309
Description: Mac TipKeywords: Macintosh,Computers,Fixing Problems,TipsLoad Timer: 202ms