Linux中國

如何在 Linux shell 中找出所有包含指定文本的文件

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

難度:容易

約定:

  • # - 需要使用 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 的文件。也就是說包含 stretchesstretched 等內容的行也會被顯示。 使用 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 命令還會列出所有包含 StretchSTRETCHStReTcH 等內容的文件,也就是說進行的是大小寫不敏感的搜索。

# 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/*

via: https://linuxconfig.org/how-to-find-all-files-with-a-specific-text-using-linux-shell

作者:Lubos Rendek 譯者:lujun9972 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出


本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
雨落清風。心向陽

    You may also like

    Leave a reply

    您的郵箱地址不會被公開。 必填項已用 * 標註

    這個站點使用 Akismet 來減少垃圾評論。了解你的評論數據如何被處理

    More in:Linux中國

    Linux中國

    捐贈 Let's Encrypt,共建安全的互聯網

    隨著 Mozilla、蘋果和谷歌對沃通和 StartCom 這兩家 CA 公司處罰落定,很多使用這兩家 CA 所簽發證書的網站紛紛尋求新的證書籤發商。有一個非盈利組織可以為大家提供了免費、可靠和安全的 SSL 證書服務,這就是 Let's Encrypt 項目。現在,它需要您的幫助
    Linux中國

    關於Linux防火牆iptables的面試問答

    Nishita Agarwal是Tecmint的用戶,她將分享關於她剛剛經歷的一家公司(印度的一家私人公司Pune)的面試經驗。在面試中她被問及許多不同的問題,但她是iptables方面的專家,因此她想分享這些關於iptables的問題和相應的答案給那些以後可能會進行相關面試的人。 所有的問題和相應的答案都基於Nishita Agarwal的記憶並經過了重寫。 嗨,朋友!我叫Nishita Agarwal。我已經取得了理學學士學位,我的專業集中在UNIX和它的變種(BSD,Linux)。它們一直深深的吸引著我。我在存儲方面有1年多的經驗。我正在尋求職業上的變化,並將供職於印度的P
    Linux中國

    Lets Encrypt 已被所有主流瀏覽器所信任

    旨在讓每個網站都能使用 HTTPS 加密的非贏利組織 Lets Encrypt 已經得了 IdenTrust的交叉簽名,這意味著其證書現在已經可以被所有主流的瀏覽器所信任。從這個裡程碑事件開始,訪問者訪問使用了Lets Encrypt 證書的網站不再需要特別配置就可以得到 HTTPS 安全保護了。 Lets Encrypt 的兩個中級證書 ...