Home A-Z Commands 50 Productive and Practical grep Command for Linux Enthusiasts

50 Productive and Practical grep Command for Linux Enthusiasts

The grep tool in Linux and other Unix-like systems is one of the most powerful command-line tools ever developed. It dates back to the ed command g/re/p and was created by the legendary Ken Thompson.

If you’re a seasoned Linux user, you’ll know the importance of regular expressions in file processing. However, many starting users don’t have a clue about them. We often see users being uncomfortable using such techniques.

However, most grep commands aren’t that complex. You can easily master grep by giving it some time. If you want to become a Linux guru, we recommend you utilize this tool in everyday computing.

Essential grep Commands for Modern Linux Users


One of the most beautiful things about the Linux grep command is that you can use it with all sorts of things. You can grep for patterns directly in files or from your standard output. It allows users to pipe other command’s output to grep and locate specific information. The following commands will outline 50 such commands.

Demo Files for Illustrating Linux grep Commands


Since the Linux grep utility works on files, we’ve outlined some files you can use for practicing. Most Linux distributions should contain dictionary files in the /usr/share/dict directory.

We’ve used the american-english file found here for some of our demonstration purposes. We’ve also created a simple text file containing the following.

this is a sample file
it contains a collection

of lines to demonstrate
various Linux grep commands

We named it test.txt and have used in for many grep examples. You can copy the text from here and use the same filename for practicing. Additionally, we’ve also leveraged the /etc/passwd file.

intro to linux grep command

Basic grep Examples


Since the grep command allows users to dig out information using many combinations, starting users are often confused with its usage. We demonstrate some basic grep examples to help you familiarize yourself with this tool. It will help you learn more advanced commands in the future.

1. Locate Information in a Single File


One of the basic uses of grep in Linux is to locate lines containing specific information from files. Enter the pattern followed by the filename after grep, as shown below.

$ grep root /etc/passwd
$ grep $USER /etc/passwd

The first example will display all lines containing root in the /etc/passwd file. The second command will display all such lines that contain your username.

2. Locate Information in Multiple Files


You can use grep to print lines containing specific patterns from more than one file at the same time. Provide all filenames separated by whitespace after the pattern. We’ve copied test.txt and created another file containing the same lines named test1.txt.

$ cp test.txt test1.txt
$ grep file test.txt test1.txt

Now, grep will print all lines containing files from both of the files.

3. Print Matched Portion Only


By default, grep will display the entire line containing the pattern. You can suppress this output and tell grep to display only the matched portion. So, grep will only output the specified patterns if it exists.

$ grep -o $USER /etc/passwd
$ grep --only-matching $USER /etc/passwd

This command will output the value of $USER as many times grep encounters it. The output will be empty if no match is found, and grep will terminate.

4. Ignore Case Matching


By default, grep will search for the given pattern case-sensitively. Sometimes, the user might not be sure of the pattern’s case. You can tell grep to ignore the case of the pattern in such cases, as demonstrated below.

$ grep -i $USER /etc/passwd
$ grep --ignore-case $USER /etc/passwd

$ grep -y $USER /etc/passwd

This returns an extra line of output in my terminal. It should also be the same on your machine. The last command is obsolete, so avoid using that one.

5. Invert Matching grep Patterns


The grep utility allows users to invert matching. It means that grep will print all lines that do not contain the given pattern. Check out the below command for a quick view.

$ grep -v file test.txt
$ grep --invert-match file test.txt

The above commands are equivalent and print only those lines that do not contain the file.

6. Match Whole Words Only


The grep utility prints any line containing the pattern. So, it will also print lines that have a pattern inside arbitrary words or sentences. Often, you’ll want to discard these values. You can easily do this using the -w option, as shown below.

$ grep hello /usr/share/dict/american-english
$ grep -w hello /usr/share/dict/american-english

If you run them one after another, you’ll see the difference. In my system, the first command returns 5 lines, whereas the second command returns only two.

7. Count the Number of Matches


Often, you might want the number of matches found using some pattern. The -c option is very handy in such situations. When used, grep returns the number of matches instead of printing the lines. We’ve added this flag to the above commands to help you visualize how this works.

$ grep -c hello /usr/share/dict/american-english
$ grep -c -w hello /usr/share/dict/american-english

The commands return 5 and 2, respectively.

8. Display Line Number


You can instruct grep to display the line numbers where a match has been found. It utilizes a 1-based index where the first line of the file is line number 1, and the tenth line is line number 10. Take a look at the below commands to understand how this works.

$ grep -n -w cat /usr/share/dict/american-english
$ grep --line-number -w cat /usr/share/dict/american-english

Both the above commands print the lines that contain the word cat in the american-english dictionary.

9. Suppress Filename Prefixes


If you run the examples of the second command again, you’ll notice that grep prefixes the output with the filenames. Often, you may want to ignore them or omit them altogether. The following Linux grep commands will illustrate this for you.

$ grep -h file test.txt test1.txt
$ grep --no-filename file test.txt test1.txt

Both commands are equivalent, so you can choose whichever you want. They will only return the lines with the matched pattern, not the filenames.

10. Display Filename Prefixes Only


On the other hand, sometimes you may only want the filenames that contain some pattern. You can use the -l option for this. The long form of this option is --files-with-matches.

$ grep -l cat /usr/share/dict/*-english
$ grep --files-with-matches cat /usr/share/dict/*-english

Both the above commands print out the filenames that contain the pattern cat. It shows the American-English and British-English dictionaries as grep’s output in my terminal.

11. Read Files Recursively


You can tell grep to recursively read all the files in a directory using the -r or --recursive option. This will print out all lines that contain the match and prefix them with the filenames where they were found.

$ grep -r -w cat /usr/share/dict

This command will output all files that contain the word cat in it alongside their filenames. We use the /usr/share/dict location since it contains multiple dictionary files. The -R option can allow grep to traverse symbolic links.

12. Display Matches with the Whole Pattern


You can also instruct grep to display only those matches that contain the exact match in the entire line. For example, the below command will generate lines containing only the word cat.

$ grep -r -x cat /usr/share/dict/
$ grep -r --line-regexp cat /usr/share/dict/

They return the three lines that contain just cat in my dictionaries. My Ubuntu 19.10 has three files in the /dict directory containing the word cat in a single line.

Regular Expressions in Linux grep Command


One of the most compelling features of grep is its ability to work with complex regular expressions. We’ve only seen some basic grep examples illustrating many of its options. However, the ability to process files based on regular expressions is far more demanding. We’ll stick with simple examples since regular expressions require thorough technical study.

13. Select Matches at the Beginning


You can use grep to specify a match at the start of a line only. This is called anchoring the pattern. You’ll need to utilize the caret ‘^’ operator.

$ grep "^cat" /usr/share/dict/american-english

The above command will print all the lines in the Linux american-english dictionary that starts with cat. We didn’t use quotes to specify our patterns until this part of our guide. However, we’ll use them from now on and recommend you use them.

14. Select Matches at Ending


Similarly to the above command, you can anchor your pattern to match lines containing a pattern at the end. Check out the below command to understand how this works in Linux grep.

$ grep "fish$" /usr/share/dict/american-english

This command will print out all lines that end in fish. Notice how we’ve used the $ symbol at the end of our pattern.

15. Match a Single Character


The Unix grep utility allows users to match any single character as part of the pattern. The dot ‘.’ operator is used for this purpose. Take a look at the below examples for a better understanding.

$ grep -x "c.t" /usr/share/dict/american-english

This command will print all lines containing three character words beginning with c and ending with t. If you omit the -x option, the output will grow larger since grep will display all lines with any combination of these characters. You can use double .. to specify two random characters and such.

16. Match from a Set of Characters


You can also choose from a set of characters easily using brackets. It tells grep to select characters based on some criteria. You’ll usually use regular expressions to specify these criteria.

$ grep "c[aeiou]t" /usr/share/dict/american-english 
$ grep -x "m[aeiou]n" /usr/share/dict/american-english

The first example will print all lines in the american-english dictionary, which contain the pattern c followed by a single vowel and the character t. The next example will print all exact words that contain m followed by a vowel, then n.

17. Match from a Range of Characters


The following commands will demonstrate how you can match from a range of characters using grep. Try out the commands on your own to see how things work.

$ grep "^[A-Z]" /usr/share/dict/american-english
$ grep "[A-Z]$" /usr/share/dict/american-english

The first example will print out all lines that start with any capital letter. The second command displays only those lines that end with a capital letter.

18. Omit Characters in Patterns


Sometimes, you may want to search for patterns that do not contain some specific character. We’ll show you how to do this using grep in the next example.

$ grep -w "[^c]at$" /usr/share/dict/american-english
$ grep -w "[^c][aeiou]t" /usr/share/dict/american-english

The first command displays all words ending with at except cat. The [^c] tells grep to omit the character c from its search. The second example tells grep to display all words that end with a vowel followed by t and doesn’t contain c.

19. Group Characters inside the Pattern


The [] only allows you to specify a single character set. Although you can use multiple bracket sets to specify additional characters, it’s unsuitable if you already know what character groups you’re interested in. You can use the () to group multiple characters in your patterns.

$ grep -E "(copy)" /usr/share/dict/american-english
$ egrep "(copy)" /usr/share/dict/american-english

The first command outputs all lines that have the character group copy in them. The -E flag is required. You can use the second command, egrep if you want to omit this flag. It’s simply an extended front-end for grep.

20. Specify Optional Characters in Pattern


The grep utility also allows users to specify optional characters for their patterns. You’ll need to use the “?” symbol for this. Anything preceding that character will be optional in your pattern.

$ grep -E "(commu)?nist" /usr/share/dict/american-english

This command will print the word communist alongside all lines in the dictionary that contain nist in them. See how the -E option is used here. It enables grep to perform more complex or extended pattern matching.

21. Specify Repetitions in Pattern


You can specify how often a pattern must be matched for certain grep commands. The following commands show you how to select the number of characters from a class for grep patterns.

$ grep -E "[aeiou]{3}" /usr/share/dict/american-english
$ grep -E "c[aeiou]{2}t" /usr/share/dict/american-english

The first example will print all lines that contain three vowels, whereas, on the other hand, the last example prints all lines containing c followed by 2 vowels, then t.

22. Specify One or More Repetitions


You can also utilize the “+” operator included in grep’s extended feature set for specifying a match one or more times. Check out the following commands to see how this works in the Linux grep command.

$ egrep -c "[aeiou]+" /usr/share/dict/american-english
$ egrep -c "[aeiou]{3}" /usr/share/dict/american-english

The first command prints out the number of times grep encounters one or more consecutive vowels. The second command shows how many lines contain three consecutive vowels. There should be a big margin of difference.

23. Specify Lower Bound for Repetitions


You can select higher and lower bounds for the number of match repetitions. The next examples demonstrate how to select lower bounds in action.

$ egrep "[aeiou]{3,}" /usr/share/dict/american-english

We’ve used egrep instead of grep -E for the above command. It selects all lines that contain 3 or more consecutive vowels.

24. Specify Upper Bound for Repetitions


As with lower bounds, you can also tell grep how many times to match certain characters at most. The following example matches all lines in the american-english dictionary that contain up to 3 vowels.

$ egrep "[aeiou]{,3}" /usr/share/dict/american-english

We recommend users use egrep for these extended functionalities since it is somewhat faster and more of a convention nowadays. Notice the placement of the comma ‘,’ symbol in the two aforementioned commands.

25. Specify Upper and Lower Bound


The grep utility also enables users to select both the upper bound and lower bound for match repetitions at the same time. The following command tells grep to match all words containing a minimum of two and, at most, four consecutive vowels.

$ egrep "[aeiou]{2,4}" /usr/share/dict/american-english

This way, you can specify both upper and lower limits at the same time.

26. Select All Characters


You can use the wildcard character ‘*’ to select all zero or more occurrences of a character class in your grep patterns. Check out the next example to understand how this works.

$ egrep "collect*" test.txt 
$ egrep "c[aeiou]*t /usr/share/dict/american-english

The first example prints out the word collection since it’s the only word that matches ‘collect’ one or more times in the test.txt file. The last example matches all lines containing c followed by any number of vowels, then t in the Linux american-english dictionary.

27. Alternate Regular Expressions


The grep utility allows users to specify alternating patterns. You can use the “|” character to instruct grep to select one of two patterns. This character is known as the infix operator in POSIX terminology. Take a look at the below example to understand its effect.

$ egrep "[AEIOU]{2}|[aeiou]{2}" /usr/share/dict/american-english

This command tells grep to match all lines that contain 2 consecutive capital vowels or small vowels.

28. Select Pattern for Matching Alphanumeric Characters


Alphanumeric patterns contain both digits and letters. The below examples demonstrate how to select all lines that contain alphanumerics using the grep command.

$ egrep "[0-9A-Za-z]{3}" /usr/share/dict/american-english
$ egrep "[[:alnum:]]{3}" /usr/share/dict/american-english

Both of the above commands do the same thing. We’re telling grep to match all lines containing three consecutive character combinations of 0-9, A-Z, and a-z. However, the second example saves us from writing the pattern specifier ourselves. This is called a special expression, and grep offers several of them.

29. Escape Special Characters


Till now, we’ve used many special characters such as “$”, “^”, and “|” for defining extended regular expressions. But what if you need to match any of those characters inside your pattern?

Luckily, the developers of grep have already thought of that and are allowed to escape these special characters using the backslash "\".

$ egrep "\-" /etc/passwd

The above command matches all lines of the /etc/passwd file against the hyphen "-" character and prints them. You can escape any other special characters using a backslash in this way.

30. Repeat grep Patterns


You’ve already used the “*” wildcard to select character strings in your patterns. The next command shows you how to print out all lines that start with parentheses and only contain letters and a single whitespace. We’ll use “*” to do this.

$ egrep "([A-Za-z ]*)" test.txt

Now add some lines enclosed within parentheses inside your demo file test.txt and run this command. You should get the hang of this command already.

Linux grep Commands in Everyday Computing


One of the best things about grep is its universal applicability. You can use this command to filter out essential information when running important Linux terminal commands. Although the below section provides you with a quick glimpse into some of them, you can apply the core principles anywhere.

31. Display all Sub-directories


The following command illustrates how we can use grep to match all folders inside a directory. We’re using the ls -l command to display the contents of the directory in the standard output and cutting the matching lines with grep.

$ ls -l ~ | grep "drw"

Since all directories in Linux contain the pattern drw in their beginning, we’re using this as our pattern for grep.

32. Display all Mp3 Files


The following command demonstrates how to use grep to locate MP3 files on your Linux machine. We’ll be using the ls command again here.

$ ls /path/to/music/dir/ | grep ".mp3"

First, ls will print the contents of your music directory to the output, and then grep will match all lines that contain .mp3 in them. You’ll not see the output of ls since we’ve piped this data to grep directly.

33. Search Text in Files


You can also utilize grep to search specific text patterns in a single file or collection of files. Suppose you want to locate all C program files that contain the text main in them. Don’t worry about this, and you can always grep for it.

$ grep -l 'main' /path/to/files/*.c

By default, grep should color code the match portion to help you easily visualize your findings. However, if it fails to do so in your Linux machine, try adding the –color option to your command.

34. Find Network Hosts


The /etc/hosts file contains information like host IP and hostname. You can use grep to find specific information from this entry using the below command.

$ grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" /etc/hosts

Don’t be alarmed if you don’t get the pattern right away. If you break it down one by one, it’s very easy to understand. Actually, this pattern searches for all matches in the range 0.0.0.0 and 999.999.999.999. You can also search using hostnames.

35. Find Installed Packages


Linux sits on top of several libraries and packages. The dpkg command-line tool allows admins to control packages on Debian-based Linux distros such as Ubuntu. You’ll see below how we use grep to filter out essential information about a package using dpkg.

$ dpkg --list | grep "chrome"

It brings out several useful pieces of information on my machine, including the version number, architecture, and description of the Google Chrome Browser. You can use it to find similar information for packages installed in your system.

36. Find Available Linux Images


We’re using the grep utility once more with the dpkg command to find all available Linux images. The output of this command will vary widely across systems.

$ dpkg --list | grep linux-image

This command simply prints out the result of dpkg --list and feeds it to grep. It then matches all lines for the given pattern.

37. Find Model Information for the CPU


The below command demonstrates how to locate CPU model information in Linux-based systems using the grep command.

$ cat /proc/cpuinfo | grep -i 'model'
$ grep -i "model" /proc/cpuinfo

In the first example, we’ve piped the output of cat /proc/cpuinfo to grep and matched all lines containing the word model. However, since /proc/cpuinfo is itself a file, you can use grep directly on it, as shown by the latter example.

38. Find Log Information


Linux saves all sorts of logs in the /var directory for us system admins. You can easily grep useful information from these log files. The below command demonstrates a simple such example.

$ grep -i "cron" /var/log/auth.log

This command will inspect the /var/log/auth.log file for potential lines that contain information about Linux CRON jobs. The -i flag allows us to be more flexible. Running this command displays all lines with the word CRON in the auth.log file.

39. Find Process Information


The next command will demonstrate how to locate useful information for system processes using grep. A process is the running instance of a program in Linux machines.

$ ps auxww | grep 'guake'

This command will print all information related to the guake package. Try with some other package if guake isn’t available in your machine.

40. Select Valid IPs Only


Earlier, we’ve used a relatively simpler regular expression for matching IP addresses from the /etc/hosts file. However, that command would also match a lot of invalid IPs since valid IPs can only take the values from the range (1-255) in each of their four-quadrant.

$ egrep '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' /etc/hosts

The above command will not print any invalid IP addresses like 999.999.999.999.

41. Search Inside Compressed Files


The zgrep front-end of the Linux grep command allows us to search for patterns directly in compressed files. Take a quick look at the following code snippets for a better understanding.

$ gzip test.txt
$ zgrep -i "sample" test.txt.gz

First, we’re compressing the test.txt file using gzip and then utilizing zgrep to search it for the word sample.

42. Count the Number of Empty Lines


You can easily count the number of empty lines in a file using grep, as shown in the next example.

$ grep -c "^$" test.txt

Since test.txt contains only a single empty line, this command returns 1. The empty lines are matched using the regular expression "^$" and their count is printed by leveraging the -c option.

43. Find Multiple Patterns


Till now, we’ve focused on finding a single pattern. The grep utility also enables users to search for lines with multiple patterns at the same time. Take a look at the below example commands to see how this works.

$ grep -e 'sample' -e 'file' test.txt
$ grep -e 'sample' test.txt | grep -e 'file'
$ grep -e 'sample\| file' test.txt

All of the above commands will print the lines that contain both ‘sample’ and ‘file’.

44. Match Valid Email Addresses


Many seasoned programmers like to validate user input themselves. Luckily, it’s very easy to validate input data like IP and emails using grep regular expressions. The following command will match all valid email addresses.

$ grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" /path/to/data

This command is extremely efficient and matches up to 99% valid email addresses at ease. You can use egrep to speed up the process.

Miscellaneous grep Commands


The grep utility offers many more useful command combinations that enable further operations on data. We’re discussing a few seldom used but essential commands in this section.

45. Select Patterns from Files


You can select your regular expression patterns for grep from pre-defined files quite easily. Use the -f option for this.

$ echo "sample"> file
$ grep -f file test.txt

We’re creating an input file containing one pattern using the echo command. The second command demonstrates file input for grep.

46. Control Contexts


You can easily control grep’s output context using the options -A, -B, and -C. The following commands show them in action.

$ grep -A2 'file' test.txt
$ grep -B2 'file' test.txt
$ grep -C3 'Linux' test.txt

The first example shows the next 2 lines after the match, the second example shows the previous 2, and the last example shows both.

47. Suppress Error Messages


The -s option allows users to suppress the default error messages shown by grep in case of nonexistent or unreadable files.

$ grep -s 'file' testing.txt
$ grep −−no-messages 'file' testing.txt

Although there’s no file named testing.txt in my working directory, grep doesn’t issue any error message for this command.

48. Display Version Information


The grep utility is much older than Linux itself and dates back to the early days of Unix. Use the next command if you want to get grep’s version information.

$ grep -V
$ grep --version

49. Display Help Page


The help page for grep contains a summarized list of all available functions. It helps to overcome many issues directly from the terminal.

$ grep --help

This command will invoke the help page for grep.

50. Consult Documentation


The grep documentation is extremely detailed and provides a thorough introduction to available features and the usage of regular expressions. You can consult the manual page for the grep using the command below.

$ man grep

Ending Thoughts


Since you can create any combination of commands using grep’s robust CLI options, it’s hard to encapsulate everything about the grep command in a single guide.

However, our editors have tried their best to outline almost every practical grep example to help you familiarize yourself with it much better.

We suggest you practice as many of these commands as possible and find ways to incorporate grep into your day-to-day file processing. Although you may face newer obstacles each day, this is the only way to really master the Linux grep command.

2 COMMENTS

  1. The best guide on grep I could ever find. Hats off to the author!

    _____ _ _
    / ____| | | | |
    | (___ __ _ | | _ _ | |_ ___
    \___ \ / _` | | | | | | | | __| / _ \
    ____) | | (_| | | | | |_| | | |_ | __/
    |_____/ \__,_| |_| \__,_| \__| \___|

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Exit mobile version