当前位置: 首页 > 科技观察

如何在Linuxshell中查找包含指定文本的所有文件

时间:2023-03-17 14:43:06 科技观察

目的:本文提供了一些有关如何在指定目录或整个文件系统中搜索包含指定单词或字符串的文件的信息。难度:简单协议:#-需要使用root权限执行指定命令,可以使用root用户直接执行也可以使用sudo命令$-可以使用普通用户执行指定命令大小写非-递归搜索包含指定字符串的文件***示例让我们在/etc/目录中搜索所有包含字符串stretch的文件,但不包括子目录:#grep-sstretch/etc/*/etc/os-release:PRETTY_NAME="DebianGNU/Linux9(stretch)"/etc/os-release:VERSION="9(stretch)"grep的-s选项会在发现文件不存在或无法读取时隐藏错误信息.结果显示,除了文件名,包含请求字符串的行也一并输出。递归搜索包含指定字符串的文件在上述情况下将忽略所有子目录。所谓递归搜索,就是同时搜索所有的子目录。下面的命令会在/etc/及其子目录中搜索包包含stretch字符串的文件:#grep-Rstretch/etc/*/etc/apt/sources.list:#debcdrom:[DebianGNU/Linuxtesting_Stretch_-官方快照amd64NETINSTBinary-120170109-05:56]/stretchmain/etc/apt/sources.list:#debcdrom:[DebianGNU/Linuxtesting_Stretch_-官方快照amd64NETINSTBinary-120170109-05:56]/stretchmain/etc/apt/sources.list:debhttp://ftp.au.debian.org/debian/stretchmain/etc/apt/sources.list:deb-srchttp://ftp.au。debian.org/debian/stretchmain/etc/apt/sources.list:debhttp://security.debian.org/debian-securitystretch/updatesmain/etc/apt/sources.list:deb-srchttp://security.debian.org/debian-securitystretch/updatesmain/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/词:outstretch/etc/dictionaries-common/words:outstretched/etc/dictionaries-common/words:outstretches/etc/dictionaries-common/words:outstretching/etc/dictionaries-common/words:stretch/etc/dictionaries-common/单词: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-mstretch`make_system_path_relative_to_its_root"$GRUB_BACKGROUND"`/etc/os-release:PRETTY_NAME="DebianGNU/Linux9(stretch)"/etc/os-release:VERSION="9(stretch)"搜索所有包含具有特定单词的文件在上面的grep命令的情况下,会列出所有包含字符串stretch的文件,也就是说,包含stretches、stretched等的行。也会显示。使用grep的-w选项只会显示包含特定单词的行:#grep-Rwstretch/etc/*/etc/apt/sources.list:#debcdrom:[DebianGNU/Linuxtesting_Stretch_-OfficialSnapshotamd64NETINSTBinary-120170109-05:56]/stretchmain/etc/apt/sources.list:#debcdrom:[DebianGNU/Linuxtesting_Stretch_-官方快照amd64NETINSTBinary-120170109-05:56]/stretchmain/etc/apt/sources.list:debhttp://ftp.au.debian.org/debian/stretchmain/etc/apt/sources.list:deb-srchttp://ftp.au.debian.org/debian/拉伸main/etc/apt/sources.list:debhttp://security.debian.org/debian-security拉伸/更新main/etc/apt/sources.list:deb-srchttp://security.debian.org/debian-securitystretch/updatesmain/etc/dictionaries-common/words:stretch/etc/dictionaries-common/words:stretch's/etc/grub.d/00_header:background_image-mstretch`make_system_path_relative_to_its_root"$GRUB_BACKGROUND"`/etc/os-release:PRETTY_NAME="DebianGNU/Linux9(stretch)"/etc/os-release:VERSION="9(stretch)"显示包含特定文本的文件名以上两个命令都会产生冗余输出下一个示例将递归搜索etc目录以查找包含stretch的文件并仅输出文件名:#grep-Rlstretch/etc/*/etc/apt/sources.list/etc/dictionaries-common/words/etc/grub.d/00_header/etc/os-releasecase-insensitivesearch默认情况下搜索是区分大小写的,也就是说在搜索的时候stringstretchwillonly包含大小写内容一致的文件。通过使用grep的-i选项,grep命令还会列出所有包含Stretch、STRETCH、StReTcH等的文件,也就是说是不区分大小写的搜索。#grep-Rilstretch/etc/*/etc/apt/sources.list/etc/dictionaries-common/default.hash/etc/dictionaries-common/words/etc/grub.d/00_header/etc/os-release包含/排除指定文件搜索时grep命令也可以只在指定的文件中搜索,比如我们只能在conf中搜索指定的文本/字符串管理文件(扩展名.conf)。以下示例将在/etc目录中搜索所有带有字符串bash和扩展名.conf的文件:#grep-Rilbash/etc/*.confOR#grep-Ril--include=\*.confbash/类似于etc/*/etc/adduser.conf,也可以使用--exclude来排除特定文件:#grep-Ril--exclude=\*.confbash/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搜索时排除指定目录和文件一样,grep也可以搜索时排除指定目录。只需使用--exclude-dir选项。以下示例在/etc目录中搜索包含字符串stretch的文件,但排除/etc/grub.d目录中的文件:#grep--exclude-dir=/etc/grub.d-Rwlstretch/etc/*/etc/apt/sources.list/etc/dictionaries-common/words/etc/os-release显示包含搜索字符串的行号-n选项还显示指定字符串所在行的行号:#grep-rnibash/etc/*.conf/etc/adduser.conf:6:DSHELL=/bin/bash查找不包含指定字符串的文件***本例使用-v列出所有不包含的文件指定的字符串。例如,以下命令将搜索/etc目录中不包含stretch的所有文件:#grep-Rlvstretch/etc/*