Search code examples
linuxbashshellunixfind

How to capture both success and error messages for linux "find" command


I'm trying to run an auto-delete script to free up space on a remote server. The command I'm thinking to use is:
find . -atime +30 -mtime +30 -type f -delete

What I want is to also capture which files were successfully deleted and which failed because of access issue. How should I do this? I think this command below might take care of the failures only, but I'm not sure.
find . -atime +30 -mtime +30 -type f -delete 2>failed_deletions.txt


Solution

  • find out of the box does not print the files it processes. If you want to list the files, add a -print or -ls before the -delete.

    This obviously prints all the files it processes, including the ones it fails to delete for whatever reason.

    Redirecting standard output to a different file should be trivial to discover; command >stdout 2>stderr

    The final command would become

    find . -atime +30 -mtime +30 -type f \
        -print -delete >success.txt 2>errors.txt