[Shell] Use grep to find all files containing the specified text

grep matches multiple keywords

1. grep'string'

cat manpath.config | grep 'MANPATH'

2. Reverse matching

cat manpath.config| grep -v 'MANPATH'

3. Match multiple keywords at the same time

grep -E "word1|word2|word3" file.txt

4. Simultaneous reverse matching of multiple keywords

grep -vE "word1|word2|word3" file.txt

 

 

Detailed explanation of grep retrieval file content

https://www.jb51.net/article/127783.htm

(1) Specify the file type :
find -type f -name'*.h' | xargs grep "hello"


(2) Exclude java and c file types
grep "hello" -nR --exclude=*.{java,c}

(3) Exclude the lib directory to find the file init.c

find -name lib -prune -o -name init.c

link to the original text: https://blog.csdn.net/wenwang88/article/details/79254337

(4) grep escape character "\" search

https://blog.csdn.net/chengf223/article/details/85229097

(5) Non-recursive search

grep -s "stretch "/etc/*

-s 选项会在发现不存在或者不能读取的文件时隐藏报错信息。

(5) Recursive search

grep -R "stretch "/etc/*

 

https://www.linuxprobe.com/grep-find-file.html

 

grep doesn't work? Filtered or displayed

service --status-all | grep network, even if I piped it grepto only the "network" string, it still lists all services

Solution:

service --status-all |& grep network

https://ubuntuqa.com/article/10818.html

 

 

 

Objective: This article provides some information on how to search for files containing specified words or strings in a specified directory or the entire file system.

Difficulty: easy

Convention:

  • #-You need to use root privileges to execute the specified commands , you can use the root user to execute directly or you can use the sudo  command
  • $-Normal users can be used to execute specified commands

Case study

Non-recursive search for files containing the specified string

In the first example, let us search for all the files containing the string stretch in the /etc/ directory, but not search the subdirectories:

# grep -s stretch /etc/*
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"

The -s option of grep will hide the error message when a file that does not exist or cannot be read is found. The result shows that in addition to the file name, a line containing the requested string is also output.

Recursively search for files containing the specified string

All subdirectories are ignored in the above case. The so-called recursive search refers to searching all subdirectories at the same time.

The following command will search for files containing the string stretch in /etc/ and its subdirectories:

# grep -R stretch /etc/*
/etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main
/etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main
/etc/dictionaries-common/words:backstretch
/etc/dictionaries-common/words:backstretch's
/etc/dictionaries-common/words:backstretches
/etc/dictionaries-common/words:homestretch
/etc/dictionaries-common/words:homestretch's
/etc/dictionaries-common/words:homestretches
/etc/dictionaries-common/words:outstretch
/etc/dictionaries-common/words:outstretched
/etc/dictionaries-common/words:outstretches
/etc/dictionaries-common/words:outstretching
/etc/dictionaries-common/words:stretch
/etc/dictionaries-common/words:stretch's
/etc/dictionaries-common/words:stretched
/etc/dictionaries-common/words:stretcher
/etc/dictionaries-common/words:stretcher's
/etc/dictionaries-common/words:stretchers
/etc/dictionaries-common/words:stretches
/etc/dictionaries-common/words:stretchier
/etc/dictionaries-common/words:stretchiest
/etc/dictionaries-common/words:stretching
/etc/dictionaries-common/words:stretchy
/etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"

Search all files containing a specific word

In the case of the grep command above, all files containing the string stretch are listed. In other words, lines containing stretches, stretched, etc. will also be displayed. Using the -w option of grep will display only the lines that contain specific words:

# grep -Rw stretch /etc/*
/etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main
/etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main
/etc/dictionaries-common/words:stretch
/etc/dictionaries-common/words:stretch's
/etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"

Display file names containing specific text

The above commands will all produce redundant output. The next case will recursively search for files containing stretch in the etc directory and output only the file name:

# grep -Rl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release

Case-insensitive search

By default, the search is case sensitive, which means that when searching for the string stretch, only files with the same case content will be included.

By using the -i option of grep, the grep command will also list all files containing Stretch, STRETCH, StReTcH, etc., which means that the search is case-insensitive.

# grep -Ril stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/default.hash
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release

Include/exclude specified files when searching

The grep command can also search only in specified files. For example, we can only search for the specified text/string in the configuration file (extension .conf). The following example will search for all files with the string bash and the extension .conf in the /etc directory:

# grep -Ril bash /etc/*.conf
OR
# grep -Ril --include=\*.conf bash /etc/*
/etc/adduser.conf

Similarly, you can also use --exclude to exclude specific files:

# grep -Ril --exclude=\*.conf bash /etc/*
/etc/alternatives/view
/etc/alternatives/vim
/etc/alternatives/vi
/etc/alternatives/vimdiff
/etc/alternatives/rvim
/etc/alternatives/ex
/etc/alternatives/rview
/etc/bash.bashrc
/etc/bash_completion.d/grub
/etc/cron.daily/apt-compat
/etc/cron.daily/exim4-base
/etc/dictionaries-common/default.hash
/etc/dictionaries-common/words
/etc/inputrc
/etc/passwd
/etc/passwd-
/etc/profile
/etc/shells
/etc/skel/.profile
/etc/skel/.bashrc
/etc/skel/.bash_logout

Exclude specified directories when searching

Like files, grep can also exclude specified directories when searching. Just use the --exclude-dir option.

The following example will search for files containing the string stretch in the /etc directory, but not files in the /etc/grub.d directory:

# grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/os-release

Display the line number containing the search string

The -n option will also display the line number of the line where the specified string is located:

# grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash

Find files that do not contain the specified string

This last example uses -v to list all files that do not contain the specified string.

For example, the following command will search for all files that do not contain stretch in the /etc directory:

# grep -Rlv stretch /etc/*

via: https://linuxconfig.org/how-to-find-all-files-with-a-specific-text-using-linux-shell

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/114755024