本文参考了 使用 grep 查找所有包含指定文本的文件,用于学习理解grep 的用法!
非递归搜索包含特定字符串的文件
grep
命令
grep -s stretch /etc/*
*****************************************************************************
# /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
# /etc/os-release:VERSION="9 (stretch)"
*****************************************************************************
注意:grep 的 -s
选项会在发现不存在或者不能读取的文件时隐藏报错信息。结果显示除了文件名之外,还有包含请求字符串的行也被一起输出了。
递归搜索包含特定字符串的文件
上述案例会忽略所有的子目录,递归需要加参数 -R
, 所谓递归搜索就是同时搜索所有的子目录。
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/dictionaries-common/words:homestretches
# /etc/dictionaries-common/words:outstretched
...
*****************************************************************************
搜索所有包含特定单词的文件
上面grep命名的案例中列出的是所有包含字符串stretch 的文件,也就是说,homestretches,outstretched 等内容的行也会被显示。使用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/dictionaries-common/words:stretch
# /etc/dictionaries-common/words:stretch's
...
*****************************************************************************
显示包含特定文本的文件名
上面的命令都会产生多余的输出。下一个案例则会递归地搜索 etc 目录中包含 stretch 的文件并只输出文件名:
grep -Rl stretch /etc/*
*****************************************************************************
# /etc/apt/sources.list
# /etc/dictionaries-common/words
# /etc/grub.d/00_header
# /etc/os-release
...
*****************************************************************************
大小写不敏感的搜索
通过使用 grep 的 -i 选项,grep 命令还会列出所有包含 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)中搜索指定的文本/字符串。 下面这个例子就会在 /etc 目录中搜索带字符串 bash 且所有扩展名为 .conf 的文件:
grep -Ril bash /etc/*.conf 或 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
搜索时排除指定目录
跟文件一样,grep 也能在搜索时排除指定目录。 使用 --exclude-dir
选项就行。
显示包含搜索字符串的行号
-n 选项还会显示指定字符串所在行的行号:
grep -Rni bash /etc/*.conf
*****************************************************************************
# /etc/adduser.conf:6:DSHELL=/bin/bash
*****************************************************************************
寻找不包含指定字符串的文件
例如下面命令会搜索 /etc 目录中不包含 stretch 的所有文件:
grep -Rlv stretch /etc/*
网友评论