How to count the number of files moved?

I'm writing a script for searching substring in file content and then moving found files. So far I've wrote script shown below

grep -lir 'stringtofind' $1 | xargs mv -t $2

How can i count number of files moved?

Assuming one file per line:

grep -lir 'stringtofind' "$1" | awk '1 ; END { print NR >"/dev/stderr" }' | xargs mv -t "$2"

awk will print the number of lines it counted to standard error, direct to the screen, and print all lines to standard out, right back into mv.

1 Like

Works like a charm! Thanks!! :))

mv has another good flag to report what just did, e.i -v

To see it in realtime

grep -lir 'stringtofind' "$1" | xargs mv -v -t $2

You can use that to just show count as well

grep -lir 'stringtofind' "$1" | xargs mv -v -t $2 2>/dev/null | wc -l

thanks needed this :slight_smile: