Grep Command In Linux Syntax Options Examples More
What Is the grep Command in Linux
The grep command is a powerful command line tool in Linux used to search and filter out specific patterns or strings in a file, directory, or even in the output of other commands. You may be wondering about its unusual name; well it stands for “Global Regular Expression Print.” It was first introduced by Ken Thompson in 1973 for the Unix operating system. With its versatility and ease of use, the grep command is a must-have tool in every Linux user’s arsenal.
Generally, the grep command comes preinstalled on most Linux distros, but if you find it to be missing on your system, install it using the following commands:
Install on Debian-based systems:
sudo apt install grep
Install on Fedora-based systems:
sudo yum install grep
Install on Arch-based systems:
sudo pacman -S grep
Linux Grep command: Syntax & Options
Using the grep command in Linux is pretty straightforward, thanks to its simple syntax along with the multiple options to play with. The syntax to use the grep command is:
grep
How to Use the Grep Command in Linux
Using the grep command is easy, and it follows a simple syntax. All you need to do is provide the file name or directory you want to search for and the pattern you want to match. For the pattern, you can use the exact words or regular expressions. You must be wondering – what is “regular expression” here? Regular expressions are special strings that are interpreted in a different manner when used in specific areas. They get replaced with all the possible combinations matching the pattern. Say, for example, you want to match email addresses, you can use the regex “(.+)@(.+)\n“. Seems complicated? Let’s break down this:
(.+) matches any characters except new lines @ checks if the “@” symbol is present in the given sentence.
Now that you know what are regular expressions and how the grep command works, let’s now see how to use the grep command in Linux.
Search For Strings in Files with grep Command
The most common way to use grep is to search for specific strings in a file. The basic syntax to search with the grep command in files is:
grep <options <string_to_search> <file_name>
In the <string_to_search> part, you need to provide the exact string you want to search. For example, if you want to search for the string “mango” inside the file named fruits.txt, use this command:
grep “mango” fruits.txt
If you want to search while ignoring the case, then use the -i flag with the grep command for the above example:
grep -i “mango” fruits.txt
Search in Multiple Files with grep
With the grep command, you can not only search in a single file but also multiple files. This feature particularly becomes very handy when you need to go through multiple large files. The syntax to search for strings/patterns in multiple files with the grep command is:
grep
grep “John” student1.txt student2.txt
Search All Files in a Directory with grep
Suppose, you need to search for a string and you don’t remember the file name where it exists. You can obviously write all the filenames present in the directory, but instead, you can use simply use the “*” wildcard as shown:
grep
grep
grep “mango” *.txt
Using Regular Expression with grep
The main advantage of using the grep command over other tools/commands is the ability to process regular expressions to search for content. Simply include the -e flag with the grep command to search using regular expressions:
grep -e <pattern_to_search>
grep -e “(.+)@(.+)” emails.txt
Search for Multiple Keywords using grep Command
By now, you must be comfortable searching for some patterns or words using grep. But, what if you need to search for multiple words/patterns? Well, grep has got you covered for that too. To search for multiple words using the grep command, use this syntax:
grep “
grep “apple|mango” fruits.txt
Count Matching Results using grep Command
Sometimes, you may need to know the number of matching results. For this, you can use the -c flag with the grep command:
grep -c “pattern_to_search” filename
For example, if you need to see how many email IDs are there in the file student1.txt, use this command:
grep -E -c “(.+)@(.+)” student1.txt
If you want to see the number of lines not matching the search query, simply add the -v flag along with -c:
grep -v -E -c “(.+)@(.+)” student1.txt
Using grep Command with Shell Pipes
The way shell pipes work in Linux is they send the output of one command to another command as input. The basic syntax to use shell pipes is:
first_command | second_command
With shell pipes, filtering out some command output with grep command becomes way easier, instead of constant scrolling and looking for something. To filter out a specific word or pattern from a specific command output, simply replace <first_command> in the above syntax with the command, and add the second command with grep command’s syntax. Here’s how it works:
first_command | grep
history | grep gcc
This will show only the results containing the word “gcc,” as shown in the picture below.
Using grep Command in Linux
The grep command is an essential tool for users who want to search for specific patterns or words in various files, directories, or even in other Linux command outputs. Once you have got hold of all the essential options and syntax, the grep command can help you to increase your productivity exponentially. While you are here, check out the new features in Ubuntu 23.04.