How to search and find a specific keyword or term from the contents of a file in the Linux terminal?
Published August 14, 2021
For searching and finding a specific keyword or a term from the contents of a file in the Linux terminal, you can run the
cat command followed by the
path to the file, then the
| operator (aka pipe operator), then the
grep command and finally the keyword you want to search for.
# Search and find the occurrence of a specific # word from the contents of a file cat <PATH_TO_FILE> | grep <YOUR_SEARCH_TERM>
For example, let's say we have a file called
myFile.txt which has some content like below,
Wikipedia is a free content, multilingual online encyclopedia written and maintained by a community of volunteer contributors through a model of open collaboration, using a wiki-based editing system.
Now, we want to find the occurrence of the word
encyclopedia from the contents of the
myFile.txt file. We can do that using the
grep commands like this,
# Search and find the occurrence of a specific # word from the contents of a file cat myFile.txt | grep encyclopedia
The output of running the above command may look like this,
As you can see that the word
encyclopedia is highlighted in the output which shows us that it is present.
See the execution of the above command live in repl.it.
That's all 😃!