grep is a powerful command line tool for searching one or more input files for lines that match a regular expression and writing each matching line to standard output.
In this tutorial, we will show you how to use
grep to filter the output and exclude . This includes excluding lines that don’t match, excluding directories and files, etc.
Exclude lines that do not match
To print only the lines that do not match the search pattern, use the
-invert-match) option. For example, to print lines that do not contain
-w option tells
grep to return only those lines where the specified string is the entire word. By default,
grep is case-sensitive. This means that uppercase and lowercase are treated as different characters. To ignore case when searching, call
grep with the
If the search string contains spaces, they need to be enclosed in single or double quotes. To specify two or more search patterns, use the following
You can use the
-e option as many times as needed. Another option to exclude multiple search patterns is to use the OR operator
| to join patterns. The following example prints lines that do not contain the strings
grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. by default,
grep interprets patterns as Basic regular expressions, where the metacharacters (e.g.,
|) lose their special meaning (in Linux
| is a pipe), and you must use them be escaped with a backslash .
If you want to use the extended regular expression
-E option, then
| should not be escaped, i.e., you do not need the backslash
\, as shown below.
You can specify different possible matches, which can be literal strings or sets of regular expressions. In the following example, lines where the string
games appears at the beginning of the line will be excluded.
The output of the
grep command can be filtered through a pipe so that only lines matching the specified pattern are printed on the terminal.
For example, to print out all running processes on the system, except those running as user “root”, you can filter the output of the
Exclude directories and files
Sometimes, when performing a recursive search with the
-R options, you may want to exclude specific directories from the search results. The main difference between the
-R options is that when grep is called with an uppercase
R, it will follow all symbolic links.
To exclude directories from the search, use the
--exclude-dir option. The path to the excluded directory is relative to the searched directory. This is an example showing how to search for files containing the string
myfreax in the
/etc directory, excluding the
To exclude multiple directories, enclose the excluded directories in curly brackets and separate them with commas and no spaces. For example, to find files on your Linux system that contain the string “GNU” and exclude the
When using wildcard matching, you can use the
--exclude option with the specified GLOB to exclude unwanted results. In the following example, we search for files in the current working directory that contain the string
myfreax, excluding files ending in the