Home > System Tutorial > LINUX > body text

Use grep to find all files containing specified text

PHPz
Release: 2024-03-08 09:31:12
forward
384 people have browsed it

使用 grep 查找所有包含指定文本的文件

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

Difficulty: Easy

Promise:

  • # - You need to use root permissions to execute the specified command. You can directly use the root user to execute it or use the sudo command
  • $ - You can use ordinary users to execute specified commands
Case
Non-recursive search for files containing the specified string

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

# grep -s stretch /etc/*
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"
Copy after login
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, the line containing the request 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 /etc/ and its subdirectories for files containing the stretch string:

# 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)"
Copy after login
Search for all files containing a specific word

In the case of the grep command above, all files containing the string stretch are listed. That is to say, lines containing stretches, stretched, etc. will also be displayed. Using the -w option of grep will display only lines containing 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)"
Copy after login
Display file names containing specific text

上面的命令都会产生多余的输出。下一个案例则会递归地搜索 etc 目录中包含 stretch 的文件并只输出文件名:

# grep -Rl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release
Copy after login
大小写不敏感的搜索

默认情况下搜索是大小写敏感的,也就是说当搜索字符串 stretch 时只会包含大小写一致内容的文件。

通过使用 grep-i 选项,grep 命令还会列出所有包含 StretchSTRETCHStReTcH 等内容的文件,也就是说进行的是大小写不敏感的搜索。

# 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
Copy after login
搜索时包含/排除指定文件

grep 命令也可以只在指定文件中进行搜索。比如,我们可以只在配置文件(扩展名为.conf)中搜索指定的文本/字符串。 下面这个例子就会在 /etc 目录中搜索带字符串 bash 且所有扩展名为 .conf 的文件:

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

类似的,也可以使用 --exclude 来排除特定的文件:

# 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
Copy after login
搜索时排除指定目录

跟文件一样,grep 也能在搜索时排除指定目录。 使用 --exclude-dir 选项就行。

下面这个例子会搜索 /etc 目录中搜有包含字符串 stretch 的文件,但不包括 /etc/grub.d 目录下的文件:

# grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/os-release
Copy after login
显示包含搜索字符串的行号

-n 选项还会显示指定字符串所在行的行号:

# grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash
Copy after login
寻找不包含指定字符串的文件

最后这个例子使用 -v 来列出所有包含指定字符串的文件。

例如下面命令会搜索 /etc 目录中不包含 stretch 的所有文件:

# grep -Rlv stretch /etc/*
Copy after login

The above is the detailed content of Use grep to find all files containing specified text. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!