[英]Linux: count files that contains a word in its name and content
I want to count files in directory that in its name and content contains a given word. 我想计算目录中名称和内容包含给定单词的文件。
find ./test -maxdepth 1 -type f -iname "*main*" | wc -l
Given snippet counts files that contains "main" in name. 给定的片段计数名称中包含“ main”的文件。 How to change it to check if it contains "main" in its content as well?
如何更改它以检查其内容中是否还包含“ main”?
Loop over your files and use grep -q
which suppresses grep output: 循环遍历文件,并使用
grep -q
抑制grep输出:
for file in `find ./test -maxdepth 1 -type f -iname "*main*"`; do
if grep -q main $file; then
wc -l $file
fi
done
Output 输出量
5 ./test/foo_main
find ./test -maxdepth 1 -type f -iname "*main*" -exec grep -q main {} \\; -exec printf '\\n' \\; | wc -l
-exec printf '\\n' \\;
instead of -print
protects against filenames with newline characters. 而不是
-print
可以防止使用换行符的文件名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.