Grep is a Linux command-line utility for searching files for specific patterns that match regular expressions. The name “grep” is derived from the ed(editor) command g/re/p, which means searching globally for regular expressions and printing those matching lines. In other words, grep is global regular expressions print. Grep was originally developed for the Unix operating system but later became available for Unix-like and OS-9 systems such as ARM/XScale, PowerPC, Intel x86 architecture, etc. As a command, it is used daily by system administrators, developers, and regular users familiar with Linux.
In the next few paragraphs of the blog post, we will explain grep with real examples. Let’s get started!
Prerequisites
- A server with Ubuntu 24.04 or any Linux OS
- User privileges: root or non-root user with sudo privileges
Update the System
Updating the system packages to their latest versions before executing any commands on the command line is recommended. To do that, execute the following command:
sudo apt update -y && sudo apt upgrade -y
Syntax of grep Command
The syntax of the grep command is the following:
grep [options] pattern [files]
The options are arguments for modifying the grep commands’ behavior. The pattern is the keyword you want to search for, and the file is the content of the item you want to search for.
Available Grep Commands
This is the list of some available grep commands explained in detail:
-r: recursive search -c: print the lines with matching pattern -i: ignores the case-sensitive pattern -n: print the lines with matching patterns along with the line number -l: print only the name of the files with matched pattern -h: print the matched lines, but not the file names (opposite of -l) -w: print the whole matched word
In the next paragraphs, we will start with some basic grep commands and end with complex ones. We created a file with some essays about the Linux OS and will use that for our examples. You can create your file with some text to test the grep command. Let’s get things done!
1. Basic Search
The basic search command is used when we want to find a particular word. For example, let’s find the name Linus (the founder of the Linux OS) in our file:
grep Linus test.txt
The lines that contain the Linus word will be printed:
Linux is a trademark owned by Linus Torvalds. In 1996, Linus announced that the mascot for Linux wound be a penguin. This is because Linus was bitten by a penguin.
2. Count Matches
Find and count the lines matching the given word, Linus. To do that, execute the following command:
grep -c "Linus" test.txt
Instead of the printed lines, you should see only a number:
root@host:~# grep -c "Linus" test.txt 3
3. Display the files that match the pattern
To list the files, that matches the pattern Linus you need to execute the following command:
grep -l "Linus" *
The output is the following:
root@host:~# grep -l "Linus" * test.txt
Let’s copy the test.txt file multiple times and execute the same command:
cp test.txt test2.txt; cp test.txt test3.txt
Once they are created, execute the command grep -l “Linus” * again.
root@host:~# grep -l "Linus" * test2.txt test3.txt test.txt
4. Recursive search
The next command is a recursive search of all the files and subdirectories within a folder. We will need a directory full of files and subdirectories for this command. That is why we downloaded the WordPress and extracted it into the root directory on our server.
Let’s find all files that contain the famous WordPress phrase “Silence is Golden.” To do that, we need to find them recursively with the command below:
grep -r "Silence is golden" wordpress/
You will get a list of the files that contain this pattern.
root@host:~# grep -r "Silence is golden" wordpress/ wordpress/wp-admin/includes/plugin.php wordpress/wp-admin/includes/privacy-tools.php wordpress/wp-content/themes/index.php: wordpress/wp-content/plugins/index.php: wordpress/wp-content/plugins/akismet/index.php: wordpress/wp-content/index.php
5. Case-insensitive search
The case-insensitive search will give us the pattern’s name, regardless of whether it is in upper or lower case. For example, if we add i in the grep command for recursive search:
grep -ril "silence is golden" wordpress/
It will give us the files as the previous command:
root@host:~# grep -ril "silence is golden" wordpress/ wordpress/wp-admin/includes/plugin.php wordpress/wp-admin/includes/privacy-tools.php wordpress/wp-content/themes/index.php wordpress/wp-content/plugins/index.php wordpress/wp-content/plugins/akismet/index.php wordpress/wp-content/index.php
6. Combination with other commands
We can combine grep with other commands as well. For example, if there are too many running services on our server and we want to know the process ID of the Apache web server, we can execute the following command:
netstat -tunlp | grep 80
Once executed, you should get output similar to this:
root@host:~# netstat -tunlp | grep 80 tcp6 0 0 :::80 :::* LISTEN 31607/apache2
The netsat command without grep will return a huge output.
More About Grep
If you want to know more about the grep command, you can execute the following:
man grep
The output will be with a full description with all possible options:
GREP(1) User Commands GREP(1) NAME grep, egrep, fgrep, rgrep - print lines that match patterns SYNOPSIS grep [OPTION...] PATTERNS [FILE...] grep [OPTION...] -e PATTERNS ... [FILE...] grep [OPTION...] -f PATTERN_FILE ... [FILE...] DESCRIPTION grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input. Debian also includes the variant programs egrep, fgrep and rgrep. These programs are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated upstream, but Debian provides for backward compatibility. For portability reasons, it is recommended to avoid the variant programs, and use grep with the related option instead. OPTIONS Generic Program Information --help Output a usage message and exit. -V, --version Output the version number of grep and exit. Pattern Syntax -E, --extended-regexp Interpret PATTERNS as extended regular expressions (EREs, see below). -F, --fixed-strings Interpret PATTERNS as fixed strings, not regular expressions. -G, --basic-regexp Interpret PATTERNS as basic regular expressions (BREs, see below). This is the default.
That’s it! You have learned some basic information about the Grep command in Linux with examples.
If you have difficulties with the grep command, our Linux admins will help you with any aspect. All you have to do is sign up for one of our monthly management or per-incident server support plans. Do not hesitate to contact us anytime. We are available 24/7.
PS. If you liked this post about Grep Command in Linux, please share it with your friends on social networks using the buttons on the left or leave a reply below. Thanks.