Use grep to search for all files containing the specified text, grep text
Objectives:This article provides information on how to search for files that contain specified words or strings in a specified directory or the entire file system.
Difficulty:Easy
Conventions:
#-You must use the root permission to execute the specified command. You can directly use the root user to execute the command or use the sudo command $-you can use a common user to execute the specified command.
Case
Non-recursive search for files containing the specified string
In the first example, let's search for all the files containing the stretch string in the/etc/directory, but not 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 hides the error message when it finds a file that does not exist or cannot be read. The results show that in addition to the file name, the rows containing the request string are also output.
Recursively searches for files containing the specified string
All subdirectories are ignored in the above case. Recursive search refers to searching all subdirectories at the same time.
The following command searches for files containing the stretch string 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 for all files containing specific words
In the case of the grep command above, all files containing the string stretch are listed. In other words, rows containing content such as stretches and stretched will also be displayed. With the-w option of grep, only rows containing specific words are displayed:
# 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
All the above commands generate redundant output. In the next case, Recursively search for files containing stretch in the etc directory and output only the file names:
# 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, search is case sensitive. That is to say, when you search for a string stretch, only files with the same case are included.
By using the-I option of grep, The grep command also lists all files containing content such as Stretch, STRETCH, and StReTcH, that is, searches are 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 during search
The grep command can also be searched only in the specified file. For example, you can only search for the specified text/string in the configuration file (with the extension. conf. In the following example, all files with the string bash and the extension. conf will be searched in the/etc directory:
# grep -Ril bash /etc/*.confOR# grep -Ril --include=\*.conf bash /etc/*/etc/adduser.conf
Similarly, you can 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 during search
Like a file, grep can also exclude a specified directory during search. Use the -- exclude-dir option.
The following example searches for files containing the string stretch in the/etc directory, but does not include 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 row number containing the search string
The-n option also displays the row number of the specified string:
# grep -Rni bash /etc/*.conf/etc/adduser.conf:6:DSHELL=/bin/bash
Search for files that do not contain the specified string
The last example uses-v to list allNoA file that contains the specified string.
For example, the following command will search for all files without stretch in the/etc directory:
# grep -Rlv stretch /etc/*