How To Use Grep Command in Linux

As the name suggests, grep or worldwide regular expression print allows you search for particular text patterns within a documents’s components. Its performances include pattern recognition, defining instance level of sensitivity, searching numerous documents, recursive search, and many more.

So whether you’re a novice or a system manager, learning about the grep command to situate the files successfully is good. This tutorial will explain how to use grep in Linux and review its various applications.

Just How To Make Use Of Grep Command in Linux

The fundamental feature of the grep command is to search for a particular message inside a documents. You can do that by going into the complying with command:

grep “text_to_search” file.txt

Please replace ‘text_to_search’ with the text you intend to look for and ‘file.txt’ with the target file. As an example, to discover the string “Hello there” in the data called file.txt, we will use:

On getting in the above command, grep will check the Intro.txt apply for “Hello there.” As a result, it shows the result of the whole line or lines containing the target text.

If the target data is on a path various from your present directory, please mention that course together with the data name. For instance:

grep “Hello” ~ / Documents / file.txt

grep-command-with-the-file-location

Here, the tildes ‘~’ mark represents your home directory. The above instance shows how you can look for an item of text in a single file. However, if you want to do the same search on multiple documents at the same time, discuss them consequently in one grep command:

grep “Hi” file.txt Linux_info. txt Password.txt

using-grep-command-for-multiple-files

In instance you’re uncertain concerning your string’s instances(capital or lowercase), perform a case-insensitive search by utilizing the i alternative:

grep -i “hello” Intro.txt

i-option-in-grep-command i-option-in-grep-command

Although the string we input was not the precise match, we got exact outcomes with the case-insensitive search. In situation you wish to invert the modifications and inspect documents that do not have the certain pattern, then please use the v alternative:

grep -v “Hello there” file.txt Linux_info. txt Password.txt

v-option-in-grep-command v-option-in-grep-command

In addition, if you want to show the lines that begin with a certain word, make use of the ‘^’ icon. It serves as an anchor that defines the start of the line.

grep-command-example grep-command-example

The above commands will just serve when you know which submit to look. In this instance, you can recursively search the string inside the entire directory site using the r choice. As an example, let’s search “Hello there” inside the Files directory site:

grep -r “Hey there” ~ / Papers

r-option-grep-command r-option-grep-command

In addition, you can also count the number of times the input string shows up in a file with the c choice:

grep -c “Hello” Intro.txt

c-option-in-grep-command c-option-in-grep-command

Similarly, you can display the line numbers along with the matched lines with the n alternative:

grep -n “Hello there” Intro.txt

n-option-in-grep-command n-option-in-grep-command

A Quick Wrap-up

Users commonly bear in mind that a data made use of to have an item of text yet forget the documents name, which can land them in deep trouble. For this reason, this tutorial was about making use of the grep command to look for message in a data’s materials. In addition, we have used various instances to show exactly how you can fine-tune the grep command’s operating with a few alternatives. You can experiment by combining numerous alternatives to figure out what fits best according to your use situation.