How to Use grep Command in Linux/UNIX with Examples

grep-command-in-linux

The grep command in Linux is handy for searching for text patterns within files. Grep stands for “global regular expression print,” and its main purpose is to allow a user to search and match text patterns within files specified in the expression.

The grep command is available in all Linux distributions, and no installation is required. Moreover, the command has numerous options that you can use to enhance your search. This post focuses on these options to help you become comfortable using them.

Grep Command Syntax and Options

The grep command in Linux follows the below syntax.

  • The options represent the grep command flags you will add to modify your search.
  • The pattern represents the regular expression for your search.
  • The file represents the name of the file(s) to search within.

Below are some common grep command options you can use in your search.

OptionDescription
-iIgnores the cases when searching within files.
-rSearch through subdirectories.
-cDisplay a count of matched lines.
-vDisplay all lines that don’t match the pattern.
-nShow the matched lines and line numbers.
-lOnly show the filenames matching the expression.
-hIt only displays the matched lines, not the filenames.
-oOnly show the matched part of the line.
-fFetch the patterns from a specified file.
-wMatch the whole word in the search.
-qOnly confirm if there is a match without showing the results

Grep Command Examples

So far, we’ve introduced the grep command in Linux, given its syntax and some of the commonly used options. The next task is to cover some practical examples of how to run the command with different options.

Basic Usage of the Grep Command in Linux

The primary way of running the grep command is with no options. For instance, we are searching for “linux” in the names.txt with the command below.

The result will display all lines that match the specified pattern and highlight the matching pattern.

grep-command-in-linux

The -i option specifies that grep to search for the pattern within the file(s) and show results without considering the cases. Thus, if the pattern is “linux,” even “Linux” will be marched.

Notice how the output below shows results where the search pattern is in mixed cases, unlike our first example, which was case-sensitive.

grep-command-ignore-cases

Search Multiple Files

There are two ways to approach this. Firstly, you can add all the names of the multiple files you want to search through.

grep-command-search-multiple-files

Alternatively, you can use the asterisk (*) to search all the files in your current working directory.

grep-command-search-within-multiple-files

Search Through Subdirectories

Another way to search for a pattern using the grep command in Linux is to include subdirectories. This option assumes that your search pattern could be in a subdirectory and not its parent.

We added the -r option and used the asterisk to search all the files and subdirectories.

We now get a matching output in the Records/file1.txt, which we would have missed with the standard search.

grep-command-search-through-directories

Show Count of Matched Lines

Suppose you are only interested in knowing how many matches have been found. Adding the -c option does the trick.

It will only show the count of matched lines without displaying the matches.

We only have three matches for our example.

grep-command-show-count

Grep Command in Linux Display All Non-matching Lines

Instead of focusing only on the lines matching the search pattern, you can focus on those that don’t match. To do so, add the -v option; the results will exclude all the matching lines.

None of the lines below will match your pattern.

grepc-ommand-show-non-matching-lines

Include Line Numbers with the Grep Command in Linux

Sometimes, adding the line numbers for every matched line is handy, especially when you have a long file. Luckily, the -n will match every match with its line number for better results.

Such an instance is helpful when checking log files as you can quickly identify the pattern and access it in your log file using its line number.

The line number will be displayed on the left of every matched line in your output.

grep-command-show-line-numbers

Show Names of Matching Files

The grep command with the -l option will display the names of all the files that match your pattern. With this option, you won’t see the matched lines but will know which files contain your search pattern.

Here’s an example where we listed all files in our current directory that match our search pattern.

grep-command-show-names-of-matched-files

Search for the Entire Word within Files

Ideally, the grep command will match lines even if the search pattern is a substring. However, if you want to restrict the search to only display where the entire word is found and not as a substring, we will use the -w option.

The example below shows the results of our grep command in Linux without the -w and what we get after adding the -w. Notice the difference in the matched lines.

grep-command-search-for-entire-word

Only Show the Matched Pattern in the Output

Although grep typically shows the entire line containing the matched pattern, it’s possible to restrict this output and only display the matched pattern.

The -o gets the trick done, like in the example below.

grep-command-show-matched-pattern

Match Lines that Start with the Search Pattern

Still, on the grep command in Linux, you can set your search pattern to match only if it is found at the start of a line. It won’t be considered a match if it appears anywhere within the line.

Here’s how to implement this option.

We’ve also included the line numbers in our output.

grep-match-lines-that-start-with-pattern

Match Lines that End with the Search Pattern

Similar to how we’ve matched lines that start with the search string, we can do the same for lines ending with the search pattern.

Here, we will add our sign($) at the end of the search pattern, as demonstrated below.

grep-match-lines-that-end-with-pattern

Source Search Patterns from a File

Instead of manually adding the search pattern when running the grep command, it’s possible to source the patterns line by line from a file.

Suppose we want to use the three search strings in our file below.

grep-pattern-source-file
Read Also: How to use the cat command in Linux.

We could run our command with the -f flag, as shown below.

grep-pattern-add-source-file

Limit Output When Running the Grep Command in Linux

When you run the grep command, it will display all lines matching the search pattern.

grep-command-no-limit

However, you may have a case where you only want to view a specific number of outputs instead of all the matched lines.

In that case, add the -m with the number of lines to display. For instance, -m4 will limit the output to four lines.

grep-command-limit-output

Utilizing Multiple Search Patterns

It’s possible to run the grep command with multiple search patterns. The output will display all lines where any of the search patterns match. Add the -e option before every search pattern.

Here’s an example.

grep-command-with-multiple-patterns
Read Also: How to search for files using the find command in Linux.

Conclusion

The grep command in Linux is convenient for searching for patterns within files. You can utilize the grep command to achieve your search goal in numerous ways. This post has covered some of these ways and given practical examples to clarify doubts and get you comfortable working with the grep command.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top