1823914 Members
3099 Online
109667 Solutions
New Discussion юеВ

Re: "rm -r `" help

 
SOLVED
Go to solution
Lucy2009_1
Frequent Advisor

"rm -r `" help

Hi,
I have a cron job running to clean logs old
than 3 days. There is always some error msg
in the log. How to get rid of it?

# /usr/bin/find $FPATH -mtime +3 -exec rm -r {} \; >> /tmp/clean_log 2>&1

Example msg in /tmp/clean_log:
/usr/bin/find: /my-path/xyz: no such file or directory.

The xyz directory already got removed even before removing the files in xyz. Then "rm -r"
try to remove it again?

Thanks in advance,

IDT

5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: "rm -r `" help

Hi:

You really want to descend the directory and deal with its contents first. Hence:

# /usr/bin/find $FPATH -depth -mtime +3 -exec rm -r {} \+ >> /tmp/clean_log 2>&1

Notice the addition of '-depth'. Notice, too, that I changed the semicolon (;) to a plus (+) character. This causes find to buffer many arguments together and '-exec' the process passing them as a block. This avoids spawning one process per file found, greatly improving performance.

Regards!

...JRF...

Lucy2009_1
Frequent Advisor

Re: "rm -r `" help

James,
It almost work but it complains:
find: missing argument to '-exec'

IDT
Peter Nikitka
Honored Contributor

Re: "rm -r `" help

Hi,

write a twoliner "/tmp/myrm"
#!/usr/bin/sh
rm -r "$@"

and after
chmod +x /tmp/myrm

use
find $FPATH -depth -mtime +3 -exec /tmp/myrm {} \+ >> /tmp/clean_log 2>&1

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: "rm -r `" help

Hi:

> It almost work but it complains:
find: missing argument to '-exec'

Then, I suspect that you are running a release much earlier than 11.11 or not an HP-UX. In this case, go back to the use of the semicolon in lieu of the "+", or pipe the 'find' output to 'xargs' to do the buffering.

By the way, since you are recursively removing files and directories, you could also do:

... rm -rf ...

...which has the property of suppressing non-existent file messages.

Beware, that by not specifying FILES (with '-type f') you may be taking DIRECTORIES and the FILES therein that you DON'T want! That is because any file that is removed from a directory causes the 'mtime' of the *directory* to be updated.

Regards!

...JRF...

Lucy2009_1
Frequent Advisor

Re: "rm -r `" help

Thanks James & Peter!