如何在 Linux shell 中找出所有包含指定文字的檔案
目標:本文提供一些關於如何搜尋出指定目錄或整個檔案系統中那些包含指定單詞或字串的檔案。
難度:容易
約定:
#
- 需要使用 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
的檔案。也就是說包含 stretches
, stretched
等內容的行也會被顯示。 使用 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
命令還會列出所有包含 Stretch
, STRETCH
, StReTcH
等內容的檔案,也就是說進行的是大小寫不敏感的搜尋。
/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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python查詢包含指定字串的所有檔案Python字串
- 如何在 Linux 中找出最近或今天被修改的檔案Linux
- Shell 檔案包含
- 刪除檔案中包含指定字串的行字串
- Linux Shell檔案之間的包含關係Linux
- linux命令從log檔案中找出404 或者500的所有報錯資訊?Linux
- 如何在“我的所有檔案”中隱藏某些檔案?
- 遠端檔案包含shell
- Linux Shell 下載網站指定檔案Linux網站
- LINUX 下查詢指定時間的所有檔案Linux
- 遍歷檔案Java中遍歷出指定目錄中的所有檔案Java
- 指定目錄下的所有檔案中的字串替換字串
- linux批量替換指定資料夾中所有檔案的指定內容Linux
- 要找出某個檔案中所有包含數字,且數字大於1000的行
- 如何在Linux上找出並刪除重複的檔案:FSlintLinux
- Linux迴圈遍歷所有檔案,刪除指定字尾名檔案Linux
- 如何在Shell指令碼中逐行讀取檔案指令碼
- 如何在Linux上利用FSlint找出並刪除重複的檔案Linux
- 檔案包含之包含了Linux檔案描述符Linux
- 使用正規表示式找出所有不包含某字串的行字串
- Linux下使用shell讀取文字檔案中的命令並逐條執行Linux
- 用linux shell逐行讀取文字檔案內容Linux
- 如何在 Linux 中找出 CPU 佔用高的程式Linux
- Python查詢包含指定字串的所有Office文件Python字串
- shell基礎教程二十四: shell基礎教程: Shell檔案包含
- 如何在一個.ear檔案中同時包含兩個.war?
- 替換文字檔案中指定的內容
- 如何找出兩個文字檔案中有相同欄位的行
- linux批量刪除資料夾(包含子目錄)下面的指定檔案Linux
- 如何在 Linux 中找出記憶體消耗最大的程式Linux記憶體
- 去掉linux文字檔案中的 ^M 符號Linux符號
- 找出一堆整數中兩個元素和為指定值的所有組合
- [Linux shell]查詢某目錄下檔案是否包含某個字串Linux字串
- linux下rm命令刪除檔名中包含特殊字元的檔案Linux字元
- html檔案中包含其他檔案的方法大全HTML
- linux_shell_awk 處理 文字檔案 並匯入excelLinuxExcel
- Java 如何找出兩個文字檔案中有相同欄位的行Java
- 第13周-專案1-小玩檔案-將文字檔案中的所有行加上行號後寫到新檔案中