##文件名+内容
grep -r "查询内容" 文件目录
-r, --recursive
Read all files under each directory, recursively, following
symbolic links only if they are on the command line. This is
equivalent to the -d recurse option.
##只显示包含内容的文件名
grep -r -l "查询内容" 文件目录
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)
##文件名+内容
find 文件目录 -type f |xargs grep "查询内容";
eg:
grep -r "192.168.100" /etc
/etc/sysconfig/network-scripts/ifcfg-ens192:IPADDR="192.168.100.178"
/etc/sysconfig/network-scripts/ifcfg-ens192:GATEWAY="192.168.100.254"
grep -r -l "192.168.100" /etc
/etc/sysconfig/network-scripts/ifcfg-ens192
find /etc/ -type f |xargs grep "192.168.100"
/etc/sysconfig/network-scripts/ifcfg-ens192:IPADDR="192.168.100.178"
/etc/sysconfig/network-scripts/ifcfg-ens192:GATEWAY="192.168.100.254
"
本文介绍了grep命令的-r选项用于递归搜索目录及其链接,-l选项仅显示匹配文件名,以及find配合xargs进行深度查找。实例演示了在/etc目录下查找包含'192.168.100'的文件内容和文件名。这些技巧在IT管理中常用于快速定位关键配置。
5353

被折叠的 条评论
为什么被折叠?



