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

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

关于linux - Linux:计算名称和内容中包含单词的文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37480304/

10-12 07:20
查看更多