Linux

Grep Command Tutorial In Linux | Beginner’s Guide

Grep Command Tutorial In Linux | Beginner's Guide

Insight: Grep Command Tutorial In Linux | Beginner’s Guide

Linux is fun to use if you are in love with command. In this post, we are going to talk about one of the Linux  command. This post is intended for beginner users. We are going show you the basic example of Grep command. Grep is  short form of “global regular expression print” and is considered as one of the most powerful commands in Linux. It is used to search the files. Grep can be used to search the lines in a file which matches a given pattern.

grep [OPTIONS] PATTERN [FILE...]

Here, OPTIONS can be the zero or the various options which you can find in this link. PATTERN refers to the search pattern and the FILE can be the zero or file names.

Below is the example to display the lines from the /opt/filename file containing the string omgfoss . You can use the following command to search the line:

grep omgfoss /etc/filename

Meanwhile, if the string includes spaces, see the example like below:

grep "omg foss " /etc/filename

Meanwhile, To display the lines which doesn’t not match a pattern:

Suppose, you want to display the lines from the /etc/filename file that do not contain the string nomatch, then you can use the following command:

grep -v nomatch /etc/filename

Meanwhile, If you want to recursively search for a pattern , use the -r option . In the following example we are searching for the string helloworld in all files inside the /opt directory:

grep -r helloworld /opt

Above example won’t follow any symbolic link. If you want to search symbolic link to then run the following command.

grep -R helloworld /opt

Grep Multiple Strings

We are searching the file for the words hello, hi, world in the omgfoss.txt file:

READ THIS ALSO  Best Lock Screen Themes For Linux In 2018 : GDM Themes

grep ‘hello\|hi\|world’ /etc/omgfoss.txt

 

Grep Show Only Filename

Below is the example to search through all files ending with .txt  in the current directory where you are right now and print only the files name containing the string heloworld type:

grep -l helloworld *.txt

This much for now. This tutorial is intended for beginner level of Linux users. Please let us know if there is any error in this post.



Subscribe To Our Newsletter.

Join our mailing list to receive the latest linux tutorials, tips & tricks, freebies,news and updates from our team.

You have Successfully Subscribed!