如何在 Linux shell 中找出所有包含指定文字的檔案

wh7577發表於2021-09-09

目標:本文提供一些關於如何搜尋出指定目錄或整個檔案系統中那些包含指定單詞或字串的檔案。

難度:容易

約定:

  • # - 需要使用 root 許可權來執行指定命令,可以直接使用 root 使用者來執行也可以使用 sudo 命令

  • $ - 可以使用普通使用者來執行指定命令

第一個例子讓我們來搜尋 /etc/ 目錄下所有包含 stretch 字串的檔案,但不去搜尋其中的子目錄:

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

grep-s 選項會在發現不存在或者不能讀取的檔案時隱藏報錯資訊。結果顯示除了檔名之外,還有包含請求字串的行也被一起輸出了。

上面案例中忽略了所有的子目錄。所謂遞迴搜尋就是指同時搜尋所有的子目錄。

下面的命令會在 /etc/ 及其子目錄中搜尋包含 stretch 字串的檔案:

/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 stretch main /etc/apt/sources.list:deb-src 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 的檔案。也就是說包含 stretchesstretched 等內容的行也會被顯示。 使用 grep-w 選項會只顯示包含特定單詞的行:

/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 stretch main /etc/apt/sources.list:deb-src 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)"

上面的命令都會產生多餘的輸出。下一個案例則會遞迴地搜尋 etc 目錄中包含 stretch 的檔案並只輸出檔名:

/etc/apt/sources.list /etc/dictionaries-common/words /etc/grub.d/00_header /etc/os-release

預設情況下搜尋是大小寫敏感的,也就是說當搜尋字串 stretch 時只會包含大小寫一致內容的檔案。

透過使用 grep-i 選項,grep 命令還會列出所有包含 StretchSTRETCHStReTcH 等內容的檔案,也就是說進行的是大小寫不敏感的搜尋。

/etc/apt/sources.list /etc/dictionaries-common/default.hash /etc/dictionaries-common/words /etc/grub.d/00_header /etc/os-release

grep 命令也可以只在指定檔案中進行搜尋。比如,我們可以只在配置檔案(副檔名為.conf)中搜尋指定的文字/字串。 下面這個例子就會在 /etc 目錄中搜尋帶字串 bash 且所有副檔名為 .conf 的檔案:

OR

/etc/adduser.conf

類似的,也可以使用 --exclude 來排除特定的檔案:

/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 目錄下的檔案:

/etc/apt/sources.list /etc/dictionaries-common/words /etc/os-release

-n 選項還會顯示指定字串所在行的行號:

/etc/adduser.conf:6:DSHELL=/bin/bash

最後這個例子使用 -v 來列出所有包含指定字串的檔案。

例如下面命令會搜尋 /etc 目錄中不包含 stretch 的所有檔案:


via:

作者: 譯者: 校對:


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2480/viewspace-2801874/,如需轉載,請註明出處,否則將追究法律責任。

相關文章