In Linux we use Grep as a search tool. It helps to search files on directory, search strings in a file. It is also a kind of a filter. Grep stands for Global search for Regular Expressions and Print. Grep helps to find strings in a large set of log files and codes. It is applicable for most of the files.
In this article we will be seeing some important grep commands with different options which will help us to make our search better.
The basic syntax for a grep command will include with the search string and filename. Different flags can be used for a customized search.
Basic Syntax: grep “search_word” File_name
Our demo files will be Testfile_1.txt, Testfile_2.txt, Testfile_1.php.
$cat Testfile_1.txt Hi LJunix users Im a searching document consists of some numbers and strings. Searching a number = 18976 Im searching document_1 Consists of numbers 1,2,3,4,5,6,7,8,9,0
$cat Testfile_2.txt Hi LJunix users Im searching document_2 Im a searching document consists of some special characters and strings. Searching a string = ()&*^ Consists of strings !,@,#,$,%,^,&,*,(,)
$cat Testfile_1.php Hi LJunix users Im a search file for PHP phpinfo()
Searching for a string in a file:
The grep command with no flag prints the string that matches on the file.
$ grep “search” Testfile_1.txt
ljunix@ljunix:~$ grep “search” Testfile_1.txt Im a searching document consists of some numbers and strings. Im searching document_1
The flag -l with grep command will list the files that contains the given string.
$ grep -l “search” Testfile_1.txt
ljunix@ljunix:~$ grep -l “search” Testfile_1.txt Testfile_1.txt
Searching for a string in multiple files:
Instead of a single file name we can add more than a file or set of particular file types.
$ grep “search” Testfile_1.txt Testfile_2.txt
ljunix@ljunix:~$ grep “search” Testfile_1.txt Testfile_2.txt Testfile_1.txt:Im a searching document consists of some numbers and strings. Testfile_1.txt:Im searching document_1 Testfile_2.txt:Im searching document_2 Testfile_2.txt:Im a searching document consists of some special characters and strings.
The above command will search for the string in all .txt files.
$ grep -l “search” *
ljunix@ljunix:~$ grep -l “search” * Testfile_1.php Testfile_1.txt Testfile_2.txt
This command will list all the files in a directory that contains the given string.
Printing the search string along with its line number:
We can print the line number of a file with the line matched on our search. For such search we can use -n with the grep command.
$ grep -n “search” *
ljunix@ljunix:~$ grep -n “search” * Testfile_1.php:2:Im a search file for PHP Testfile_1.txt:2:Im a searching document consists of some numbers and strings. Testfile_1.txt:4:Im searching document_1 Testfile_2.txt:2:Im searching document_2 Testfile_2.txt:3:Im a searching document consists of some special characters and strings.
Search on case insensitive:
Using -i enables to search for a string insensitively in a file. That is the search will print Uppercase, Lowercase words which match the give string like “Search”,”search”, “sEARCH”, etc.,.
$ grep -i “search” *
ljunix@ljunix:~$ grep -i “sEaRch” * Testfile_1.php:Im a search file for PHP Testfile_1.txt:Im a searching document consists of some numbers and strings. Testfile_1.txt:Searching a number = 18976 Testfile_1.txt:Im searching document_1 Testfile_2.txt:Im searching document_2 Testfile_2.txt:Im a searching document consists of some special characters and strings. Testfile_2.txt:Searching a string = ()&*^
Printing the count of number of matches:
We can find the number of kines that matches the string using -c flag with grep command.
$ grep -c “search” *
ljunix@ljunix:~$ grep -c “search” * Testfile_1.php:1 Testfile_1.txt:2 Testfile_2.txt:2
Searching the lines that starts with a string:
Using the ^ expression before the search string will print the line that starts with the given string.
$ grep “^Im” *
ljunix@ljunix:~$ grep “^Im” * Testfile_1.php:Im a search file for PHP Testfile_1.txt:Im a searching document consists of some numbers and strings. Testfile_1.txt:Im searching document_1 Testfile_2.txt:Im searching document_2 Testfile_2.txt:Im a searching document consists of some special characters and strings.
Searching the lines that ends with a string:
Using the $ expression after the search string will print the line that ends with the given string.
$grep “$” *
ljunix@ljunix:~$ grep “users$” * Testfile_1.php:Hi LJunix users Testfile_1.txt:Hi LJunix users Testfile_2.txt:Hi LJunix users
Searching multiple expression on a single search:
Using -e flag we can search for multiple expression in a search.
$ grep -e “Testfile_1” -e “Testfile_2” -e “search_3” File_name
ljunix@ljunix:~$ grep -e “some” -e “document” -e “LJunix” Testfile_1.txt Hi LJunix users Im a searching document consists of some numbers and strings. Im searching document_1
Recursive search:
It is a burden to search a particular word or string in a directory that contains sub-directories. This usually happens in a web directory. But using grep we can search for a string in a recursive manner using -r flag. More options can use along with the -r flag to make the search fair and clean.
For this demo we will use 3 directory with same files,
ljunix@ljunix:~$ ls Dir_1 Dir_2 Dir_3 Testfile_1.php Testfile_1.txt Testfile_2.txt
To list all the files in the current directory and sub-directories matched the search use -il .
$ grep -ril “search_word” *
ljunix@ljunix:~$ grep -ril “some” * Dir_1/In_Dir_1.txt Dir_2/In_Dir_2.txt Dir_3/In_Dir_3.txt Testfile_1.txt Testfile_2.txt
To list all the matches in a file in the current directory and sub-directories use -r with insensitive search.
$ grep -ri “search_word” *
ljunix@ljunix:~$ grep -ri “some” * Dir_1/In_Dir_1.txt:Im a searching document consists of some numbers and strings. Dir_2/In_Dir_2.txt:Im a searching document consists of some special characters and strings. Dir_3/In_Dir_3.txt:Im a searching document consists of some numbers and strings. Testfile_1.txt:Im a searching document consists of some numbers and strings. Testfile_2.txt:Im a searching document consists of some special characters and strings.
A grep command can be used with any of other file commands such as cat, less, zless, etc ..,. using a pipe symbol
$ cat .txt | grep search_word
ljunix@ljunix:~$ cat search_1.txt | grep consist Im a searching document consists of some numbers and strings.
Hope this articles helps you. Feel free to ask if you have any questions.

Leave a comment