简体   繁体   English

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

[英]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.

相关问题 使用Linux中特定内容行的名称批量重命名pdf文件 - bulk rename pdf files with name from specific line of its content in linux 如何在Linux中根据名称在具有相应名称的文件夹中移动文件? - How to move files in Linux based on its name in a folder with a corresponding name? 在Linux中搜索目录中的MS word文件以获取特定内容 - Search MS word files in a directory for specific content in Linux 如何计算名称中包含元音的文件数 - How to count the number of files whose name contains a vowel 根据Linux中文件名将文件内容合并为一个文件 - Concatenate files content into one single file based on their name in linux linux搜索文件中的多个单词 - linux search multiple word in a files Linux在所有文件中替换单词 - Linux replace a word in all files 比较Linux中2个文件的不同单词 - Comparing 2 files in linux for different word 如何在名称中包含另一个字符串并在linux中最近7天创建的文件中搜索字符串? - How to search for string in files whose name contains another string and are created in last 7 days in linux? 如何在 Linux 中提取文件中包含的字符串,方法是将其与包含它的文件的名称相关联 - How I can extract, in Linux, a string contained in the files by associating it with the name of the file that contains it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM