How to Grep Two Strings in Linux? A Comprehensive Guide


Linux provides a powerful command-line utility called grep that allows users to search for specific text patterns within files. Sometimes, you might need to search for two different strings simultaneously. In this article, we will delve into the intricacies of how to grep two strings in Linux effectively. Whether you’re a beginner or an experienced user, you’ll find valuable insights and hands-on examples to master this task.

How to Grep Two Strings in Linux?

When it comes to searching for two strings in Linux, the grep command can be a valuable ally. To perform this task, follow these steps:

  1. Open your Linux terminal.
  2. Use the following syntax to search for two strings:perlCopy codegrep -e "string1" -e "string2" file.txt Replace string1 and string2 with the actual strings you want to search for, and file.txt with the name of the file you want to search in.

Practical Examples

Let’s explore some practical examples to better understand how to grep two strings in Linux:

Example 1: Searching in a Single File

Suppose you have a file named data.txt, and you want to search for occurrences of both “apple” and “orange.” You can use the following command:

grep -e "apple" -e "orange" data.txt

This command will display all lines containing either “apple” or “orange” or both.

Example 2: Searching in Multiple Files

To search for two strings across multiple files in a directory, you can use the grep command with the wildcard *:

grep -e "pattern1" -e "pattern2" *.txt

This command will search for the specified patterns in all text files within the directory.

Advanced Techniques

Mastering grep involves more than just basic searches. Here are some advanced techniques to enhance your string searching skills:

Filtering Output

You can filter the output further by using the -w flag to match whole words or the -i flag for case-insensitive searches:

 -wie "apple" -e "orange" data.txt

This command will only match the whole word “apple” and perform a case-insensitive search for “orange.”

Inverting Matches

Use the -v flag to invert matches, displaying lines that don’t contain the specified strings:

grep -v -e "banana" -e "grape" data.txt

This command will show lines that do not contain “banana” or “grape.”


Can I search for more than two strings simultaneously?

Absolutely! You can extend the -e flag to include as many strings as you need in your search.

No, the order doesn’t matter. grep will match both strings regardless of their sequence.

Yes, you can utilize regular expressions to perform more complex and versatile searches.

How can I search for two strings in subdirectories as well?

Combine the grep command with the -r flag to search for strings recursively in subdirectories.

Is there a graphical alternative to using the terminal for string searching?

While there are GUI tools available, using the terminal and grep provides greater flexibility and efficiency.

Can I save the search results to a file?

Yes, you can redirect the output to a file using the > operator. For instance, grep -e "pattern" file.txt > results.txt will save the results to a file named “results.txt.”

How to grep two strings in Linux?

Use the grep command followed by the -e flag and the two strings to search for both in a file.

How do I grep multiple words in a single line in Linux?

Employ the grep command with the -e flag and provide the multiple words to find them in a single line of a file.

How do I grep multiple patterns in Linux?

Utilize the grep command along with the -E (extended regex) flag and enclose the patterns in parentheses separated by the | symbol.

How do you grep two words together in Linux?

Invoke the grep command with the -E flag and enclose the two words together within double quotes to find them as a phrase in a file.


Searching for two strings in Linux using the grep command is a powerful skill that can significantly enhance your productivity. By following the steps outlined in this guide, you can efficiently locate relevant information within files. Additionally, the advanced techniques and practical examples provided here give you the tools to become a proficient grep user. Embrace the command-line interface, and you’ll unlock a world of possibilities for text searching in Linux.

Leave a comment