Skip to Content

4 ways to Search Text in Files with Grep Command in Linux

Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. When it finds a match, it prints the line with the result. The grep command is handy when searching through large log files.

Grep lets us enter a pattern of text and then it searches for this pattern within the text that we provide it. It returns all the lines that have the given pattern or string in them.

Grep Command Syntax in Linux

The simplest grep command syntax looks like this:

grep pattern filename

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

The grep command consists of three parts in its most basic form. The first part starts with grep, followed by the search pattern that we are searching for. After the string comes the file name that the grep searches through.

A search pattern is a sequence of symbols or characters, eg. “This is a string”. Strings typically must be enclosed in quotations to ensure the command correctly recognizes them as strings and not a number or variable name.

We will use these two files as an example.

$ cat test
howtouselinux
howtouselinux1
howtouselinux2
howtouselinux hello

$ cat test2
howtouselinux
howtouselinux3
howtouselinux4
howtouselinux hello

Search Text in One File in Linux

To print any line from a file that contains a specific pattern of characters.

grep search-pattern filename

In our case, we need to find howtouselinux in the file test, run the following command.

Grep will display every line where there is a match for the word howtouselinux.

$ grep howtouselinux test
howtouselinux
howtouselinux1
howtouselinux2
howtouselinux hello

Search Text in Multiple Files in Linux

We can use the following command to search text in multiple files.

grep search-pattern filename1 filename2

In our case, we need to find howtouselinux in the file test and test2, run the following command.

Grep will display every line where there is a match for the word howtouselinux in two files.

$ grep howtouselinux test test2
test:howtouselinux
test:howtouselinux1
test:howtouselinux2
test:howtouselinux hello
test2:howtouselinux
test2:howtouselinux3
test2:howtouselinux4
test2:howtouselinux hello

Search text in All the files under the current directory in Linux

In Linux * means a lot of things. Here it means all the files in the current directory. We can use the following way to search text in all the files under the current directory.

grep search-pattern *

Search More Patterns in Linux

If we need to search for more keywords, we can use the following commands.

grep search-pattern1 filename|grep search-pattern2

egrep 'search-pattern1|search-pattern2' filename

For example, this command will display the lines which have howtouselinux and hello.

grep howtouselinux test|grep hello

If we need to display the lines which have howtouselinux or hello, we can use this command.

egrep 'howtouselinux|hello' test

Grep Options

  • -w Find Exact Match Words – The Linux grep command illustrated in the earlier example also lists lines with partial matches. we can use -w to find only the exact occurrences of a word.
  • -i Ignore Case Distinctions – By default, grep searches for patterns in a case-sensitive way. We may want to turn this off if we don’t know in what case the pattern is beforehand.

 

Related: