Search Text in Files in Linux with Grep Command

Updated: Sep 2

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

The simplest grep command syntax looks like this:

grep pattern filename

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

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

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 * 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

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:


72 views
屏幕快照 2021-08-08 下午5.16.32.png