Linux grep exclude dir

How to exclude filtered output from Linux Grep

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 .

grep -wv nologin /etc/passwd 
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.

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 .

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 .

Читайте также:  Linux block device driver

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.

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.

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.

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.

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.

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.

grep -rl --exclude=*.png,jpg> myfreax * 

Источник

Grep: Excluding a specific folder using

I want to recursively search /app/data but I do not want to search /app/secondary/data . From within the app folder, what would my grep command look like?

2 Answers 2

grep -r --exclude-dir='secondary/data' PATTERN data 

Why the accepted answer is wrong

The accepted answer by @seamus is wrong because grep -r —exclude-dir= matches against base names which by definition can’t contain slashes (slashes turn it into a no-op effectively as no base name will match).

—exclude-dir=glob

. When searching recursively, skip any subdirectory whose base name matches glob.

Why the question is probably wrong

I think the OP’s directory structure doesn’t represent the problem well because because if one has.

. ├── app ├── data └── secondary └── data 

. and the current directory is app one can simply do:

It will not search the secondary folder as its not inside data .

What the intended question might be

Given directory structure with nested data folders how to look in the top-level data but not in the nested folders.

. └── data ├── file.txt ├── folder1 │ └── folder2 │ ├── data │ │ └── file.txt │ └── file.txt └── folder3 ├── data │ └── file.txt └── file.txt 

What the answer would be

> grep -r --exclude-dir='data' data/* 

Note the * at the end. If omitted the top-level data folder won’t be searched neither.

Источник

How does grep exclude directory, file, keyword in Linux/unix

In linux/unix, “grep exclude” allows us to find what we need to find more accurately.

The grep command supports multiple exclusion methods, including grep to exclude directory, grep to exclude file, grep to exclude binary files, and grep to exclude keywords.

Today we will focus on the above several “grep exclude” usage examples.

Grep exclude keywords

Grep exclude keywords or word“, this is the function I use most frequently in the linux grep command, it can well block the interference of big data to me.

The grep command excludes keywords. We can use the grep -v option, which can which can invert the match.

grep -v syntax

# grep excludes a single keyword ➜ grep -v "keyword" file # grep excludes multiple keywords ➜ grep -v "keyword1" file | grep -v "keyword2" | . 

grep -v examples

In the following example, we will use grep to search for keywords and exclude specific keyword.

➜ ~ grep -i 'use' test.log | grep -v "option"

grep exclude keyword

In the following example, we will use a pipeline with grep to exclude multiple keywords.

➜ ~ grep -i 'use' test.log | grep -v "option" | grep -v "find"

grep excludes multiple keywords

Grep exclude directory

Grep exclude directories, we can use the grep –exclude-dir option, which needs to be used with the grep -R option.

Grep –exclude-dir excludes directories matching the given file name pattern from the search.

  • –exclude-dir
    If -R is specified, it excludes directories matching the given filename pattern from the search.
  • -R, -r, –recursive
    Recursively search subdirectories listed.

grep –exclude-dir syntax

# grep exclude a directory ➜ grep --exclude-dir "directory" -R "keyword" . # grep exclude multiple directories ➜ grep --exclude-dir "directory1" --exclude-dir "directory2" -R "keyword" .

grep –exclude-dir examples

In the following example, we use grep –exclude-dir to exclude one or more directories.

➜ grep --exclude-dir "test" -R "grep" . . ➜ grep --exclude-dir "test" --exclude-dir "backup" -R "grep" . . 

grep exclude directory

Grep exclude file

Grep exclude file, as with excluding directories, we can use the grep –exclude option, which will exclude files matching the given file name pattern from the search.

grep — exclude syntax

# grep exclude a file ➜ grep --exclude "file" "keyword" files # grep exclude multiple files ➜ grep --exclude "file1" --exclude "file1" "keyword" files

grep — exclude examples

In the following example, we will use the grep –exclude option to exclude one or more files during pattern search.

➜ grep --exclude "test.log" "grep" *.log . ➜ grep --exclude "test.log" --exclude "m.log" "grep" *.log . 

grep exclude file

Grep exclude binary files

When we use the grep command pattern search, we often find some binary files, these are not what we need, so we need to exclude these binary files.

Grep exclude binary files, we can use grep -I option, it can ignore binary files when searching.

grep -I syntax

grep -I examples

In the following example, we will use the grep -I option to ignore the binary file search content.

Источник

Оцените статью
Adblock
detector