For a raspberry backup bash-script I want to log which and how many older backups are deleted.

I use

find $backup_path/$HOSTNAME.*.img -mtime +$retention_days -type f -delete

to clean.

How do I count and display and log to a file which files are deleted?

  • Thank you, I will check the various solutions. – Thuurke Dec 29 '18 at 18:30

You can use the -print option of find, to output the files that are deleted, then pipe things to tee to write results into a logfile. And finally count the lines of the deleted files and append it in the logfile.

find $backup_path/$HOSTNAME.*.img -mtime +$retention_days -type f -delete -print | tee ${LOGFILE} | wc -l | xargs echo "Files deleted:" >> ${LOGFILE}

If you want to append new results to the same ${LOGFILE} you would have to use tee -a.


find has an fprint action that can write the results of the find command to a file. You can then extend your command as:

find $backup_path/$HOSTNAME.*.img -mtime +$retention_days -type f -fprint /path/to/log.txt -delete

You can then retrieve the file count from the log file, by using wc:

cat /path/to/log.txt | wc -l

If special characters can appear in the results of find, you can use the the fprint0 option as an alternative. This will write the results to the specified file as null-delimited strings.

To count the items in the resulting file, you can look the options discussed under this question: Count null delimited items in file.

  • 1
    Not all implementations of find support fprint, GNU find does but busybox for example does not. It's also not mandated by POSIX: pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html. And you don't need cat to count number of lines with wc: wc -l <FILE> is enough. – Arkadiusz Drabczyk Dec 28 '18 at 13:43
  • @ArkadiuszDrabczyk Thanks for the information on POSIX find. Regarding the usage of cat, I used it so that the output is only a number. wc shows the filename beside the count with the wc -l file syntax. – Haxiel Dec 28 '18 at 13:49

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 '.' \

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.


You can use below command to achieve the same

find $backup_path/$HOSTNAME.*.img -mtime +$retention_days -type f -exec rm -rvf {} \; >log_file ===> This  will delete and writes to logfile which are the files getting deleted

wc -l log_file===> will display count of files which are deleted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.