How to search and find a specific keyword or term from the contents of multiple files in the Linux terminal?

Published August 16, 2021

For searching and finding a specific keyword or a term from the contents of multiple files in the Linux terminal, you can use the grep command followed by the search term you need to search for, then the paths to the files you need to search the word separated by space.

TL;DR

# Search and find the occurrence of a specific
# word from the contents of more than one file
grep <YOUR_SEARCH_TERM> <PATH_TO_FILE_1> <PATH_TO_FILE_2> <PATH_TO_FILE_N>

For example, let's say we have two files called myFile1.txt and myFile2.txt which have the same content as below,

Example Content

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 two files myFile1.txt and myFile2.txt. We can do that using the grep command like this,

# Search and find the occurrence of a specific
# word from the contents of more than one file
grep encyclopedia myFile1.txt myFile2.txt

The output of running the above command may look like this,

If we look at the above output we can see that the first portion of each line tells us the name of the file the where the word is found and then the word we searched for is highlighted with color which tells us that it is present in that file.

See the execution of the above command live in repl.it.

That's all 😃!

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter