The grep (Global Regular Expressions Print) command is a powerful tool for searching and filtering text in Ubuntu or Linux Debian. Whether you are a developer, a system administrator, or just a regular user, knowing how to use the grep command in Linux can help you quickly find the information you need in your log files, configuration files, and other text-based files such as HTML code, PHP, and more.
Grep for Linux/Ubuntu: Practical Examples Included
grep -r ;
The first and most basic way to use the grep command in Linux is by using grep -r
. For example, if you want to search for a specific word, character, or phrase in a directory, you can use the grep command with the option -r
. Let’s say you want to find and display the value “memory_limit” in the /etc/php directory on Linux. You can do this with the following command:
grep -r memory_limit /etc/php/
As you can see in the screenshot, after using grep -r to find the value of “memory_limit” it showed all the configuration results in the /etc/php.
grep -c ;
The grep -c
command is used to count the number of occurrences of a pattern in a file or multiple files. The displayed result of this command is the number of lines in the file(s) that match the specified keyword. The grep command in Linux with the option -c is very handy to return the number of matches instead of displaying lines..
grep -c memory_limit /etc/php/8.0/fpm/php.ini /etc/php/8.0/fpm/php.ini
grep -c "^$" /etc/php/8.0/fpm/php.ini
The above command grep -c
will count the number of blank lines (no characters on the line) in the php.ini
. The result returned is the number of blank lines. Specifically:
grep
: command to search string in file-c
: option to count the number of occurrences of the search string instead of displaying the lines containing the search string"^$"
: the search string is an empty line (start^
and end$
without any characters in between)
So, using the grep command in Ubuntu or Linux with the -c
above option will count the number of blank lines in the file php.ini
and return that number.
grab -n;
The command grep -n in Linux is used to display the line number of the search results. When using this command, grep will display all lines containing the search string, and the sequence number of each line will be given before the content of that line. The example below shows the value of memory_limit located at line 430.
grep -n memory_limit /etc/php/8.0/fpm/php.ini
grep -E ;
The grep (or grep –extended-regexp) command -E
in Linux is used to search for strings with extended regular expressions. It helps you to search more complex strings with more special characters than just using regular grep.
grep -E "(copy)" /etc/php/8.1/fpm/php.ini
Here’s an example of using the grep command to look up IP addresses in the /etc/hosts file on Linux or Ubuntu. By using the -E
option to enable extended regular expressions, and the -o
option to only display the found match, we can use a regular expression to find valid IP addresses in the file. The regular expression searches for numeric strings ranging from 0 to 9, with a length of 1 to 3, combined with the dot symbol to match IP addresses in the /etc/hosts
file.
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" /etc/hosts
Here’s an example of how to use the grep command in Linux with the -E
and -o
options to search for and display email addresses in a file called “filename” located in the directory “/dir/”. The -E option enables extended regular expressions, while the -o
option displays only the matched objects rather than the entire corresponding line.
grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" /dir/filename
The regular expression used in this command looks for the standard email format: “username@domain.com” or “username@domain.co.xx” (where xx is the country domain).
zgrep -i;
grep -i "itsmeit" /var/log/nginx/arriveddev.com.vhost.err;
With an example of how to use the grep command in linux with the option -i
as shown in the illustration, we used grep to search for a string containing “cron” in the log file /var/log/auth.log
on a Linux system. Option -i
used for case-insensitive search. The result will be all lines in the log file containing the string “cron”.
zgrep -i "keyword" test.txt.gz
Command zgrep
used to search for a word or phrase in a gzip archive (.gz extension). Option -i
used for case-insensitive search. In this case, the command will search for the word “keyword” in the test.txt.gz
.
grep -v, -l, -L, -w, -A, -B
Some of the popular options for the grep command in Linux or advanced Ubuntu are below that you can try similar to the examples we provided above:
-v
(or –invert-match): Display lines that do not contain search results.-l
(or –files-with-matches): Display only the name of the file containing the search results, not the content.-L
(or –files-without-match): Displays only the names of files that do not contain search results.-w
(or –word-regexp): Searches only for individual words instead of any combination of characters.-A
(or –after-context): Displays the number of lines after the search result.-B
(or –before-context): Displays the number of lines before the search result.
These options increase the flexibility and power of the grep command in finding information in files and directories. Also when using the grep command in Linux or Ubuntu, one of the other cool uses is to combine grep with the ls, find, dpkg, cat
. You can search for specific files or folders in the system’s directory structure and filter the returned results to make searches faster.
grep combined with ls
To illustrate how to combine the ls command with the grep command, we will try a simple example showing all the subdirectories in its home directory. Since all directories start with the string “drw”, we can use the “ls -l ~” command to list all the directories in this directory, then use the grep command to filter the results for the string “drw”:
ls -l ~ | grep "drw" (hoặc) ls -l /home/arriveddev| grep "drw"
Another example of how to use grep command in linux in combination with ls to find and display all Mp3 files
ls -R /media/arriveddev/Media/Music | grep -E '\.mp3$'
The Linux grep command with the above option -R
will search for all .mp3 audio files in the /Music directory and all its subdirectories. Optionally -R
allow command ls
to search recursively in subdirectories on my Ubuntu machine. The results will be filtered through grep with the option -E
to use regular expressions and show only files ending in .mp3
.
grep with find
find /path/to/directory -type f -name '*.mp3' -print0 | xargs -0 grep -l ''
In there:
/path/to/directory
is the path to the directory containing the .mp3 files.-type f
search only in files (files), not in directories.-name '*.mp3'
Searches only for files with the .mp3 extension.-print0
prints search results separated by null characters instead of spaces, to ensure that filenames with spaces are not mishandled.xargs -0 grep -l ''
reads word search resultsfind
and searches within the files themselves to determine if they contain the search string. The results will print the names of files containing the search string.
Note that when using grep command in linux to search the entire system can take a lot of time and resources such as taking up a lot of RAM, high CPU. You should only search in the necessary directories to minimize search time.
dpkg with grep
The following example uses the grep command in Linux or Ubuntu to list the installed packages on the system and filter out any packages related to “chrome”.
dpkg --list | grep "chrome"
grep in combination with the cat
cat /proc/cpuinfo | grep -i 'model' grep -i "model" /proc/cpuinfo
Both commands are used to display information about the CPU model (model) in the file /proc/cpuinfo. However, the syntax of the two commands is slightly different.
The first command uses the command cat
to display the entire contents of the file /proc/cpuinfo
, then the grep command to search and filter out the lines containing the word “model” (with option -i
to search case insensitive).
The second command uses the grep command directly on the /proc/cpuinfo file to search and filter out lines containing the word “model”.
ps and grep
ps auxww | grep 'chrome'
The command ps
stands for “process status” and it is used to list the processes running on Ubuntu or Linux debian systems. The option auxww
will show all running processes (including those of other users) and their details. The command grep
is used to filter the lines of display output from the ps
.
The command ps
stands for “process status” and is used to list the processes running on Ubuntu or Linux Debian systems. The option auxww
shows all running processes, including those of other users, and their details. By piping the output of “ps” to “grep” and specifying the search term “chrome”, the command ps auxww | grep 'chrome'
filters out all lines containing the word “chrome” from the output. This can help identify any running instances of the Chrome browser on the system.
These are just a few real-life examples of how to use the grep command in Ubuntu or Linux Debian to check logs and handle errors during processes. However, this only scratches the surface of the power of the “grep” command. After reading this article, you will likely find yourself using “grep” more frequently and discovering its amazing capabilities.