grep
I've mentioned this one already, but in case you missed it, have you ever heard of the grep
command? It's short for global regular expression print, but don't let that long name scare you – it's super handy - I find myself using it all the time.
Basically, grep
is like a searchlight for text. Give it a pattern, and it'll hunt through files to find matching lines and show them to you. And if you don't specify any files? No sweat! It'll just listen to whatever info is coming its way, like from another command's output.
General Syntax
The syntax for the grep
command is as follows:
The items in square brackets are optional.
OPTIONS
- Zero or more options. Grep includes a number of options that control its behaviour.PATTERN
- Search pattern.FILE
- Zero or more input file names.
To be able to search the file, the user running the command must have read access to the file.
Additional Options
Search for a String in Files
The most basic usage of the grep
command is to search for a string (text) in a file. For example, to display all the lines containing the string bash
from the /etc/passwd
file, you would run the following command:
The output should look something like this:
If the string includes spaces, you need to enclose it in single or double quotation marks:
Using Grep to Filter the Output of a Command
A command’s output can be filtered with grep
through piping, and only the lines matching a given pattern will be printed on the terminal.
For example, to find out which processes are running on your system as user www-data
you can use the following ps
command:
You can also chain multiple pipes in on command. As you can see in the output above there is also a line containing the grep
process. If you don’t want that line to be shown pass the output to another grep
instance as shown below.
Invert Match (Exclude)
To display the lines that do not match a pattern, use the -v
( or --invert-match
) option.
For example, to print the lines that do not contain the string nologin
you would use:
Case Insensitive Search
By default, grep
is case sensitive. This means that the uppercase and lowercase characters are treated as distinct.
To ignore case when searching, invoke grep
with the -i
option (or --ignore-case
).
For example, when searching for Zebra
without any option, the following command will not show any output i.e there are matching lines:
But if you perform a case insensitive search using the -i
option, it will match both upper and lower case letters:
Specifying “Zebra” will match “zebra”, “ZEbrA” or any other combination of upper and lower case letters for that string.
Recursive Search
To recursively search for a pattern, invoke grep
with the -r
option (or --recursive
). When this option is used grep
will search through all files in the specified directory, skipping the symlinks that are encountered recursively.
To follow all symbolic links , instead of -r
, use the -R
option (or --dereference-recursive
).
Here is an example showing how to search for the string example.com
in all files inside the /etc
directory:
The output will include matching lines prefixed by the full path to the file:
If you use the -R
option, grep
will follow all symbolic links:
Notice the last line of the output below. That line is not printed when grep
is invoked with -r
because files inside the Nginx’s sites-enabled
directory are symlinks to configuration files inside the sites-available
directory.
Show Only the Filename
To suppress the default grep
output, and print only the names of files containing the matched pattern, use the -l
( or --files-with-matches
) option.
The command below searches through all files ending with .conf
in the current working directory and prints only the names of the files containing the string example.com
:
The output will look something like this:
The -l
option is usually used in combination with the recursive option -R
:
Search for Full Words
When searching for a string, grep
will display all lines where the string is embedded in larger strings.
For example, if you search for “gnu”, all lines where “gnu” is embedded in larger words, such as “cygnus” or “magnum” will be matched:
To return only those lines where the specified string is a whole word (enclosed by non-word characters), use the -w
( or --word-regexp
) option.Word characters include alphanumeric characters (a-z
, A-Z
, and 0-9
) and underscores (_
). All other characters are considered as non-word characters.
If you run the same command as above, including the -w
option, the grep
command will return only those lines where gnu
is included as a separate word.
Count Matches
To print a count of matching lines to standard output, use the -c
( or --count
) option.
In the example below, we are counting the number of accounts that have /usr/bin/zsh
as a shell.
Search for Multiple Strings (Patterns)
Two or more search patterns can be joined using the OR operator |
.
By default, grep
interprets the pattern as a basic regular expression where the meta-characters such as |
lose their special meaning, and their backslashed versions must be used.
In the example below we are searching all occurrences of the words fatal
, error
, and critical
in the Nginx log error file:
If you use the extended regular expression option -E
, then the operator |
should not be escaped, as shown below:
Last updated