我想计算目录中名称和内容包含给定单词的文件数。
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:计算名称和内容中包含单词的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37480304/