Mastering the Grep Command in Linux: A Comprehensive Guide

Grep is a powerful command-line utility used in Linux for searching and manipulating text data. The name “grep” stands for “Global Regular Expression Print“. This command is used to search for a particular pattern or string of characters within a file or multiple files.

Grep command in Linux is one of the most commonly used commands, and it can be used for a variety of purposes, including searching for specific words or phrases, filtering out unwanted data, finding files in Linux, and processing log files.

Grep Command in Linux


The grep command in Linux is a powerful tool used for searching files for specific patterns or strings. It has been a part of Unix-based operating systems since the 1970s and is now an integral part of Linux. It is one of the most commonly used commands on the Linux command line and is essential for anyone who wants to become a Linux power user.

Basic usage of grep command in Linux

The basic usage of the grep command involves searching for a specific string in a file or multiple files.

The syntax of the command is simple, and it can be used in various ways to search for different types of strings. One of the most useful features of the command is its ability to perform case-insensitive searches.

Searching for a string in a file: grep “string” filename

Suppose we have a file called file.txt with the below content and we will use this simple file for our grep tutorial to make this tutorial more informative. If you are new to Linux or grep, you can try the below example on your own Linux machines.

# Ref: https://www.storagetutorials.com/
# Tutorial: grep command in Linux
This is line number 1
this will be line 2
THIS is LINE THREE
# This is comment line
5 - This line starts with number
i love linux and grep

Grep Example 1: Search for the string “Linux” in file.txt.

$  grep Linux file.txt 
# Tutorial: grep command in Linux

To search for a string in multiple files use grep “string” file1 file2


Case insensitive search: grep -i “string” filename

In the above example, you might have noticed that our command did not return the last line. This is because the word “linux” was in a small case.

To make our search case insensitive we will use the option/switch “-i” with our grep command

Grep Example 2: Search for all the lines containing the string “Linux” in file.txt.

$ grep -i Linux file.txt
# Tutorial: grep command in Linux
i love linux and grep

Searching for files with grep command in Linux: ls |grep -i file

The grep command can also be used to search for files in a directory.

This feature can be used to find specific files or to search for files that contain a specific string.

Grep Example 3: Search for the file name in a directory.

$ ls |grep -i file
file.txt

The command can also be used to print line numbers in the search results.

Grep Example 4: Search for the term ‘linux’ and also print the line number

$ grep -n linux file.txt 
8:i love linux and grep

Grep recursive search: grep -r “string” /path/to/directory

You can also use the grep command in Linux to search a string in all the files present in the directory. This is very helpful for searching a particular log in multiple files.

Grep example 5: Search for all the files containing the string ‘linux’ in a directory.

$ grep -r linux /data/st/Linux
                                                                                  
/data/st/Linux/c1:Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [10.7 kB]                                                                        kB]
/data/st/Linux/c1:Get:3 https://download.docker.com/linux/ubuntu focal/stable amd64 conkages [10.7 kBtainerd.io amd64 1.4.9-1 [24.7 MB]
/data/st/Linux/c1:Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 doctainerd.io amdker-ce-cli amd64 5:20.10.8~3-0~ubuntu-focal [38.8 MB]
/data/st/Linux/c1:Get:5 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amdker-ce amd64 5:20.10.8~3-0~ubuntu-focal [21.2 MB]
/data/st/Linux/c1:Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5ker-ce-rootless-extras amd64 5:20.10.8~3-0~ubuntu-focal [7917 kB]
/data/st/Linux/c1:Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-rootlesker-scan-plugin amd64 0.8.0~ubuntu-focal [3889 kB]
/data/st/Linux/c1: OS/Arch:           linux/amd64                                      ker-scan-plugi
/data/st/Linux/c1:  OS/Arch:          linux/amd64
/data/st/Linux/file.txt:i love linux and grep


Advanced pattern matching with grep command in Linux

The grep command can also be used for advanced pattern matching.

This feature allows users to search for multiple patterns, use OR and AND operators in pattern matching, use wildcards in pattern matching, and use back-references in pattern matching. These features make the grep command a powerful tool for searching files for specific patterns.

Searching for multiple patterns (OR): grep -E “pattern1|pattern2” filename

You can also use the grep command to search multiple strings at the same time. This is also called the grep OR operation.

Grep Example 6: Search for the terms “Linux or linux” in the text file. Also, search for the term Linux or hello.

$ grep -E "Linux|linux" file.txt 
# Tutorial: grep command in Linux
i love linux and grep


$ grep -E "Linux|hello" file.txt
# Tutorial: grep command in Linux

In the above example, we searched for Linux or linux in the file, as they both were present, and we got the output, but in the second case hello was not in the file, so we only got a line containing Linux.


Using AND operators in pattern matching: grep -E “pattern1.*pattern2” filename

basically, there is no AND operator in grep, but, you can use the below pattern to make it like AND operation.

Grep Example 7: Search for the line containing both the terms grep and Linux.

$ grep -E "grep.*Linux" file.txt
# Tutorial: grep command in Linux


$ grep -E "love.*linux.*grep" file.txt
i love linux and grep


Wildcards in pattern matching: grep “str*ng” filename

You can also use a wildcard with grep. * is the most common wildcard used by system administrators and Linux enthusiasts.

Grep Example 7: Search for all the lines that have strings that start with l and end with x.

$ grep -i "l*x" file.txt 
# Tutorial: grep command in Linux
i love linux and grep

Match the Start of a line: grep ^string filename

You can also search for the lines which start with a particular string or character. This is very handy when sorting out all the comments in a file or just ignoring all the comments.

Grep Example 8: Find all the comments in a file

$ grep ^# file.txt 
# Ref: https://www.storagetutorials.com/
# Tutorial: grep command in Linux
# This is comment line

Grep Example 9: Find all the lines without comment or find all the uncommented lines.

$ grep -v "^#" file.txt
This is line number 1
this will be line 2
THIS is LINE THREE
5 - This line starts with number
i love linux and gre

Finding lines containing numbers or digits: grep [0-9] filename

Grep is a versatile tool and can also be used to find lines containing numbers.

Grep Example 10: Find all the line containing numbers

$ grep [0-9] file.txt
This is line number 1
this will be line 2
5 - This line starts with number


Grep and Output Manipulation: grep pattern filename | sed ‘s/foo/bar/g’

The grep command can also be used for output manipulation. We can use it with other commands like sed and awk to manipulate the output as needed.

Grep example 11: Find linux and replace it with Linux

$ grep linux file.txt | sed 's/linux/Linux/g'
i love Linux and grep

This command will search for the specified pattern in the file and replace all occurrences of “linux” with “Linux” in the output.

Note that it will not replace this in the original file. To save this output, please redirect the output to a new file using “>”.


Note that these examples are just a few of the many ways the grep command can be used. The options and syntax can vary depending on the specific use case.

In conclusion, mastering the grep command in Linux is an essential skill for anyone who wants to become a Linux power user. This command is a versatile tool that can be used for basic or advanced pattern matching, file searching, and more.

We hope this comprehensive guide has helped you understand the grep command in Linux better. By following the steps outlined in this guide, you can become more proficient in using the command to search for files, patterns, and strings in Linux. Remember to practice, and soon, you’ll be a Linux power user!

FAQs


Q: What is Grep used for in Linux?
A: Grep is used to search for specific strings or patterns within files and directories in Linux.

Q: How do I search for multiple patterns with Grep?
A: To search for multiple patterns with Grep, we can use the “-E” flag followed by the patterns we want to search for.

Q: How do I use the recursive search with Grep?
A: To use Grep to search for files or strings recursively, we can use the “-r” flag followed by the pattern we want to search for.

Q: Can I use Grep to extract data from files?
A: Yes, we can use Grep to extract data from files by piping the output to other commands like awk or sed.

Q: Is Grep case sensitive?
A: By default, Grep is case-sensitive. However, we can use the “-i” flag to make it case insensitive.

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.