linux grep

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 -v (or -invert-match) option. For example, to print lines that do not contain nologin.

1
grep -wv nologin /etc/passwd
1
2
3
root❌0:0:root:/root:/bin/bash
git❌994:994:git daemon user:/:/usr/bin/git-shell
myfreax❌1000:1000:myfreax:/home/myfreax:/bin/bash

The -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 -i option.

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 -e option.

1
grep -wv -e nologin -e bash /etc/passwd

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 nologin or bash.

1
grep -wv 'nologin\|bash' /etc/passwd

GNU 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.

1
grep -Ewv 'nologin|bash' /etc/passwd

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.

1
grep -v "^games" file.txt

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 ps command.

1
ps -ef | grep -wv root

Exclude directories and files

Sometimes, when performing a recursive search with the -r or -R options, you may want to exclude specific directories from the search results. The main difference between the -r or -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 /etc/pki directory.

1
grep -R --exclude-dir=pki myfreax /etc

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 proc, boot and sys directories.

1
grep -r --exclude-dir={proc,boot,sys} gnu /

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 .png and .jpg directories.

1
grep -rl --exclude=*.{png,jpg} myfreax *