Linux is a very secure operating system, which is often used by organizations to host business applications. Such business applications generate large amounts of data, such as large log and trace files. It is very difficult to find certain information in multiple files, unless you use a specialized utility, such as the grep command.
Grep or Global Regular Expression Print is a built-in Linux command line utility that lets you search for a word or string in a specified file. The search pattern of the grep commands is called a regular expression. It is very useful for developers and system administrators to search for all the files that contain given variables, parameters, timestamps, words, or any other such information.
In this tutorial, we learn about grep command in Linux and go through its main option with examples.
- A computer running Linux operating system.
- Basic understanding of Linux command line interface.
- A few files with some text in them to try these examples.
- Eagerness to learn new Linux commands.
What is Grep Command in Linux?
Grep is a built-in command line Linux utility that is used to find words, text strings, number strings, or any other information in a single file or multiple files. You can use various options with this command to find words or strings that meet your specific criteria.
The grep command supports part of words, whole words, strings, case sensitive search, case insensitive search, subdirectories, inverse search, and exact match options. It prints matching line of the given word or string.
By default, the grep command searches only the matching case words or strings.
Grep Command Syntax
This is the basic syntax of the grep command:
grep [options] word|”string” filename1 filename2
Grep Command Options
The following table lists the main options of the grep commands:
|Ignore case in search results.|
|Find only the whole words.|
|Display non-matching lines.|
|Display line number for matching lines.|
|Perform recursive search.|
|Print only the file names.|
|Show only a count of the number of matches.|
|Highlight matched string in different colors.|
How to use the Grep Command in Linux?
The grep command provides options to search for a given word or string in the specified file, a series of files, or a given directory and its subdirectories. Refine the search options to control the output of the grep command. The most common use of the grep commands is to search for a word in a file.
For example, use the following command to search for the word
Linux in the
grep Linux sample.txt
The grep command is case sensitive and you must always either define the correct case of the word or string that you search, or use the
-i option to ignore the case.
For example, if you do not specify the correct case in the previous example, grep output comes empty because the input file does not contain the word
linux in lowercase:
The search word and file name or directory name are the mandatory arguments.
Linux Grep Command Examples
You can use the grep command options in different combinations to narrow down your search results. The following examples show different use cases of the grep command:
Find Text in a File
The most common and the simplest use of the grep command is to search for a word in a single file or multiple files.
The following example shows how to search for the word Linux in the files
grep Linux sample.txt test.txt
In case you do not remember the complete word and just remember only a few characters of the word, then also you can use the grep command to search for the part of the word:
Find Whole Words Only
Your computer has different types of files, including log files, text files, application files, code files, and trace files. For example, a text file may contain a word operator and a code file may contain a variable oper. In such a case, use the grep command to search only for the whole word.
The following example shows how to search for the whole words that matches the given word:
grep -w operating sample.txt
If the given word does not match any whole word in the file, then the grep command does not return any results or error messages:
Ignore Cases in Grep Searches
Grep is a case sensitive command and thus to search for the words that matches all cases you can use the
-i option to ignore cases in your search results.
The following example shows how to search for the word Linux in both lowercase and title case:
grep -i Linux sample.txt test.txt
In this example, grep displays both Linux and linux, and ignores the case.
Inverse Search Using Grep
The grep command works in two ways: search for the given text or print lines that do not contain the given word. The
-v option in the grep command inverts the grep output.
In the following example, grep prints all the lines that do not contain the word Linux.
grep -v command sample.txt test.txt
When you do an inverse search using the grep command, then you also get the results that do not match with the case of the given word:
As discussed earlier, grep is a case-sensitive command and you must be very careful with the casing to get the correct command output.
Search String in Multiple Files
The grep command is not just to search files for a given word. This command can also be used to search for a given string, which developers and system administrators may need to do more often in a production environment.
The following example shows how to search string in a given file:
grep “Linux operating system” sample.txt test.txt
List FileNames Containing the Search String
Sometimes you do not need to search for the given word in the files, but just the number of files that contain the given word. The grep command provides an option to list the filenames that match the search word.
The following output data shows the name of the files that contain the word Linux in the current directory:
grep -l Linux *
Count Number of Matches in Each File
In the previous example, you saw how to print the file names that contain the given word. However, it does not show how many times the given word was repeated in each file.
The following example shows how to list all the files and the number of instances that matches the given word in the current directory:
grep -c Linux *
Display Line Numbers with Matched Lines
When the grep command lists multiple results, it is helpful to have line numbers next to the matching lines that include the given word. This option is also helpful when counting the number of lines in a code file.
The following example shows matched lines with line numbers:
grep -n -C 2 Linux sample.txt
Find Two Different Words with Grep
The grep command can be used to find either a word, or a string in given files. However, there may be cases when you need to find multiple words, for example two strings in a binary file. In such cases, use the egrep command, which belongs to the grep command line utility family and searches for patterns.
In the following example, egrep command searches for words secure and strong in the given file and shows numbers for the matched lines:
egrep -n ‘secure|strong’ sample.txt
In this tutorial, you learned how to use the grep command to search for a given word in a single text file or multiple files. The grep command is very powerful and can be used to search for a word or term in thousands of files. It provides options to narrow down the search results as explained in the examples of this tutorial.