Operating System - HP-UX
1828670 Members
2034 Online
109984 Solutions
New Discussion

find command returns error if it cant find file

 
SOLVED
Go to solution
Ratzie
Super Advisor

find command returns error if it cant find file

I have a cron that does some cleanup:
00 5 * * * find /dir/*dump/*.trc -mtime +14 -exec rm {} \;

Which is fine, but find complains if the directory is not there. Which in some cases it wont be.

produced the following output:

find: stat() error/dir/*dump/*.trc: No such file or directory

How do I run this find command in my cron, and not produce an error if it /dir/*dump/ does not exist?

4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: find command returns error if it cant find file

Hi:

# 00 5 * * * find /dir/*dump/*.trc -mtime +14 -exec rm {} \; 2> /dev/null

...will suppress the STDERR output you are seeing.

TO make your removal faster. The '-exec' terminated with a semicolon will spawn a process for every file found. Using the '+' terminator causes the bundling of many arguments (here, file names) together and then creating one or just a few processes to remove them.

# 00 5 * * * find /dir/*dump/*.trc -mtime +14 -exec rm {} + 2> /dev/null

Regards!

...JRF...

Steven Schweda
Honored Contributor

Re: find command returns error if it cant find file

> How do I run this find command [...]

You don't. You run something better. (Write
a script which does what you want?)

You could test for the directory/ies before
running the "find" command.

If a "/dir/*dump" was always there, but the
"*.trc" part was uncertain, then you might
do something like:

find /dir/*dump -name '*.trc'

Using a file-spec wildcard in a "find"
command is often a sign of a poor design.
Dennis Handly
Acclaimed Contributor

Re: find command returns error if it cant find file

>find /dir/*dump/*.trc -mtime +14 -exec rm {} + 2> /dev/null

You may also want to improve it by finding only files "-type f" and/or adding "-f" or "-rf" to your rm:
find /dir/*dump/*.trc -mtime +14 -exec rm -rf {} + 2> /dev/null
Ratzie
Super Advisor

Re: find command returns error if it cant find file

worked like a charm! Thanks!