0

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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Thuurke
  • 1
  • 1
  • 1

4 Answers4

5

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.

Thomas
  • 6,242
  • 8
  • 26
  • 32
3

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.

Haxiel
  • 8,201
  • 1
  • 20
  • 30
  • 1
    Not all implementations of `find` support `fprint`, GNU find does but busybox for example does not. It's also not mandated by POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html. And you don't need cat to count number of lines with wc: `wc -l ` 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
1

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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
  • +1, The idea of using dots to count the number of processed files is very clever. – Haxiel Dec 29 '18 at 05:32
  • 1
    Thank you, it is! I cannot take any credit for it; I saw it here on U&L and also thought it was clever. [hluk in 2012](https://unix.stackexchange.com/a/38486/117549), [slm in 2013](https://unix.stackexchange.com/a/101418/117549), [Hauke Laging in 2014](https://unix.stackexchange.com/a/116649/117549), [Gilles in 2014](https://unix.stackexchange.com/a/123611/117549), [Stéphane in 2015](https://unix.stackexchange.com/a/231351/117549), [ilkkachu in 2017](https://unix.stackexchange.com/a/376167/117549), and others. – Jeff Schaller Dec 29 '18 at 13:42
0

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
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14