繁体   English   中英

Linux:计算名称和内容中包含单词的文件

[英]Linux: count files that contains a word in its name and content

我想计算目录中名称和内容包含给定单词的文件。

find ./test -maxdepth 1 -type f -iname "*main*" | wc -l

给定的片段计数名称中包含“ main”的文件。 如何更改它以检查其内容中是否还包含“ main”?

循环遍历文件,并使用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

输出量

5 ./test/foo_main

find ./test -maxdepth 1 -type f -iname "*main*" -exec grep -q main {} \\; -exec printf '\\n' \\; | wc -l

-exec printf '\\n' \\; 而不是-print可以防止使用换行符的文件名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM