Using grep on Files That Match Specific Criteria


Grep (global regular expression print) command matches and searches the specific pattern in the regular expressions. This command filters and searches for a particular pattern of characters and displays them as output. It is considered one of the most useful commands on Unix / Linux-like systems for sysadmins and developers.

Linux contains various types of commands and utilities to simplify every task. Unlike other operating systems, finding any file in Linux is simple because you can use the grep command to search any file.

You can use the grep command to display the specific file's name that contains a particular pattern you are looking for. However, there are a few things that you should follow while using the grep command in Linux −

  • Grep is a case-sensitive command by default. ,That's why you must use the -i option to match the case-insensitive pattern.

  • You must use quotation marks in the grep command to search for a character with a special meaning to the shell.

  • The Grep command displays the whole line that contains the specific character. So you have to use the -o option only to display the name of the file and the searched character.

In this short guide, we will use a grep on files that match specific criteria on Linux.

Using Grep on Files that Match Specific Criteria on Linux

The grep command is pre-installed in most Linux distributions. Let's create some sample files in the 'Documents' directory and match for a string or a regular expression in those files.

~$: sudo tree Documents 
Documents
|__ sample1
|__ sample2
|__ sample3

0 directories, 3 files

Now, we will search for these sample files through the different options of the grep command in Linux.

Matching the Case-Sensitive Search on Files

It comes under case-sensitive search if you have to find exact words and patterns using grep on files.

grep <pattern> <filename1> <filename2>.....

Here we will search for the word "unix" among all the files in the Documents directory.

For this, use the below command −

~$: grep unix sample1 sample2 sample3
sample1:unix
sample3:unix operating system

The above command matches the mentioned pattern and displays the files containing "unix".

Matching the Case-Insensitive Search on Files

You can search for any pattern insensitively using the -i option with the above command −

grep -i <pattern> <filename1> <filename2>.....

Let's display all the results related to 'unix', but the terminal will show all files containing 'Unix' or 'unix.'

~$: grep -i "unix" sample1 sample2 sample3
sample1:unix
sample1:Unix
sample2:Unix
sample3:unix operating system

Searching Just the Matched Pattern

The grep command displays the entire paragraph or line by default. You can show only the pattern instead of the entire passage or line by using the -o option with the grep command.

grep -o "<pattern>" <filename>

Here, we will search "Linux" through the grep command and its -o option −

~$: grep -o "Linux" sample1 sample3
sample1:Linux

Searching the File Names that Match the Pattern

If you want to search files containing a specific pattern or string, use the following command −

grep -l "<pattern>" <filename1> <filename2>.....

Or,

grep -l "<pattern>" *

For instance, the below command will show the file's name in the current directory containing the word "linux" −

~$: grep -l "linux" *
sample1
sample3

Matching Any Pattern

Use the following grep command to display specific characters that match any word, pattern, etc., in the files.

grep "..<letter_pattern>" <filename>

Here, the below command shows both the patterns 'Linux' and 'Unix,' which ends with 'x.'

~$: grep "..x" sample1 sample3
sample2:Linux
sample2:Linux
sample2:Unix
sample3:linux
sample3:unix operating system.

Matching the Inverting Pattern

You can match only lines that are not similar to the mentioned pattern. In this case, you can use the -v option with the grep command as follows −

grep -v "<pattern>" <filename>

For example, we will search for those files which do not contain the word "Linux".

~$: grep -v "Linux" sample1 sample2 sample3
sample1:GNU operating system
sample1:gnu
sample1:Gnu
sample:unix
sample1:linux
sample1:unix
sample2:GNU
sample2:unix
sample3:linux
sample3:unix operating system.
sample3:gnu operating system.

Matching the Pattern that Starts with a Specific Word / String

The regular expression pattern '^' defines the beginning of a passage or line. Using the following grep command, you can display the lines starting with a specific pattern or string.

grep "^<pattern>" <filename>

In the following command, we will use the grep command to search "gnu" in the files −

~$: grep "^gnu" sample2 sample3
sample3:gnu operating system.

Matching the Pattern that Ends with a Specific Word / String

The regular expression pattern '$' defines the end of a passage/line. Using this expression with the grep command, you can match the lines or passages which end with the mentioned pattern/word.

grep "<pattern>$" <filename>

For example, we will search "system" in multiple files −

~$: grep "system$" sample1 sample2 sample3
sample3:unix operating system.
sample3:gnu operating system.

Matching Recursively for a Pattern in the Directory

If you want to match a pattern within the entire directory instead of specific files in the directory, you can use the -R option with the grep command like this −

grep -R <pattern> <directory>

Now, we will run the grep command recursively to find "GNU" containing files in the Documents directory −

~$: grep -R GNU Documents
Documents/sample2:GNU
Documents/sample1:GNU operating system

Bonus Tip − If you want to match a specific pattern among all the files present in the current directory, you can do so by using the -w option with the grep command as follows −

grep -w <pattern> *

Matching files with Specific Extensions

Use the following command to search for a pattern in files with a specific extension included in a directory

grep -R --include=*.<file_extention> '<pattern>' <directory>

In the below example, the grep command checks the mentioned pattern only in files with the '.txt' extension.

~$: grep -R --include=*.txt 'Linux' Documents
Documents/sample1.txt:Linux is a kernel

Note − You can also use the above command to match patterns in files with multiple extensions at once.

Conclusion

The grep command is an excellent way to filter files with specific patterns through the terminal in Linux. In this guide, we have explained the ways to match specific criteria in files through various examples. Using the grep command is very easy, and you can also see it through some of the above examples. Thus you can use the grep command to search for any pattern, line, word, etc., in Linux in just a few clicks.

Updated on: 18-May-2023

879 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements