Linux下查找包含指定文字列

本文介绍了grep命令的-r选项用于递归搜索目录及其链接,-l选项仅显示匹配文件名,以及find配合xargs进行深度查找。实例演示了在/etc目录下查找包含'192.168.100'的文件内容和文件名。这些技巧在IT管理中常用于快速定位关键配置。

##文件名+内容
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

"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值