Remove empty files and save a list of deleted files

Solution 1:

When I redirect the output of your command to a file in ., it gets delete by the find command before anything is written to it, since it is empty.

To solve this, make sure the output file is not empty at the beginning, or save it elsewhere:

find . -type f -empty -print -delete > ../log

or

date > log
find . -type f -empty -print -delete >> log

or, adapted from @DanielFarrell's comment:

find . -type f -empty -a -not -wholename ./log  -print -delete > log

The added -a -not -wholename ./log excludes ./log from the find operation.

Solution 2:

You can use -exec option with rm command instead of -delete.

find . -type f -emtpy -exec rm --verbose {} \; >> logfile.txt

logfile.txt:

removed './emptyfile1'
removed './emptyfile0'

Or you can use pipes and xargs for a more clean output:

find . -type f -empty | xargs ls | tee -a logfile.txt | xargs rm

This will give you only deleted filenames.