grep (global regular expression print) command is one of the most useful and commonly used command in Linux systems. Grep command searches given input from files and will write each matching line to output. If files are not specified then
grep reads from standard input. This guide explains how to use grep command with different options.
Grep Command Syntax
Below is the basic syntax of
grep [OPTIONS] PATTERN [FILE…]
- OPTIONS – Zero or more options. Grep provides a number of options that control its behavior.
- PATTERN – Search pattern.
- FILE – Zero or more input file names.
Ensure that, the user should have read access to the file to search in file.
Use grep Command to Search String in Files
Mostly, grep command is used to search text in files. For an instance, to search username
/etc/passwd file, type:
grep tecnstuff /etc/passwd
It will show output something like this:
If you want to search words with space then you should enclose using
grep "Gnome Display Manager" /etc/passwd
Search string in Command output using Grep Command
If you want to search string from other command’s output and display then you can use pipe the output. In output it will show only matching lines.
For example, you can find the current running processes of your system as
www-data user, run below
ps -ef | grep www-data
www-data 975 973 0 Dec28 ? 00:00:10 nginx: worker process www-data 1006 904 0 Dec28 ? 00:00:20 php-fpm: pool www www-data 1007 904 0 Dec28 ? 00:00:19 php-fpm: pool www tecnstuff 3019 3008 0 04:19 pts/0 00:00:00 grep --color=auto www-data
You can use grep command to search recursively for a pattern. Use
--recursive) option with
grep command. It will search through all files in a specified directory.
For example, search for string
tecnstuff.net in all files inside the
grep -r tecnstuff.net /etc
/etc/hosts:127.0.0.1 tecnstuff.net /etc/nginx/sites-available/tecnstuff.net: server_name tecnstuff.net www.tecnstuff.net;
If you would like to search in symbolic links then you can should use
-R instead of
grep -R tecnstuff.net /etc
It will display the matching lines with path:
/etc/hosts:127.0.0.1 tecnstuff.net /etc/nginx/sites-available/tecnstuff.net: server_name tecnstuff.net www.tecnstuff.net; /etc/nginx/sites-enabled/tecnstuff.net: server_name tecnstuff.net www.tecnstuff.net;
Use grep to search exact words only
When search any word by default grep will return all line wherever keyword is used. For example, if you search for
sweta user then grep will match with
test_sweta and more. You can get exact match using
-w option along with grep command. It will force grep to match only whole word
grep -w "sweta" file
Show Only the Filename
When need to print only file name where search pattern match. Use the
-l ( or
--files-with-matches) option with grep command:
grep -l tecnstuff.net *.conf
Above command will search in all files ending with
.conf in current working directory and will print only the file names which contains the string
Force grep invert match
To print the invert of match you can use
-v option with grep command. It will match only those line which doesn’t contains the given word.
For example print all line that do not contain the word
grep -v gnu /path/to/file
Case Insensitive Search
By default, the
grep command is case sensitive. So the uppercase and lowercase characters are considered as different. If you want to ignore case when searching, use
/home/tecnstuff/test_file file have words nginx, nGinx, NginX and run below command:
grep Unix /home/tecnstuff/test_file
Above command will not match and doesn’t show anything in output.
But if you execute grep command with
-i option then it will match to the both uppercase and lowercase and return output.
grep -i Unix /home/tecnstuff/test_file
It should show output as below:
nginx nGinx NginX
Show Line Numbers
It’s very convenience to check in file by line number. So if you want to print line number along with the output then you can use
grep -n gzip /etc/nginx/nginx.conf
By running above command it will print below output.
48: gzip on; 49: gzip_disable "msie6"; 51: gzip_vary on; 52: gzip_proxied any; 53: gzip_comp_level 6; 54: gzip_buffers 16 8k; 55: gzip_http_version 1.1;
If you required only count of line matches, use
--count) option with grep. It will return number of counts. Below command will return the number of match with
grep -c gzip /etc/nginx/nginx.conf
It will print number 8 as output.
The grep command is regularly used command and you can search for a specific pattern inside of files. You can get more information about Grep at Grep Manual page.
If you have any questions or suggestion, feel free to leave a comment below.