使用 grep 尋找所有包含指定文字的文件
#目標:本文提供一些關於如何搜尋指定目錄或整個檔案系統中那些包含指定單字或字串的檔案。
難度:容易
約定:
- ## - 需要使用 root 權限來執行指定指令,可以直接使用 root 使用者來執行也可以使用 sudo 指令
- $ - 可以使用一般使用者來執行指定指令
第一個範例讓我們來搜尋 /etc/ 目錄下所有包含 stretch 字串的文件,但不去搜尋其中的子目錄:
# grep -s stretch /etc/* /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)" /etc/os-release:VERSION="9 (stretch)"
grep 的 -s 選項會在發現不存在或無法讀取的檔案時隱藏報錯資訊。結果顯示除了檔案名稱之外,還有包含請求字串的行也被一起輸出了。
上面案例中忽略了所有的子目錄。所謂遞歸搜尋就是指同時搜尋所有的子目錄。
下面的指令會在 /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 的檔案。也就是說包含 stretches , stretched 等內容的行也會被顯示。使用 grep 的 -w 選項會只顯示包含特定單字的行:
# 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)"
上面的命令都会产生多余的输出。下一个案例则会递归地搜索 etc 目录中包含 stretch 的文件并只输出文件名:
# grep -Rl stretch /etc/* /etc/apt/sources.list /etc/dictionaries-common/words /etc/grub.d/00_header /etc/os-release
默认情况下搜索是大小写敏感的,也就是说当搜索字符串 stretch 时只会包含大小写一致内容的文件。
通过使用 grep 的 -i 选项,grep 命令还会列出所有包含 Stretch , STRETCH , StReTcH 等内容的文件,也就是说进行的是大小写不敏感的搜索。
# 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
grep 命令也可以只在指定文件中进行搜索。比如,我们可以只在配置文件(扩展名为.conf)中搜索指定的文本/字符串。 下面这个例子就会在 /etc 目录中搜索带字符串 bash 且所有扩展名为 .conf 的文件:
# grep -Ril bash /etc/*.conf OR # grep -Ril --include=\*.conf bash /etc/* /etc/adduser.conf
类似的,也可以使用 --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
跟文件一样,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
-n 选项还会显示指定字符串所在行的行号:
# grep -Rni bash /etc/*.conf /etc/adduser.conf:6:DSHELL=/bin/bash
最后这个例子使用 -v 来列出所有不包含指定字符串的文件。
例如下面命令会搜索 /etc 目录中不包含 stretch 的所有文件:
# grep -Rlv stretch /etc/*
以上是使用 grep 尋找所有包含指定文字的文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

要打開 web.xml 文件,可以使用以下方法:使用文本編輯器(如記事本或 TextEdit)使用集成開發環境(如 Eclipse 或 NetBeans)使用命令行編輯命令(Windows:notepad web.xml;Mac/Linux:open -a TextEdit web.xml)

語言多線程可以大大提升程序效率,C 語言中多線程的實現方式主要有四種:創建獨立進程:創建多個獨立運行的進程,每個進程擁有自己的內存空間。偽多線程:在一個進程中創建多個執行流,這些執行流共享同一內存空間,並交替執行。多線程庫:使用pthreads等多線程庫創建和管理線程,提供了豐富的線程操作函數。協程:一種輕量級的多線程實現,將任務劃分成小的子任務,輪流執行。

Linux最適合用作服務器管理、嵌入式系統和桌面環境。 1)在服務器管理中,Linux用於託管網站、數據庫和應用程序,提供穩定性和可靠性。 2)在嵌入式系統中,Linux因其靈活性和穩定性被廣泛應用於智能家居和汽車電子系統。 3)在桌面環境中,Linux提供了豐富的應用和高效的性能。

DebianLinux以其稳定性和安全性著称,广泛应用于服务器、开发和桌面环境。虽然目前缺乏关于Debian与Hadoop直接兼容性的官方说明,但本文将指导您如何在Debian系统上部署Hadoop。Debian系统需求:在开始Hadoop配置前,请确保您的Debian系统满足Hadoop的最低运行要求,这包括安装必要的Java运行时环境(JRE)和Hadoop软件包。Hadoop部署步骤:下载并解压Hadoop:从ApacheHadoop官方网站下载您需要的Hadoop版本,并将其解

使用Go語言連接Oracle數據庫時是否需要安裝Oracle客戶端?在使用Go語言開發時,連接Oracle數據庫是一個常見需求�...

“DebianStrings”並非標準術語,其具體含義尚不明確。本文無法直接評論其瀏覽器兼容性。然而,如果“DebianStrings”指的是在Debian系統上運行的Web應用,則其瀏覽器兼容性取決於應用本身的技術架構。大多數現代Web應用都致力於跨瀏覽器兼容性。這依賴於遵循Web標準,並使用兼容性良好的前端技術(如HTML、CSS、JavaScript)以及後端技術(如PHP、Python、Node.js等)。為了確保應用與多種瀏覽器兼容,開發者通常需要進行跨瀏覽器測試,並使用響應式

我開發了一個名為Lua-Libuv的項目,並樂於分享我的經驗。項目初衷是探索如何利用Libuv(一個用C語言編寫的異步I/O庫)構建簡單的HTTP服務器,而無需深入掌握C語言。借助ChatGPT的輔助,我完成了HTTP.C的基礎代碼。在處理持久連接時,我成功實現了在適當的時機關閉連接並釋放資源。起初,我嘗試創建一個簡單的服務器,通過關閉連接來結束主程序,但遇到了一些問題。我嘗試過使用流式傳輸發送數據塊,雖然有效,但這會阻塞主線程。最終,我決定放棄這個方法,因為我的目標並非深入學習C語言。最終,我

無法以 root 身份登錄 MySQL 的原因主要在於權限問題、配置文件錯誤、密碼不符、socket 文件問題或防火牆攔截。解決方法包括:檢查配置文件中 bind-address 參數是否正確配置。查看 root 用戶權限是否被修改或刪除,並進行重置。驗證密碼是否準確無誤,包括大小寫和特殊字符。檢查 socket 文件權限設置和路徑。檢查防火牆是否阻止了 MySQL 服務器的連接。
