If you may have filenames with newlines in them (touch /the/path/$'foo\nbar'
to create an example), then you could avoid some confusion by using GNU find's -fprintf
feature to print the filenames to one logfile and a dot for each filename to a separate logfile. Then the byte count of the dotfile will equal the number of matching files and the filenames themselves will be in a separate file.
find "$backup_path"/"$HOSTNAME".*.img -mtime +"$retention_days" -type f \
-fprintf ./deleted-files '%p\n' \
-fprintf ./count-files '.' \
-delete
Above, I've specifically placed the two -fprintf
statements after the previous filtering criteria of -mtime
and -type f
and just before the -delete
, so that they are triggered only when -delete
would be.
The first new statement prints the file paths to the ./deleted-files
file; the second one prints a dot to ./count-files
. You may browse the deleted-files log for the deleted filenames and use wc -c < count-files
to report on the total number of deleted files. The filenames are overwritten by -fprintf
with each run.