首頁 > 軟體

如何使用Linux grep命令查詢具有特定文字的所有檔案

2020-06-16 17:00:09

目的

本文提供了關於Linux檔案系統中如何查詢特定目錄或包含特定單詞或字串的所有檔案的有用技巧。

約定

  • - 要求直接以root使用者身份或使用sudo命令以root許可權執行給定的命令
  • $ - 給定的命令作為一個普通的非特權使用者執行

範例

以非遞迴方式查詢具有特定字串的所有檔案

第一個命令範例將在/etc/目錄下的所有檔案中搜尋字串artful,同時排除任何子目錄:

linuxidc@linuxidc:~$ 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

linuxidc@linuxidc:~$ sudo grep -R artful /etc/*
[sudo] linuxidc 的密碼:
/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的所有檔案。 也顯示了artfulesartfuled等。 使用grep -w選項只顯示一個特定的單詞:

linuxidc@linuxidc:~$ sudo grep -Rw artful /etc/*
[sudo] linuxidc 的密碼:
/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的所有檔名:

linuxidc@linuxidc:~$ 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等的任何行,從而執行不區分大小寫的搜尋。

linuxidc@linuxidc:~$ 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的所有檔案:

linuxidc@linuxidc:~$ sudo grep -Ril bash /etc/*.conf
/etc/adduser.conf

或者

linuxidc@linuxidc:~$ sudo grep -Ril --include=*.conf bash /etc/*
/etc/adduser.conf

同樣,使用--exclude選項,我們可以排除任何特定的檔名:

linuxidc@linuxidc:~$ 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:

linuxidc@linuxidc:~$ 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還將提供有關特定字串的行號的資訊:

linuxidc@linuxidc:~$ sudo grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash

找到所有不包含特定字串的檔案

最後一個例子將使用-v選項來列出所有不包含特定關鍵字的檔案。 例如,以下搜尋將列出不包含字串artful的/etc/目錄中的所有檔案:

linuxidc@linuxidc:~$ 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
......

這篇文章對您有用嗎?歡迎在Linux公社(linuxidc.com)下面的評論處留言,讓我們知道您的想法。

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-12/149058.htm


IT145.com E-mail:sddin#qq.com