Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»Grep Command in Linux (Search Text in Files)

    Grep Command in Linux (Search Text in Files)

    By RahulSeptember 8, 20215 Mins Read

    Grep is a powerful tool for searching a text, Grep means “Global regular expression print”. Basically, Grep searches a text file for the specified regular expression and outputs any line containing a match to standard output. So Grep command can be used to search some kind of text, word, pattern or sentence in a text file or a number of text files.

    Advertisement
    • Suggested Read: 12 Awesome Linux Find Command Examples

    In this tutorial, we will show you how to use the Grep command with some practical examples.

    Prerequisites

    • A system running the Linux operating system.
    • Access to a terminal/command line.

    To Search a Specific String in a File

    • To search for a string tecadmin in the file file1.txt, run the following command:
      grep tecadmin file1.txt
      

      This command will print all lines that contain a word tecadmin:

      tecadmin is a popular linux blog
      i love tecadmin
      tecadminlinux
      

      Linux grep command example 1

    • To search for an exact string tecadmin in the file file1.txt, run the following command:
      grep -w tecadmin file1.txt
      

      This command will print all lines that contain a whole word tecadmin:

      tecadmin is a popular linux blog
      i love tecadmin
      

      Linux grep command example 2

    • To search for a string tecadmin case insensitively in the file file1.txt, run the following command:
      grep -i tecadmin file1.txt
      

      This command will print all lines that contain a word tecadmin case insensitively:

      tecadmin is a popular linux blog
      i love tecadmin
      tecadminlinux
      Tecadmin is my favourite blog
      This is Tecadmin website
      

      Linux grep command example 3

    To Search a Specific String in Multiple File

    • To search for a string linux in file file1.txt and file2.txt, run the following command:
      grep -i linux file1.txt file2.txt
      

      This command will print all lines that contain a word linux in file1.txt and file2.txt:

      file1.txt:tecadmin is a popular linux blog
      file1.txt:tecadminlinux
      file2.txt:Linux is an open-source operating system.
      file2.txt:linux is made by linus torvalds.
      file2.txt:linux is most popular operating system.
      

      Linux grep command example 4

    • To search for a string linux in all files in the current directory and sub-directories, run the following command:
      grep -r linux *
      

      This command will print all lines that contain a word linux in all files in the current directory and sub-directories:

      file1.txt:tecadmin is a popular linux blog
      file1.txt:tecadminlinux
      file2.txt:linux is made by linus torvalds.
      file2.txt:linux is most popular operating system.
      file3.txt:linux vs windows
      file3.txt:Ubuntu is a linux operating system
      

      Linux grep command example 5

    Inverse Grep Search

    You can use grep command with -v option to print all lines that do not match a specific pattern of characters.

    For example, print all lines that don’t contain the string linux in file1.txt and file2.txt, run the following command:

    grep -v linux file1.txt file2.txt
    

    This command will exclude all the lines that contain the string linux:

    file1.txt:i love tecadmin
    file1.txt:Tecadmin is my favourite blog
    file1.txt:This is Tecadmin website
    file2.txt:Linux is an open-source operating system.
    

    Linux grep command example 6

    To List Filenames That Matches Specific Pattern

    You can display only filenames that contain a specific string using -l option.

    For example, list all filenames in the current directory that matches the string tecadmin, run the following command:

    grep -l tecadmin *
    

    You should see the following output:

    file1.txt
    

    Linux grep command example 7

    Display the Number of Matches

    You can use grep with -c option to display all files with the number of lines that matches the given string.

    For example, to display all files with the number of lines that matches a string linux in the current directory, run the following command:

    grep -c linux *
    

    You should see the following output:

    file1.txt:2
    file2.txt:2
    file3.txt:2
    

    Linux grep command example 8

    Display Line Number with Matching Pattern

    You can use grep with -n option to print line numbers with matching patterns.

    For example, to display line number that matches the pattern linux in the current directory, run the following command:

    grep -n linux * 
    

    You should see the following output:

    file1.txt:1:tecadmin is a popular linux blog
    file1.txt:3:tecadminlinux
    file2.txt:2:linux is made by linus torvalds.
    file2.txt:3:linux is most popular operating system.
    file3.txt:1:linux vs windows
    file3.txt:2:Ubuntu is a linux operating system
    

    Linux grep command example 9

    You can also display one line before and after the matching string using the option c and n with grep command.

    For example, display one line before and after the matching string linux in file4.txt, run the following command:

    grep -n -C 1 linux file4.txt
    

    You should see the following output:

    1-Hi, i am tecadmin user
    2:i am linux operating system
    3-i am windows operating system
    

    Linux grep command example 10

    Display Only Matching Pattern

    By default, grep command prints the entire line which matches a pattern.

    You can print only matching patterns using the -o option.

    For example, search for file1.txt that matches the string/pattern linux with the following command:

    grep -o linux file1.txt 
    

    You should see the following output:

    linux
    linux
    

    Linux grep command example 11

    Conclusion

    In the above tutorial, you learned how to use the grep command to search for a specific string in files. I hope you have now enough knowledge of grep command and how it is used in various conditions.

    grep grep command linux command
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    free Command in Linux (Check Memory Uses)

    A Practical Guide to Extracting Compressed Files in Linux

    TR Command in Linux: A Guide with Practical Examples

    TR Command in Linux: A Guide with Practical Examples

    View 1 Comment

    1 Comment

    1. tychen on February 10, 2019 10:20 pm

      To search for multiple strings you need to use -E option or egrep.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)
    • Understanding Basic Git Workflow: Add, Commit, Push
    • The Difference Between Git Reset –soft, –mixed, and –hard
    • Understanding the Staging Area in Git’s Workflow
    • Python Function with Parameters, Return and Data Types
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.