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: Think for Yourself: In the end, everything is just an opinion. Be skeptical, and think for yourself! (posted 2026-04-25 at 21:12:02). Or go to the Home Page of My Blog to choose a post to view.
I do podcasts too! You can listen to my latest podcast, here: OJB's Podcast 2026-04-25 Think For Yourself: In the end, everything is just an opinion. Be skeptical, and think for yourself! Or subscribe to my podcast RSS feed, on my RSS Feeds page.
Latest Site News and Notifications (Desktop): You are currently viewing OJB's web site, version 2.4 which has some major changes, and possibly errors! Please report any problems to ojb@mac.com.