Search code examples
unixfindreturnkshrm

KSH - Check return code on individual commands in find -maxdepth 0 $path -type f -mtime +$daysold -exec rm -fv {}\;


I am trying to figure out how to check the return codes on the find command and the rm -fv command in the following statement:

find -maxdepth 0 $path -type f -mtime +$daysold -exec rm -fv {}\;

Basically, if an error occurs, I want to know if it occurred in the find or the rm command and forward that information to our developers via e-mail. How would I go about this? I understand if I look at $?, it will just look at the statement as a whole.


Solution

  • The find uses the return code of the -exec command as a predicate: if the command was successful - the file matches.

    Thus you can make the find to print all the files for which the rm -fv command has failed by negating the predicate with the !:

    find -maxdepth 0 $path -type f -mtime +$daysold ! -exec rm -fv {} \; -print
    

    (You have to add -print since the presence of -exec overrides the default implicit -print.)

    You can redirect the output of the find into a file, and if the file is not empty, then you can e-mail it accordingly.