读linux shell脚本攻略有感

本文介绍如何使用find和grep组合命令来查找包含特定字符串的文件,并对比不同命令组合的效果,帮助理解find和grep的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前用的查找文件是否存在某个字符,并把文件名列出来:

find . –type f|xargs grep 204

结果:

./it.conf:<VirtualHost 192.168.105.204>
./sohudl.conf:<VirtualHost 192.168.105.204>
./sohunb.conf:<VirtualHost 192.168.105.204>
./travel.conf:<VirtualHost 192.168.105.204>

这个命令的分解是:

find查找出 ./it.conf ./sohudl.conf ./sohunb.conf ./travel.conf文件

然后通过xargs 把这个./it.conf ./sohudl.conf ./sohunb.conf ./travel.conf 当参数传给grep

相当于grep 204 ./it.conf ./sohudl.conf ./sohunb.conf ./travel.conf

想了想换个方式能不能行:

find . -type f -exec grep 204 {} \;

结果:

<VirtualHost 192.168.105.204>
<VirtualHost 192.168.105.204>
<VirtualHost 192.168.105.204>
<VirtualHost 192.168.105.204>

并没有想上个那样显示文件名,这个和grep有关的

命令分解:

find每次找出./it.conf 然后执行-exec把找出的文件替换掉{}结果是grep 204 ./it.conf,

其他的也是这样,这样的话grep只是分别执行了:

grep 204 ./it.conf

grep 204 ./sohudl.conf

grep 204 ./sohunb.conf

grep 204 ./travel.conf

所以grep 单个文件的时候,不会给出 文件名的,只有多个 才给出 如:

grep 204 ./it.conf ./sohudl.conf ./sohunb.conf ./travel.conf

或者grep 204 *

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值