Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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@ ; cd /to/directory/ ; zip these files

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

share|improve this question

You can append a command to ssh:

ssh user@ "cd /to/directory/ ; gzip -r somedir >"

Be careful of string expansion:

Double quotes get expanded on the local machine.

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

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

Single quotes get expanded on the remote machine.

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

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

share|improve this answer
Am I missing something, or did you re-draft the expansion examples and break them in the process? – James Green 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 – James Green 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! – James Green Mar 5 '11 at 0:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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