Linux find and delete files but redirect file names to be deleted

Srujan Kumar Gulla picture Srujan Kumar Gulla · Dec 5, 2013 · Viewed 14.6k times · Source

Is there a way to write the file names to a file before they are deleted for reference later to check what has been deleted.

find <PATH> -type f -name "<filePattern>" -mtime +1 -delete 

Answer

William Pursell picture William Pursell · Dec 5, 2013

Just add a -print expression to the invocation of find:

find <PATH> -type f -name "<filePattern>" -mtime +1 -delete -print > log

I'm not sure if this prints the name before or after the file is unlinked, but it should not matter. I suspect -delete -print unlinks before it prints, while -print -delete will print before it unlinks.