Introduction to Grep(g/re/p) and 10 Useful Commands

Sridhar Rao
3 min readSep 14, 2020

Search is a big part of our modern-day lives. Anytime, we want to find some information online, we rely on web search engines to provide relevant search results. Search engines are highly optimized to provide good results based on a myriad of factors. More often than not, we do end up in situations, where we need to perform a search of our local files. If you are an application developer, dev-ops engineer, or a product owner, you may have felt the need to search more efficiently on your machine than relying on spotlight search.

Grep is a simple search utility, which is available in most Unix based operating systems. It searches for a string/pattern in a file or files and returns corresponding matches. It’s been around for ~45 years and can be extremely powerful. Grep is an acronym for global regular expression print. Over the years, its capabilities are extended. Such as fixed strings, extended regular expressions, etc.

Here are the top 10 most useful basic grep commands.

1. Search for a specific string in a file

This is a very common use case. By default, when a match is found, grep will print the whole line.

$ grep "string_to_search" filename

2. Search for a specific string in the current directory

This is useful, when we know the directory, but doesn’t know the exact file to search.

$ grep "string_to_search" *

3. Search all files in the current directory and subdirectories recursively

$ grep -r "string_to_search" *

Instead of the current directory, we could also specify the directory path. Ex: /root/

4. Search for a pattern based on regex

Having a good understanding of regular expressions makes this feature very powerful.

$ grep "lines.*empty" filename

Some common regex operators

  • . — matches any single character.
  • ? — The preceding item is optional and matched at most once.
  • * — The preceding item is matched zero or more times.
  • + — The preceding item will be matched one or more times.
  • {n} — The preceding item is matched exactly n times.
  • {n,} — The preceding item is matched n or more times.
  • {,m} — The preceding item is matched at most m times.
  • {n,m} — The preceding item is matched at least n times, but not more than m times.

5. Search with case insensitive and full word options.

By default, grep does a case-sensitive substring search. Those options could be changed by passing parameters to the search. “-i” is for case-insensitive and “-w” is for full word.

$ grep -iw "string_to_search" filename

6. Display additional context for search results

Sometimes to get a little more context about the search results, it is necessary to look at a few lines before and after the matching word line. You could do this with -A(after), -B(before), -C(Context or both) options.

$ grep -C 2 "string_to_search" filename

7. Display lines which doesn’t match the search term

This is inverse to the default search behavior. It’s pretty useful in doing log analysis.

$ grep -v "string_to_search" filename

8. Display only counts for the matches

$ grep -v -c "string_to_search" filename

9. Display only filenames for the matches

$ grep -r -l "string_to_search" /var/log/*

-L: is inverse for this. i.e. it prints names of files that do not contain matches.

10. Display original line numbers for the matches

$ grep -n "string_to_search" filename

Grep could also accept inputs from other commands or series of commands via a pipe, that comes in handy as well.

$ ifconfig | grep 192.168

--

--