I would like to run a CRONTAB job to delete files older than 5 days in a folder AND pipe the command output to a file in case of errors.
This command deletes the files when run from a command line:
/usr/bin/find /mnt/SQL_Backups/* -mtime +5 -exec rm {} \;
but, when I add this to it to get the the stdout and stderr pipe, it fails.
/usr/bin/find /mnt/SQL_Backups/* -mtime +5 -exec rm {} \; > /mnt/output/CRONDeleteFiles.txt 2>$1
From the command line, error is
-bash: $1: ambiguous redirect
while from CRONTAB email error message, I get this error
/bin/sh: 1: cannot create : Directory nonexistent
I suspect it has something to do with my piping code?
What is the right way to do this?
2>&1
not2>$1
. Also you can change\;
to+
to speed up deleting by the find command – αғsнιη Oct 28 '22 at 13:23