1

I use the following command in order to delete only the files that start with DBG and older then two day , but this syntax not print the files that deleted

find /tmp  -type f -mtime +2 -name "DBG*" -exec rm {} \;

How to add to this the find syntax , the print in order to print the deleted files?

  • 1
    Why don't you use rm -v? – cuonglm Feb 9 '16 at 16:22
6

Just use -print flag:

find /tmp  -type f -mtime +2 -name "DBG*" -exec rm {} \; -print

or, if rm supports the -v option, let rm do it all:

find /tmp  -type f -mtime +2 -name "DBG*" -exec rm -v {} +

or if your find supports -delete:

find /tmp  -type f -mtime +2 -name "DBG*" -delete -print

(note that the first two have a race condition that could allow one to delete DBG* files anywhere on the file system)

  • 1
    -print -exec rm {} \; prints the names of files which could not be deleted, too. – Hauke Laging Feb 9 '16 at 16:46
  • @HaukeLaging: Yeah, of course. I intended to edit it when I went back home, but Stephane did it for me. – cuonglm Feb 9 '16 at 17:03
2

Multiple immediate options:

find /tmp  -type f -mtime +2 -name "DBG*" -exec echo {} \; -delete

or

find /tmp  -type f -mtime +2 -name "DBG*" -exec echo {} \; -exec rm {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.