grep stands for International Common Expression Print. grep command is accessible in Unix/Linux based mostly working programs. Because the full-form of the device means that it’s used for looking any textual content or expression within the given file(s).

It’s helpful when you’re looking for a line in a file that comprises a selected key phrase. grep command may use choices to advance the search question.

The essential syntax of the grep command –

grep expression filename

The essential syntax of the command requires not less than two inputs. The primary enter is the expression or key phrase that you just need to seek for. The second argument is the file to look.

For the demonstration functions, I’ve created 4 recordsdata in a listing referred to as ‘Information’. Every file comprises some traces of textual content.

Grep Command-Linux Command GuideInformation

I’ll begin from the essential utilization of the grep command and additional advance the question utilizing the instruments choices.

grep argument_1 file.txt
Grep Command-Linux Command Guidegrep fundamental search

As you possibly can see, the above command has grabbed the road from file.txt that comprises the expression ‘argument_1‘. It’s easy. Proper?

grep command – search from a number of recordsdata

Equally, we are able to additionally seek for any expression in a number of recordsdata by passing recordsdata names separated with house.

grep expression_4 file.txt file1.txt
Grep Command-Linux Command Guidegrep search a number of recordsdata

grep command – Search all recordsdata in a listing

You can too search all of the recordsdata saved in a listing directly. Right here is how you are able to do it –

grep argument_4 *
Grep Command-Linux Command Guidegrep search all recordsdata in listing

grep – Search recordsdata in all subdirectories (Recursive search)

If the recordsdata are saved in a number of subdirectories, then it’s potential to look all of the recordsdata in the principle listing and subdirectories on the identical time utilizing -r choice.

grep -r argument_3 *
Grep Command-Linux Command Guidegrep search recursive

Print solely quite a lot of traces from outcomes

By default, grep will print all of the traces that comprise the given expression. For instance, if I seek for the phrase “Howdy” in a file, there’s a risk that the phrase “Howdy” is talked about a number of occasions in that file. So the command will print all of the traces that comprise the phrase Howdy.

Grep Command-Linux Command Guidegrep command output

This habits will be modified by offering the -m choice to the command.

grep -m 1 argument_1 file.txt
Grep Command-Linux Command Guiderestrict output traces

Print bytes offset with the road

To print the bytes offset with the discovered line, go the -b choice to the command.

grep -b argument_1 file.txt
Grep Command-Linux Command Guidegrep print bytes offset

Print line quantity with line

You can too print the road quantity earlier than every line that it finds.

grep -n argument_1 file.txt
Grep Command-Linux Command Guideprint line quantity

Disguise filename from traces

In case you search a number of recordsdata, by default the device additionally prints the filename earlier than every line. To cover the road quantity, use -h argument.

Grep Command-Linux Command Guideprint filename with linesgrep -h argument_1 file.txt file1.txt
Grep Command-Linux Command GuideDisguise filename from traces

Print solely matching expression

By default, the device prints the entire line with the expression. In case you solely need to print the expression, use -o choice.

grep -o argument_2 file.txt file1.txt
Grep Command-Linux Command GuidePrint solely matching phrase

grep command – Search recordsdata match the precise sample

To solely search recordsdata that match a selected sample, use the -R choice within the command.

grep -R –include=*.txt argument_3 *
Grep Command-Linux Command Guidesearch recordsdata with sample

Exclude recordsdata & directories match the precise sample

To exclude recordsdata and directories from the search that match the precise sample, use -R –exclude choice within the command.

grep -R –exclude=*.txt argument_3 *
Grep Command-Linux Command GuideExclude recordsdata match particular sample

grep command – Exclude directories match the precise sample

To exclude directories from the search, use -R –exclude-dir choice within the command.

grep -R exclude-dir=Information argument_3 *
Grep Command-Linux Command GuideExclude directories match particular sample

grep command – Print solely filenames with no match

To print filenames that do match the expression, use the -L choice within the command.

grep -L argument_3 file.txt file1.txt file2.txt
Grep Command-Linux Command Guideprint filenames with out match

Print solely filenames with a match

To solely print filenames that match the expression, use -l choice within the command.

Grep Command-Linux Command Guideprint filenames with match

Mix choices

If you wish to go a number of choices in a single question, that is how you are able to do that –

grep -nohR –include=*.txt argument_3
Grep Command-Linux Command Guidegrep mix choices

Conclusion

So there you may have it. grep command is helpful when writing bash scripts, or performing a seek for an expression in a listing construction with a whole lot of recordsdata. Simply follow the command and attempt to mix choices collectively to additional filter the outcomes as you want.

If you’d like extra assist, please consult with the person pages or go away a remark under this text.

grep command in linux with examples,grep command options,grep command to find a file in a directory,grep in current directory and subdirectories,how to search a particular word in a file in linux,sed command in linux,grep command in unix with example pdf,how to use grep command in terminal