I'm new to this, wondering if someone could help.

I need to ssh to a server and bind commands to it, which copies a directory, zips it up etc..

ssh user@123.45.67.89 ; cd /to/directory/ ; zip these files

Something similar to the above. If anyone could offer advice, would be a great help! Thanks

link|improve this question

59% accept rate
feedback

1 Answer

You can append a command to ssh:

ssh user@123.45.67.89 "cd /to/directory/ ; gzip -r somedir > test.zip"

Be careful of string expansion:

Double quotes get expanded on the local machine.

ssh user@123.45.67.89 "gzip -r /var/www/$(hostname)/httpdocs > test.zip"

expands to: gzip -r /var/www/localhostname/httpdocs > test.zip

Single quotes get expanded on the remote machine.

ssh user@123.45.67.89 'gzip -r /var/www/$(hostname)/httpdocs > test.zip'

expands to: gzip -r /var/www/remotehostname/httpdocs > test.zip

link|improve this answer
Am I missing something, or did you re-draft the expansion examples and break them in the process? – jimbo Mar 3 '11 at 13:08
@Jimbo nope, the examples above are correct, what's broken for you? – Andy Mar 3 '11 at 15:55
I don't see where the 'rm' or the '-rf' come from – jimbo Mar 4 '11 at 9:41
@Jimbo Aha point taken, thanks! – Andy Mar 4 '11 at 10:32
ah that's alright, I was beginning to doubt my sanity! – jimbo Mar 5 '11 at 0:34
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.