Searching tool in Linux - Grep Commands


    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.







Searching for a string in a file:

    The grep command with no flag prints the string that matches on the file.





    The flag  -l  with grep command will list the files that contains the given string.





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.





    The above command will search for the string in all .txt files.





    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.
   




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.,.





Printing the count of number of matches:

    We can find the number of kines that matches the string using  -c  flag with grep command.





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.





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.






Searching multiple expression on a single search:

    Using  -e  flag we can search for multiple expression in a search.





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,



    To list all the files in the current directory and sub-directories matched the search use  -il .





    To list all the matches in a file in the current directory and sub-directories use -r  with insensitive search.




   
    A grep command can be used with any of other file commands such as cat, less, zless, etc ..,. using a pipe symbol





    Hope this articles helps you. Feel free to ask if you have any questions.

Comments

Popular Posts on this blog

Apache Webserver - Installation from source (tar.gz and tar.bz2)

Setting up a Mail server using Roundcube - Part-1 - Centos 7

How to Install LAMP Stack on Linux - Centos 7

Installing Docker-Compose on Ubuntu

Apache Tomcat Installation - Ubuntu - 17.04

Setting up a Mail server using Roundcube - Part-3 - Centos 7

Blog Top Sites

Blog Log