2

I have a script which runs the next command:

find /storage2/hpfiles_staging/ -name \*.gz -mmin +600

And then rsync's the files to a remote location.

Sometimes the find command returns no output (as there are no files named *.gz with age of 10 minutes or more in that location). When it happens, I want to write a message to the log saying there are no files there.

But when I run the find command, whether it finds files to transfer or not the exit code of the find command is always "0" (unless there's a syntax error of course) and thus cannot give me indication about files being found in that location or not.

Is there any switch or a trick to get a different exit code than 0 if no files have been found by the find command?

Thanks in advance

Itai Ganot
  • 10,424
  • 27
  • 88
  • 143
  • I just thought about something... I could check using `wc -l` how many lines of output were received from the find command... – Itai Ganot Feb 24 '15 at 11:28
  • Just save the output to a variable and check if it's empty. I'll link to an existing question here that shows you how to do that next. –  Feb 24 '15 at 11:29
  • I found an elegant way of doing this: https://stackoverflow.com/a/71375403/522104 – asoundmove Mar 07 '22 at 00:53

1 Answers1

3

I don't think you can affect the exit code from find. You should perhaps look to capture the output from find and use that to determine if any files where found.

user9517
  • 114,104
  • 20
  • 206
  • 289