grep Command in Linux

Learn via video courses
Topics Covered

Overview

The grep command in Linux is one of the most frequently used commands in a Linux environment. The name 'grep' stands for 'global regular expression print'. This powerful command-line tool allows users to search and filter text or files for specific patterns. In essence, the grep command in Linux is primarily used for pattern searching in file systems, making it an invaluable tool for Linux administrators and power users.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of grep Command in Linux

The basic syntax of the grep command in Linux is as follows:

Where:

  • options: This part allows the user to specify different parameters that modify the behavior of the grep command.
  • pattern: This is the search term you are looking for. This could be a word, a number, or a regular expression.
  • file: This is the file, or files, in which you are searching the pattern. If no file is specified, grep searches the standard input.

Options in grep Command in Linux

  1. -i: This option makes the grep command case-insensitive.

    For example -

    Output:

    This command will search for the 'pattern' in 'filename', ignoring the case.

  2. -v: This option inverts the search, returning lines that do not match the pattern.

    For example -

    Output:

    This command will display lines that do not include the specified 'pattern'.

  3. -r or -R: This option enables recursive search. '-R' also follows symbolic links.

    For example -

    Output:

    This command will search for 'pattern' recursively in all files under the specified directory.

Example Usages

  • Basic pattern search:

    Output:

    Explanation: This is the most basic usage of grep, searching for 'pattern' within 'filename'.

  • Count the number of matching lines:

    Output:

    Explanation: This usage of grep returns a count of the number of lines that contain the specified 'pattern'.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Tips

  • Remember, by default, grep is case sensitive. Use the '-i' option for case-insensitive searches.

  • You can use regular expressions with the grep command for more complex pattern searching.

  • Use the '--color' option to highlight the matching strings in the output. This can be very helpful in visually parsing the results.

Advanced Use Cases of grep Command in Linux

  • Display lines before and after match:

    Output:

    Explanation: This command will show context around the lines matching the pattern, which can be useful for understanding the occurrences of the pattern.

  • Search with regular expressions:

    Output:

    Explanation: This command uses regular expressions to search for lines that start with the specified pattern.

  • Display only the matched pattern, not the whole line:

    Output:

    Explanation: This command will show only the parts of lines that match the pattern, rather than the entire lines.

Conclusion

  • The grep command in Linux is an incredibly powerful tool for searching and filtering text.

  • Grep can be used with a variety of options to modify its behavior, including case-insensitive searching, inverting the search, and recursive searching.

  • The ability to use regular expressions with grep provides a high degree of flexibility for complex pattern searching.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more