There are several syntax problems, one of which is fatal and another which is likely to bite you at some point.
By the way, you can use +
instead of ;
to end the -exec
directive; this way gzip
will be executed in batches, which is slightly faster.
If the value of LOGS_DIR
is /somewhere/with/logs
, then the following command is executed on the remote host:
cd /somewhere/with/logs; echo cd /somewhere/with/logs; find . -name *.log -mmin +1440 -exec gzip {} \; exit
or, substituting newlines for the equivalent semicolon operator:
cd /somewhere/with/logs
echo cd /somewhere/with/logs
find . -name *.log -mmin +1440 -exec gzip {} \; exit
When find
sees that spurious exit
after the -exec … ;
directive, it doesn't know what to do with it; it hazards a (wrong) guess that you meant it to be a path to traverse. You need a command separator: put another ;
after \;
(with or without a space before).
A second problem is that the wildcard pattern *.log
will be expanded in the directory /somewhere/with/logs
. If there is a matching file (i.e. if there is a file of the form /somewhere/with/logs/something.log
), the pattern will be replaced by that matching file. It's only if there is no match that *.log
remains unchanged by the remote shell and is seen by find. Add quotes around the pattern to prevent this.
A third problem is with the value of LOGS_DIR
. It is expanded locally into a string which is executed as a shell script on the remote side. So if there are any shell special characters (whitespace, $
, etc.) in it, this can execute an arbitrary remote command. A quick-and-dirty way to quote most characters is to put single quotes around the variable expansion; this will break only if the value contains single quotes. If your local script is running bash, you can replace single quotes by the 4-character sequence '\''
to protect the value of LOGS_DIR
:
ssh -q "$CUR_HOST" "cd '${LOGS_DIR//\'/\'\\\'\'}'; find . -name '*.log' -mmin +1440 -exec gzip {} +; exit"
*.log
('*.log'
) to prevent globbing in the remote shell.