Why Is Grep Returning No Matches?

Grep returns no matches

The grep (“go to regular expression and print”) command line utility is one of the most important programs in your Unix toolkit, and indispensable when managing linux VPS servers. Grep is a powerful search tool that can help you find patterns in critical files (and directories). It can also search recursively within multiple files to return results based on regular expressions.

Basics of Grep

When first starting out with grep it can be confusing to figure out what kind of options and inputs it takes. What most people start out doing is concatenating (with cat) file contents and piping (|) then into grep.

cat file | grep "search pattern"

But this is not actually necessary, because grep is capable of performing its functions without the extra step of concatenating input.

grep "search pattern" file

However, if your goal is to concatenate (cat) different files and then search the contents then piping into grep is not a bad idea. However, you should note that grep can search all files within a directory recursively by using the -r option. For example:

grep -r "search pattern" /directory/*

Why Is Grep Returning No Matches?

It can be frustrating to troubleshoot with grep when you don’t know why it’s returning no matches. Here are a few troubleshooting steps you can take to isolate the issue.

Remember Case Sensitivity

Don’t forget that case sensitivity is a factor, especially when you are dealing with UNIX-like systems. But often, there may be situations in which you can’t recall the case of the character you are searching for, or you want to capture all instances of a word or string of text regardless of case. In that case, you should use the -i option. For example:

grep -i "<case insensitive search pattern>" file

Is It a Directory?

When in doubt, check the error you are getting and try to work through what it is trying to communicate to you. Take a look at the following example. The first line of the example is the command, the second line is the error it generates:

chris@server:~$ grep "search" ~/Downloads/
grep: /home/chris/Downloads/: Is a directory

The first part of the error merely prints out “grep” to let you know there is an error with grep itself. Then it prints out the directory, followed by “Is a directory.” This error indicates that you are trying to run grep on a directory. Grep is not meant to work that way. Grep can perform search operations on input, for example, when you are piping into, or it works upon a file or series of files as such.

If your intention in the above command example was to search instead for patterns within files inside of the “Downloads” directory, then all you need to do is add the * in front of the directory path, as in the following example:

grep "search" ~/Downloads/*

To put the full command and output in context:

chris@server:~$ grep "search" ~/Downloads/*
search pattern

Notice now that the grep output has come back with a match. Remember you can always find complete Linux tutorials available in the support center.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!