TypechoJoeTheme

Dcr163的博客

统计

如何使用Linux grep命令查找具有特定文本的所有文件

2019-08-19
/
0 评论
/
1,015 阅读
/
正在检测是否收录...
08/19

目的

本文提供了关于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公社

朗读
赞(0)
版权属于:

Dcr163的博客

本文链接:

https://dcr163.cn/335.html(转载时请注明本文出处及文章链接)

评论 (0)