Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 343
Srujan Kumar Gulla Avatar asked Dec 05 '13 16:12

Srujan Kumar Gulla


People also ask

How do I delete files with similar names in Linux?

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 do you find and delete files recursively in Linux?

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.


2 Answers

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 image 138
William Pursell Avatar answered Nov 16 '22 01:11

William Pursell


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
like image 39
mhoughton125 Avatar answered Nov 16 '22 01:11

mhoughton125