Feb 22, 2024
How to Use the Linux Grep Command
At first glance, searching for files on a VPS may appear to be a simple task. It can, however, quickly become overwhelming, particularly if you have thousands of large log files to check or are running a Linux operating system without a graphical interface.
That’s where the grep command comes in, allowing users to easily search for a string within the system.
In this tutorial, we will go over the fundamentals of the grep command as well as some practical applications, such as searching for multiple strings and performing a grep search recursively.
What Is the grep Command in Linux
Grep, or global regular expression print, is one of the most versatile and useful Linux commands available. It searches for text and strings defined by users in a given file. Grep allows users to search files for a specific pattern or word and see which lines contain it.
For example, system administrators who handle hundreds of services and configuration files use grep to search for specific lines within those files.
To start using the grep command, connect to the VPS using SSH.
Linux users can just open the terminal.
grep Command Syntax
The grep basic syntax when searching for a single file looks like this:
grep [options] pattern [FILE]
- grep – the command instruction.
- [options] – modifiers to the command.
- pattern – the search query to be found.
- [FILE] – the file in which the command will be searching.
A visual example would look like this:
If you need more information about the command, check out the comprehensive documentation by executing the following command:
This will provide you with a list of all possible flags and their descriptions.
While the grep command offers a lot of options, the most important and commonly used flags are these:
- -A – print lines before the matched string.
- -B – print lines after the matched string.
- -C – print lines before and after the matched string.
ABC flags help you add some context to your searches. For example, when using grep in a configuration file, lines before or after your preferred string might provide more useful information about the search.
In the command example above, we used the regular grep utility, which only showed the Password line. Then, we combined the A1 flag to print out one additional line before the matched Password string. A similar process was done with B1 and C1 flags.
To customize your search even further, add the following flags:
- -i – case insensitive search. If users, for example, search for a string car, it will show the same results as CAR.
- -w – searches for full words only, ignoring your string if it’s a part of another word.
- -c – will show the number of matches with the searched pattern.
- -r – enables recursive search in the current directory.
- -n – search for lines and receive only the matched numbers of the text lines.
- -v – this option shows the lines that do not match the specified pattern.
grep Command Line Examples
Check out these useful examples of the grep command to understand it better.
Use grep to Search for a Word in a File
One popular use case for grep is searching for a particular word inside a text file.
To do so, just type the following command:
grep query file
- query – the word you’re looking for.
- file – the file in which you’re looking for the query.
In our case, we’re looking for the word VPS in the sample file called Hostinger.txt:
grep VPS Hostinger.txt
The output highlights the lines that match this query:
Use grep to Find a Keyword Match in Multiple Files
If you need to search through several files, refer to this syntax:
grep query file1 file2 file3
- query – the word you’re looking for.
- file1, file2, file3 – files in which you’re looking for the query.
In our example, we are looking for the word VPS in these three files: Hostinger.txt, VPS.txt, SharedHosting.txt.
It is also possible to search for all the files in a given directory by applying the following command.
grep query *
Use grep to Search for Multiple Keywords
So far, we’ve covered grep commands for matching a single keyword. However, grep also supports multiple queries in a single command.
Here are four different ways to search for multiple keywords:
grep 'query1\|query2' file egrep 'query1|query2' file grep -e query1 -e query2 file grep -E 'query1|query2' file
Any of these commands work similarly – you can decide which one to use according to your preference.
Use grep to Find Matches That Start or End With Query
Grep searches can also look for strings starting or ending with a user-specified query. To match the start of a line, use the ^ regular expression.
grep '^query2' file
On the other hand, to match the end of a line, use the $ regular expression:
grep '&query2' file grep
Users can also combine these two regular expressions and search for all the lines that contain both the start and end query.
For example, we will be searching for the lines that start with H and end with o:
As seen above, lines that have the Hello string match our search query.
Use grep to Display a Line Number with grep Command Search
Linux configuration files are usually lengthy, ranging from a few hundred to a few thousand lines. For this reason, it can be very hard to track lines position.
Luckily, grep can help. With the use of the -n flag, users will be able to see line numbers as well as their search query.
In our case, we used grep to search for line string inside the Grep.txt file. The green numbers on the left display line numbers on our file.
Use grep to Use Invert grep Search
Users can invert the grep expression by using the -v flag. This is very useful for printing out non-matching lines. The following example shows how the command displays output lines that are not matching the VPS search query.
We have also combined the regular expressions and used the -n flag to see the matching line numbers.
Use grep to Perform the grep Search Recursively
Recursive grep is useful for searching between all the lines in sub-directories and files inside the current working directory. For this example, we have created a Grep directory with VPS and SharedHosting directories inside it and will look for the VPS query.
Use grep to Export the grep Output to a File
If you need to save the grep command output, you can do so in a separate file. The easiest way is by referring to the following command:
grep query file > OutputFile.txt
Keep in mind that you do not need to create the file before, as running the above command will create it automatically. This is what it would look like in the command line:
As seen from the example above, we have also used the cat to print out the contents of the newly created file.
The grep command makes searching between hundreds of files and directories easy. With its vast collection of different flags, you will be able to find specific lines quickly.
In this tutorial, we’ve covered the syntax of the grep command and learned how to:
- Search for a word in a file.
- Find a keyword match in multiple files.
- Search for multiple keywords.
- Find matches that start or end with your query.
- Display a line number.
- Use inverted grep search.
- Perform recursive search.
- Export grep output contents to a file.
We’ve also provided some popular flags you can use when trying out the grep command. If you have any insights or questions, let us know in the comments section below.
Learn More About Linux Commands
How to Read the Content of a File
How to Read the End of a File
How to Read a File With Sed Command
How to Write and Display to File With Tee Command
How to List Services in Linux
How to Kill a Process in Linux
How to Use SCP Command to Copy and Transfer Files in Linux