bash如何使用grep命令在给定目录中的所有文件中搜索字符串

标签 bash shell

这个问题在这里已经有了答案:





How to perform grep operation on all files in a directory?

(5 个回答)


去年关闭。




我的代码到现在为止。我是初学者。

count=0
for f in $ls do
count+= grep -c "classes" $f
done
echo $count

最佳答案

只需使用

grep -R <stringToSearch> <dirName>

例如在当前目录和里面的所有文件中搜索“文本”
grep -R "text" .

如果您想获得出现次数,请使用 wc -l作为管道
grep -R "text" . | wc -l

关于bash如何使用grep命令在给定目录中的所有文件中搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45801821/

相关文章:

shell - 为什么 WGET 在成功之前返回 2 条错误消息?

shell - echo smbpasswd by --stdin 不起作用

c - C : chdir() Behaving Odd 中的 Linux Shell

ruby - Ruby 能否访问出现的 shell 命令的输出?

shell - 无法从 gitlab-ci.yml 推送

bash - 清理 shell 运行时环境以运行系统 Perl?

bash - 如何将 Go cmd 应用程序提供为高效应用程序

macos - 使用 osascript 命令在 Bash 中组合变量以形成发送到 AppleScript 的命令

bash -/bin/sh:在Docker容器内找不到my_custom命令

C 使用 Linux shell 函数