How to Use the grep Command to Find Specific Text Inside Multiple Files in Linux

If you have a massive directory containing 500 different log files, and you need to find the single specific file that contains the error code “ERROR_503,” manually opening and reading every single text document is impossible. Instead, you can use the legendary Linux grep command to instantly scan the contents of every file simultaneously and return the exact line of text where the error occurs.

What Does grep Do?

The grep command (Global Regular Expression Print) is the ultimate search engine for the Linux terminal. While the find command looks for file names, grep looks inside the files to analyze the actual text.

How to Search Multiple Files

Using grep requires a simple syntax: you type the command, followed by the specific word you want to find, followed by the files you want to search.

  1. Open your Linux terminal.
  2. Navigate to the directory containing your text files or logs.
  3. To search for the phrase “ERROR_503” inside every single file in the current directory, use the wildcard (*) symbol:

grep "ERROR_503" *

  1. Press Enter.

The terminal will instantly output a list. Every line will begin with the name of the file, followed by a colon, followed by the exact sentence where the error code was found.

Advanced grep Features

If your logs are buried inside multiple folders and subfolders, the standard asterisk will not search deep enough. You must use the -r (recursive) flag to force grep to dig through every single folder in the tree.

grep -r "ERROR_503" /var/log/

Furthermore, grep is case-sensitive by default. If the error is written as “error_503” (lowercase), the command will miss it. To force grep to ignore capital letters and find every variation of the word, add the -i (case-insensitive) flag.

grep -ri "ERROR_503" /var/log/

This single command is the absolute fastest way to troubleshoot a broken Linux server, allowing you to instantly locate needles hidden inside massive digital haystacks.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.