DEV Community

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

Posted on • Originally published at melvingeorge.me

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

Originally posted here!

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>
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

output of running the command to search and find a specific keyword on multiple files

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 😃.


Top comments (0)