如何使用 Linux shell 查找具有特定文本的所有文件
客观的
以下文章提供了一些有用的提示,介绍如何查找包含任何特定单词或字符串的任何特定目录或整个文件系统中的所有文件。
困难
简单的
惯例
# – 要求以root权限直接以root用户身份或使用
sudo命令执行给定的linux命令$ – 要求以常规非特权用户身份执行给定的 Linux 命令
例子
非递归查找具有特定字符串的所有文件
第一个命令示例将在 /etc/ 目录中的所有文件中搜索字符串 stretch,同时排除任何子目录:
# grep -s stretch /etc/*
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"
-s grep 选项将抑制有关不存在或不可读文件的错误消息。输出显示文件名并打印包含请求字符串的实际行。
递归查找具有特定字符串的所有文件
上面的命令省略了所有子目录。递归搜索意味着也遍历所有子目录。以下 Linux 命令将在 /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的所有文件。这意味着还显示带有 stretches、stretched 等的线条。使用 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 选项,该命令还将列出包含 Stretch、STRETCH、StReTcH 等的所有行,因此,执行不区分大小写的搜索。
# 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 的配置文件中搜索特定文本/字符串。下一个示例将在包含字符串 bash 的 /etc 目录中查找扩展名为 .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 还将提供有关找到特定字符串的行号的信息:
# grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash
查找所有不包含特定字符串的文件
最后一个示例将使用 -v 选项列出所有不包含特定关键字的文件。例如,以下搜索将列出 /etc/ 目录中不包含字符串 stretch 的所有文件:
# grep -Rlv stretch /etc/*
