grep command in linux
What is grep command?
Well,the grep command is used to search character patterns in files line-by-line which can be executed in the command line to see if a particular string exists in the file or not in the linux or unix operating system.Like we do in the windows system for finding files we simply navigate to our top right corner search box to find inside the folder or the entire system.The advantage of grep command is that in one single command we can search for more than one file.In this examples I will be using git bash.So if you don't know about git or git bash then check here
I have explaned every command in it briefly so try to refer my article for your use!!I hope my tutorials are good often!!
How to install grep in system:-
It is easy to install grep as like any package in the linux,you can simply use your default package manager to install the package in your linux system,For any case you don’t know how to install package in linux then type this commands,
“ sudo apt-get install grep ”
For Ubuntu/Debian
“ sudo yum install grep ”
For RHEL/CentOS/Fedora
Syntax for grep command:-
“ grep [option] pattern file_path ”
Pattern-"string that you want to find”
Option-”Which behaviour you want to search”
file_path-”The location of the corresponding file”
Note:-if you are inside the folder where your file is located,then simply type your file name with the corresponding file type.
Some important options:-
There are a lot of options available in the grep commands which are listed below:-
- “ -c ”-It is used to display the number of matched lines which you have written in the pattern for finding the character
For example,
“grep -c "binod" grep2.txt ”
The above mentioned grep command will search for the word “binod” in the text file “grep2.txt”.It will provide you the counting of the matched characters inside the file
- “ -h ”-It is used to display the number of matched lines in the which you have written in the pattern for finding the character and hides the file name which you have provided
For example,
“ grep -h "binod" grep2.txt ”
The above mentioned grep command will search for the word “binod” in the text file “grep2.txt” and with won’t display the file name,
- “ -i ”-It is used to display the number of matched lines in which you have written in the pattern for finding the character and it ignores the case which means it will ignore the case during matching
For example,
“ grep -i "binod" grep2.txt”
The above mentioned grep command will search for the word “binod” in the text file “grep2.txt” case-insensitively.
- “ -l ”-It is used to display the list of files with matched pattern but will not display the matched lines inside the files
For example,
“ grep -l "binod" grep2.txt ”
The above mentioned grep command will search for the word “binod” in the text file “grep2.txt” and displays the list of files
- “ -n ”-It is used to display the matched line and the line number in which the character or pattern is found inside the file.It is the default option in the linux or unix grep command
For example,
“ grep -n "binod" grep2.txt ”
The above mentioned grep command will search for the word “binod” in the text file “grep2.txt” and displays line number in which it is found
Note:-You can also use multiple options in one grep command that is,
For example,
“ grep -ni "binod" grep2.txt ”
The above example will search for the pattern inside the file(s) as well as it will ignore the case also(i.e, case sensitivity).
How to find all the files that match the pattern:-
The above command is used to search for all files that match the pattern.This can be easily done using the * symbol
“ grep -i "binod" * ”
This command will find all the files consisting of the pattern ‘binod’ and it will ignore the case.
Search multiple files using the command in linux:-
To search for multiple files using you can simply add the filename after one and another,which is explained in the below mentioned code,
“ grep -i "binod" grep1.txt grep2.txt ”
The above mentioned code finds the pattern ‘hi’ in both of the files(i.e, file1 and file2)
Brief About Grep commands:-
Operating System Support----Linux OS(i.e, distro),Unix based System and Mac OS(i.e, Apple computer)
Root Access ----Not required
Time to Execute ---less than 15 seconds
Pre-installed --No(need to be installed in your system).
Uses of grep command:-
- It is very easy to navigate files that match the text using the grep command in linux/unix or in Mac OS.
- Work is done very fast,executing command in the terminal is more faster than the execution in the GUI based system.
- More control on system resources,it is the best command to search on files without even opening the file which is very nice for anyone,anyway doing something in the command line is more faster than in GUI based system
Final Words:-
“Hope you have enjoyed reading this article!!Thanks for reading!!And don’t forget to subscribe my blog!! I think that now you know all about the grep commands and there use in the linux file system,you are free to copy my code and execute it in your terminal,We discussed all about the grep command in the linux”
0 Comments
Thanks for commenting! If you have any doubt please let me now!