Skip to content

Find a keyword in many text files in Linux

Using grep

grep is the most common and versatile tool for searching text in Linux. Here’s how to use grep for your task:

Basic search

grep "keyword" filename.txt – This searches for the keyword “keyword” in the file “filename.txt” and displays lines containing the keyword.

Search all files in a directory recursively

grep -r "keyword" directory/ – This searches for “keyword” in all files inside the directory “directory/” and its subdirectories.

Additional options

-i: Ignore case (make search case-insensitive)
-w: Search for whole words only (not parts of words)
-n: Print line numbers where the keyword is found
-l: List only the filenames containing the keyword (without showing the content)

Leave a Reply