Two alternatives come to mind for printing the file as it's being removed:
find /tmp -type f -mmin +100 -printf '%p was deleted!!\n' -delete
or
find /tmp -type f -mmin +100 -exec rm -v {} \;
The former instructs GNU find
to print the file name (fully-pathed) before deleting it; the latter tells find
to execute rm -v
on each filename, where -v
instructs (GNU) rm
to be verbose about what it's doing.
Output of the former would be like:
/tmp/.sh_history.6668 was deleted!!
/tmp/krb5cc_6094 was deleted!!
/tmp/.sh_history.18394 was deleted!!
While the output of the latter would be:
removed ‘/tmp/.sh_history.6668’
removed ‘/tmp/krb5cc_6094’
removed ‘/tmp/.sh_history.18394’
Another item to note on -exec rm {}
vs -delete
is that -exec rm
will search your $PATH for rm
while -delete
directly unlinks the file. Not usually an issue, but something to be aware of.
Example:
$ pwd
/tmp/jeff
$ ls
bar foo rm
$ cat rm
#!/bin/sh
echo Hi, I am the fake rm: "$@"
$ PATH=/tmp/jeff:$PATH
$ find . -type f -exec rm {} \;
Hi, I am the fake rm: ./rm
Hi, I am the fake rm: ./bar
Hi, I am the fake rm: ./foo
With -delete
, find
will also traverse the search path in a depth-first manner by default. This allows it to delete directories that it will not later try to enter. You would have to use find
with -depth
if you use -exec rm -rf {}
on directories, or you will cause find
to complain about not finding the directories that it thought were there.