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

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

执行不区分大小写的搜索

所有搜索默认情况下都区分大小写,这意味着任何搜索字符串的搜索都将只显示包含确切的大写和小写匹配的文件。 通过使用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

如下图:

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

包含或排除搜索中的特定文件名称

使用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

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

同样,使用--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

如下图:

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

从搜索中排除特定的目录

与文件一样,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

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

显示包含搜索字符串的行号

通过使用-n选项,grep还将提供有关特定字符串的行号的信息:

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

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

找到所有不包含特定字符串的文件

最后一个例子将使用-v选项来列出所有不包含特定关键字的文件。 例如,以下搜索将列出不包含字符串artful的/etc/目录中的所有文件:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13195.html