1

This is the command I'm entering.

find /home/things/backups -mtime +0 -exec echo "{}" >> /home/things/logs/backup.log \; -exec echo "Hourly rotate successful: $(date). {} was deleted." >> /home/things/logs/backup.log; \;

and the output is below.

find: missing argument to `-exec'

;: command not found

It creates the file backup.log fine, but the file is empty.

2 Answers 2

1

Using:

find /home/things/backups -mtime +0 -exec \
    sh -c 'echo "$0" >> /home/things/logs/backup.log; echo "Hourly rotate successful: $(date). $0 was deleted." >> /home/things/logs/backup.log;' {} \;

We start a new shell sh with a command -c and the parameter {} (the find result). In the command we use this parameter via $0.

2
  • alright, now if I wanted to delete the files instead of the first echo would I just replace the echo with rm? Jul 1, 2015 at 19:20
  • Sorry, be fair, I corrected your script. For a new question ask a new question.
    – A.B.
    Jul 1, 2015 at 19:25
1

Let's reposition the redirections a bit and add some line breaks to see what you're actually running:

 >> /home/things/logs/backup.log \
   >> /home/things/logs/backup.log \
   find /home/things/backups -mtime +0 -exec echo "{}" \; \
     -exec echo "Hourly rotate successful: $(date). {} was deleted.";
   \;
  • You're redirecting find's output, not that of the commands run by -exec. This would affect the commands run using -exec since they are child processes and inherit open file descriptors, so the effect would be what you needed even if the way you went about it was weird.

  • Then the ; at the end of the second exec wasn't escaped. The find command got terminated there without a ; for exec, causing the error. find quit immediately because of this, so no output was produced.

  • The \; at the end is then processed by the shell. Usually, it would be a statement delimiter, but since it is escaped, it tries to run it as a command. This fails, naturally.

Consider simplifying the command:

find /home/things/backups -mtime +0 -exec \
  bash -c 'printf "%s\nHourly rotate successful: $(date). %s was deleted.\n" "$0" "$0"' {} \; \
  >> /home/things/logs/backup.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .