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

时间:2016-05-27 09:51:30

标签: linux terminal find

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

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

给定片段计数包含" main"在名字中。如何更改它以检查它是否包含" main"在其内容中也是如此?

2 个答案:

答案 0 :(得分:1)

遍历您的文件并使用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

答案 1 :(得分:0)

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

-exec printf '\n' \;代替-print使用换行符保护文件名。