I recently read How to find the total number of occurrences of text and files with find command, which asked about a way to find the number of times text "abc" appears in a file. The answer there provided the command find . -name "*.txt" | xargs grep -i "abc" | wc -l
to find the count.
Previously I have used something like more "file_name*" |grep "abc" |wc -l
to list the number if times text "abc" appears in a specific file(s).
I tried this and found that the more
command returned faster, but seemed to use ~30% more CPU (I was also monitoring via top
) than the find
command.
I was wondering if anyone had some more solid data on which of these two commands would be more intensive on resources if you were to query through approximately 15 files? How about 30+ files?
find . -print0 -name "*.txt" | xargs -0 -P 4 grep -i "abc" | wc -l
The combination of-print0
andxargs -0
helps with filenames containing spaces!more
displays multiple files, it inserts a header with the file name before each file's content? So if one of your files' name contains the string you search for,grep
ing inmore
's output will find extra occurrences in the file names. To avoid this, better usecat
.xargs -P 4
is what will create 4 separate instances of the command to run?