1

We want to delete the files under /tmp that are older than 100 minutes.

What is the difference between:

find /tmp -type f -mmin +100 -exec rm {} \;

and:

find /tmp -type f -mmin +100 -exec -delete 

second - Is it possible to add some print / echo in the command so that it will print each file it deletes?

/tmp/hadoop-unjar7118762490947462979/META-INF/NOTICE was deleted !!!
terdon
  • 242,166
yael
  • 13,106
  • 2
    What's a "safe deletion"? – Jeff Schaller Dec 17 '18 at 17:23
  • since we are working on production machine , then I want to understand what the approach that is more safe , second how to add some safe rule that the command will deleted the files only under /tmp – yael Dec 17 '18 at 17:25
  • 1
    @yael that does not clarify user Jeff Schaller's question. Please edit your post to elaborate what exactly a "safe deletion" would be. – kemotep Dec 17 '18 at 17:31
  • I edit my question , hope it is clearly now – yael Dec 17 '18 at 17:34
  • If you're not sure what's going to be deleted, perhaps a "print-only" report would be a good start, followed by an "archive" or "move" action, then later followed by a remove action. – Jeff Schaller Dec 17 '18 at 17:44

1 Answers1

4

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.

Kusalananda
  • 333,661
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 1
    Maybe worth adding -xdev just in case someone does something silly like mount --bind / /tmp/foobar – Stephen Harris Dec 17 '18 at 17:36
  • yes this is very good point , in case some user run - find / -type , insted of find /tmp -type , then how to avoid thsi such case , can we add in the cli some additional syntax that will avoid other partitions as "/" – yael Dec 17 '18 at 17:40
  • obviously running find / ... -delete is less "safe" than running find /tmp ... -delete; I'm not sure how to address the "safeness" here. – Jeff Schaller Dec 17 '18 at 17:43