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
use rm -i PATTERN . You will have to confirm each removal manually. before doing rm you could do an ls PATTERN or echo PATTERN and check if the list is ok.
How to find and delete directory recursively on Linux or Unix-like system. I type ' find . -type d -iname foo -delete ' command to find all foo directories and delete them.
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.
Like William said, you can use -print
. However, instead of -print > log
, you can also use the -fprint
flag.
You'd want something like:
find <PATH> -type f -name "<filePattern>" -mtime +1 -fprint "<pathToLog>" -delete
For instance, I use this in a script:
find . -type d -name .~tmp~ -fprint /var/log/rsync-index-removal.log -delete
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With