Grep Command
Grep stands for “Global Regular Expression Print”. The grep is a command line utility which is basically used to search for a file / file(s) or searching for a pattern. The grep command lets you to search one or more files for a specific string and outputs line that contain the particular string. In simple, grep searches file(s) for specific text.
For example, you have a document saved on your computer and you forgot the name of the file, but you remember some words in that document. You can search for that file using the grep command with the words that you remember.
The syntax for grep command would be:
grep options “Search String” filename/directory pathThe following are the most commonly used options of the grep command:
-i Ignore the case of the letters when searching the file.
-n Output the line number of each line where a match is found in addition to the line itself.
-V Output all the lines that do not contain the search-string.
-W Output all the lines that contain the word being searched for.
-r Read all files under each directory, recursively
-l Outputs only filenames that contain the search-string.
The basic usage of grep command would look like:
grep “string” pathExample 1:
grep “john” /home/gemini/programs/calc.txtThe above command will search and display all the lines from the calc.txt file which contain the string “john”.
Example 2:
grep -i “john” /home/gemini/programs/calc.txtThe above command will ignore the case of the letters while searching the file calc.txt
Example 3:
grep -r “john” /home/gemini/programs/The above command will search and display all the lines from the files in the directory which contain the string “john”.
You can use the options in combination. To search for a text recursively in a directory by ignoring the case of the letters, you need to use the command like:
grep -ri “john” /home/gemini/programs/Example 4:
grep -l 'var' *.txtThis command will list all the .txt files in the current directory whose content matches the string var.
Example 5:
grep -v “welcome” /home/gemini/programs/function.txtThe above command will search and display all the lines from the file function.txt which do not contain the string “welcome”.
To use the output of another command as input to grep command:
Example: ls -l | grep 'id'
This command will list all the files with permissions containing string 'id' in the file name.
To redirect the results of a search to a file:
grep -i “arial” /home/gemini/programs/test.php > fonts.txtThe above command will search for the string “arial” in the test.php file and the search result will be stored in the file fonts.txt
Grep can also be piped multiple times. For example, the output of one grep command can be the input for the second one, and so on.
grep -i "var" /home/gemini/programs/test.php | grep -i "id"In the above example, the first grep command will search the string “var” and the second command will search and list the lines which contains the string “id” from the previous grep search result.










Post new comment