If you – like me – copy image files regularly to an external HD, you might also have come across the problem that sometimes the file references are created but the eventual copying of the file contents failed. Thus, I regularly end up with a lot of dead files that block the way for the real files when I want to copy the actual files later.

Therefore, I have come up with the following shell script that finds empty files in the current folder and deletes them:
for i in * ; do if [ ! -s "$i" ] ; then rm $i ; fi ; done

To use the script, cd to the folder containing the affected files in the command line and then paste and execute the script. Beware that you cannot undo the deletion of the files. Hence, before you execute the script, I recommend executing the following shell script at first that lists you the affected files:
for i in * ; do if [ ! -s "$i" ] ; then echo $i ; fi ; done

As soon as you are sure that the correct files will be deleted, execute the first script.

Note: As you can easily test with the second script, folders will not be listed/removed using the scripts.

Published by Johannes Luderschmidt

About this blog