如何使用Linux grep命令尋找具有特定文本的所有檔案
目的
本文提供了關於Linux檔案系統中如何尋找特定目錄或包含特定單詞或字串的所有檔案的有用技巧。
約定
- # - 要求直接以root使用者身份或使用sudo命令以root許可權執行給定的命令
- $ - 給定的命令作為一個普通的非特權使用者執行
樣本以非遞迴方式尋找具有特定字串的所有檔案
第一個命令樣本將在/etc/目錄下的所有檔案中搜尋字串artful,同時排除任何子目錄:
bkjia@bkjia:~$ grep -s artful /etc/*
/etc/lsb-release:DISTRIB_CODENAME=artful
/etc/os-release:VERSION_CODENAME=artful
/etc/os-release:Ubuntu_CODENAME=artful
grep -s 選項將禁止關於不存在或不可讀檔案的錯誤訊息。 輸出顯示檔案名稱以及列印包含請求字串的實際行。
如:
遞迴地尋找具有特定字串的所有檔案
以上命令省略了所有的子目錄。 遞迴搜尋意味著遍曆所有的子目錄。 以下命令將在/etc/目錄內的所有檔案(包括所有子目錄)中搜尋字串artful:
bkjia@bkjia:~$ sudo grep -R artful /etc/*
[sudo] bkjia 的密碼:
/etc/apt/sources.list.save:# deb cdrom:[Ubuntu 17.10 _Artful Aardvark_ - Release amd64 (20171018)]/ artful main restricted
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/etc/apt/sources.list.save:# deb-src http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/etc/apt/sources.list.save:# deb-src http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful universe
/etc/apt/sources.list.save:# deb-src http://cn.archive.ubuntu.com/ubuntu/ artful universe
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful-updates universe
......
/etc/dictionaries-common/words:artful
/etc/dictionaries-common/words:artfully
/etc/dictionaries-common/words:artfulness
/etc/dictionaries-common/words:artfulness's
/etc/lsb-release:DISTRIB_CODENAME=artful
/etc/os-release:VERSION_CODENAME=artful
/etc/os-release:UBUNTU_CODENAME=artful
如
搜尋包含特定單詞的所有檔案
上面的grep命令樣本列出了包含字串artful的所有檔案。 也顯示了artfules,artfuled等。 使用grep -w選項只顯示一個特定的單詞:
bkjia@bkjia:~$ sudo grep -Rw artful /etc/*
[sudo] bkjia 的密碼:
/etc/apt/sources.list.save:# deb cdrom:[Ubuntu 17.10 _Artful Aardvark_ - Release amd64 (20171018)]/ artful main restricted
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/etc/apt/sources.list.save:# deb-src http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/etc/apt/sources.list.save:# deb-src http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/etc/apt/sources.list.save:deb http://cn.archive.ubuntu.com/ubuntu/ artful universe
......
/etc/dictionaries-common/words:artful
/etc/lsb-release:DISTRIB_CODENAME=artful
/etc/os-release:VERSION_CODENAME=artful
/etc/os-release:UBUNTU_CODENAME=artful
如:
僅列出包含特定文字的檔案名稱
上述命令可能會產生不必要的輸出。 下一個樣本將只遞迴地顯示包含/etc/目錄下的字串artful的所有檔案名稱:
bkjia@bkjia:~$ sudo grep -Rl artful /etc/*
/etc/apt/sources.list.save
/etc/apt/sources.list
/etc/apt/sources.list.d/noobslab-ubuntu-deepin-sc-artful.list
/etc/apt/sources.list.d/openshot_developers-ubuntu-ppa-artful.list
/etc/apt/sources.list.d/forkotov02-ubuntu-ppa-artful.list
/etc/apt/sources.list.d/forkotov02-ubuntu-ppa-artful.list.save
/etc/apt/sources.list.d/openshot_developers-ubuntu-ppa-artful.list.save
/etc/dictionaries-common/words
/etc/lsb-release
/etc/os-release
如:
執行不區分大小寫搜尋
所有搜尋預設情況下都區分大小寫,這意味著任何搜尋字串的搜尋都將只顯示包含確切的大寫和小寫匹配的檔案。 通過使用grep的-i選項,該命令還將列出包含Artful,STRETCH,ArtFul等的任何行,從而執行不區分大小寫搜尋。
bkjia@bkjia:~$ sudo grep -Ril artful /etc/*
/etc/apt/sources.list.save
/etc/apt/sources.list
/etc/apt/sources.list.d/noobslab-ubuntu-deepin-sc-artful.list
/etc/apt/sources.list.d/openshot_developers-ubuntu-ppa-artful.list
/etc/apt/sources.list.d/forkotov02-ubuntu-ppa-artful.list
/etc/apt/sources.list.d/forkotov02-ubuntu-ppa-artful.list.save
/etc/apt/sources.list.d/openshot_developers-ubuntu-ppa-artful.list.save
/etc/dictionaries-common/words
/etc/lsb-release
/etc/os-release
如:
包含或排除搜尋中的特定檔案名稱
使用grep命令也可以只包含特定的檔案作為搜尋的一部分。 例如,我們只想在副檔名為.conf的設定檔中搜尋特定的文本/字串。 下一個例子將在/ etc目錄下找到包含字串bash的副檔名為.conf的所有檔案:
bkjia@bkjia:~$ sudo grep -Ril bash /etc/*.conf
/etc/adduser.conf
或者
bkjia@bkjia:~$ sudo grep -Ril --include=\*.conf bash /etc/*
/etc/adduser.conf
同樣,使用--exclude選項,我們可以排除任何特定的檔案名稱:
bkjia@bkjia:~$ sudo grep -Ril --exclude=\*.conf bash /etc/*
/etc/alternatives/lzcmp
/etc/alternatives/vivaldi
/etc/alternatives/vi
/etc/alternatives/view
/etc/alternatives/gnome-www-browser
/etc/alternatives/ex
/etc/alternatives/x-www-browser
/etc/alternatives/lzdiff
/etc/alternatives/rview
/etc/alternatives/x-session-manager
/etc/apparmor/init/network-interface-security/sbin.dhclient
/etc/apparmor.d/sbin.dhclient
/etc/apparmor.d/usr.sbin.cupsd
/etc/apparmor.d/usr.lib.libreofficeprogram.senddoc
/etc/apparmor.d/usr.bin.evince
/etc/apparmor.d/abstractions/ubuntu-browsers.d/plugins-common
/etc/apparmor.d/abstractions/bash
/etc/apparmor.d/abstractions/private-files
/etc/apparmor.d/usr.lib.libreofficeprogram.soffice.bin
/etc/bash.bashrc
/etc/bash_completion
/etc/bash_completion.d/grub
/etc/bash_completion.d/git-prompt
/etc/bash_completion.d/apport_completion
/etc/cron.daily/mlocate
/etc/cron.daily/apt-compat
/etc/dhcp/dhclient-enter-hooks.d/resolved
/etc/dictionaries-common/words
/etc/gdm3/Xsession
/etc/group
/etc/group-
/etc/gshadow
/etc/gshadow-
/etc/ImageMagick-6/mime.xml
/etc/inputrc
/etc/passwd
/etc/passwd-
/etc/profile
/etc/profile.d/bash_completion.sh
/etc/profile.d/vte-2.91.sh
/etc/shells
/etc/skel/.bashrc
/etc/skel/.bash_logout
/etc/skel/.profile
如:
從搜尋中排除特定的目錄
與檔案一樣,grep也可以從搜尋中排除特定的目錄。 使用--exclude-dir選項從搜尋中排除目錄。 以下搜尋樣本將在/etc目錄中尋找包含字串artful的所有檔案,並從搜尋中排除/etc/grub.d:
bkjia@bkjia:~$ sudo grep --exclude-dir=/etc/grub.d -Rwl artful /etc/*
/etc/apt/sources.list.save
/etc/apt/sources.list
/etc/apt/sources.list.d/noobslab-ubuntu-deepin-sc-artful.list
/etc/apt/sources.list.d/openshot_developers-ubuntu-ppa-artful.list
/etc/apt/sources.list.d/forkotov02-ubuntu-ppa-artful.list
/etc/apt/sources.list.d/forkotov02-ubuntu-ppa-artful.list.save
/etc/apt/sources.list.d/openshot_developers-ubuntu-ppa-artful.list.save
/etc/dictionaries-common/words
/etc/lsb-release
/etc/os-release
顯示包含搜尋字串的行號
通過使用-n選項,grep還將提供有關特定字串的行號的資訊:
bkjia@bkjia:~$ sudo grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash
找到所有不包含特定字串的檔案
最後一個例子將使用-v選項來列出所有不包含特定關鍵字的檔案。 例如,以下搜尋將列出不包含字串artful的/etc/目錄中的所有檔案:
bkjia@bkjia:~$ sudo grep -Rlv artful /etc/*
/etc/acpi/events/asus-wireless-on
/etc/acpi/events/lenovo-undock
/etc/acpi/events/ibm-wireless
/etc/acpi/events/thinkpad-cmos
/etc/acpi/events/asus-wireless-off
/etc/acpi/events/asus-keyboard-backlight-up
/etc/acpi/events/asus-keyboard-backlight-down
......
這篇文章對您有用嗎?歡迎在幫客之家(bkjia.com)下面的評論處留言,讓我們知道您的想法。
本文永久更新連結地址: