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

3
  • I just thought about something... I could check using wc -l how many lines of output were received from the find command... Commented Feb 24, 2015 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. Commented Feb 24, 2015 at 11:29
  • I found an elegant way of doing this: stackoverflow.com/a/71375403/522104 Commented Mar 7, 2022 at 0:53

1 Answer 1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.