如何在Linux shell中找出所有包含指定文本的文件
目標(biāo):本文提供一些關(guān)于如何搜索出指定目錄或整個(gè)文件系統(tǒng)中那些包含指定單詞或字符串的文件。
難度:容易
約定:
#- 需要使用 root 權(quán)限來(lái)執(zhí)行指定命令,可以直接使用 root 用戶(hù)來(lái)執(zhí)行也可以使用sudo命令$- 可以使用普通用戶(hù)來(lái)執(zhí)行指定命令
案例
非遞歸搜索包含指定字符串的文件
***個(gè)例子讓我們來(lái)搜索 /etc/ 目錄下所有包含 stretch 字符串的文件,但不去搜索其中的子目錄:
# grep -s stretch /etc/*/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"/etc/os-release:VERSION="9 (stretch)"
grep 的 -s 選項(xiàng)會(huì)在發(fā)現(xiàn)不存在或者不能讀取的文件時(shí)隱藏報(bào)錯(cuò)信息。結(jié)果顯示除了文件名之外,還有包含請(qǐng)求字符串的行也被一起輸出了。
遞歸地搜索包含指定字符串的文件
上面案例中忽略了所有的子目錄。所謂遞歸搜索就是指同時(shí)搜索所有的子目錄。
下面的命令會(huì)在 /etc/ 及其子目錄中搜索包含 stretch 字符串的文件:
# 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)"
搜索所有包含特定單詞的文件
上面 grep 命令的案例中列出的是所有包含字符串 stretch 的文件。也就是說(shuō)包含 stretches , stretched 等內(nèi)容的行也會(huì)被顯示。 使用 grep 的 -w 選項(xiàng)會(huì)只顯示包含特定單詞的行:
# 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)"
顯示包含特定文本的文件名
上面的命令都會(huì)產(chǎn)生多余的輸出。下一個(gè)案例則會(huì)遞歸地搜索 etc 目錄中包含 stretch 的文件并只輸出文件名:
# grep -Rl stretch /etc/*/etc/apt/sources.list/etc/dictionaries-common/words/etc/grub.d/00_header/etc/os-release
大小寫(xiě)不敏感的搜索
默認(rèn)情況下搜索是大小寫(xiě)敏感的,也就是說(shuō)當(dāng)搜索字符串 stretch 時(shí)只會(huì)包含大小寫(xiě)一致內(nèi)容的文件。
通過(guò)使用 grep 的 -i 選項(xiàng),grep 命令還會(huì)列出所有包含 Stretch , STRETCH , StReTcH 等內(nèi)容的文件,也就是說(shuō)進(jìn)行的是大小寫(xiě)不敏感的搜索。
# 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
搜索時(shí)包含/排除指定文件
grep 命令也可以只在指定文件中進(jìn)行搜索。比如,我們可以只在配置文件(擴(kuò)展名為.conf)中搜索指定的文本/字符串。 下面這個(gè)例子就會(huì)在 /etc 目錄中搜索帶字符串 bash 且所有擴(kuò)展名為 .conf 的文件:
# grep -Ril bash /etc/*.confOR# grep -Ril --include=\*.conf bash /etc/*/etc/adduser.conf
類(lèi)似的,也可以使用 --exclude 來(lái)排除特定的文件:
# 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
搜索時(shí)排除指定目錄
跟文件一樣,grep 也能在搜索時(shí)排除指定目錄。 使用 --exclude-dir 選項(xiàng)就行。
下面這個(gè)例子會(huì)搜索 /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
顯示包含搜索字符串的行號(hào)
-n 選項(xiàng)還會(huì)顯示指定字符串所在行的行號(hào):
# grep -Rni bash /etc/*.conf/etc/adduser.conf:6:DSHELL=/bin/bash
尋找不包含指定字符串的文件
***這個(gè)例子使用 -v 來(lái)列出所有不包含指定字符串的文件。
例如下面命令會(huì)搜索 /etc 目錄中不包含 stretch 的所有文件:
# grep -Rlv stretch /etc/*



























