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. How to change it to check if it contains "main" in its content as well?
Loop over your files and use grep -q
which suppresses grep output:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.