🔍 Search text files for a particular word or phrase

🔍 Search text files for a particular word or phrase

grep can search one or many files for a word or phrase.

To search one file for the phrase “Libby the dog”, try this:

grep "Libby the dog" file.txt

Note that you use quotation marks around your phrase because you are searching for a phrase. If you’re searching for a word, you don’t need quotation marks.

To search in a directory of text files for the phrase “Libby the dog”, try this:

grep "Libby the dog" *.txt

By default, grep searches are case-sensitive. If you grep for “dog”, you will NOT see results for “Dog”. To search in a directory of text files for the word “dog” or “Dog”, try this:

grep -i dog *.txt

To search in a directory of text files for word “dog” or “Dog” and then automatically have the results of that search entered into a text file called “dogsearch.txt”, try this:

grep -i dog *.txt > dogsearch.txt

To find out how many times the word “dog” is used in a text file, try this:

grep -c dog file.txt
whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.