Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 trying to figure out a way to perform the following loop on a remote host via ssh.

Basically it renames a series of directories to create a rotating backup. But it's local. I want it to work against directories on a remote host.

while [ $n -gt 0 ];
    do {
    if [ -d /backup/$src ];
    then {
    mv /backup/$src /backup/$dst;
share|improve this question
up vote 3 down vote accepted

If you're using bash, you'll want to use [[ and ]] instead of [ and ]. It makes it much easier to do comparisons like you're doing here.

First, what is $n's starting value?

Try this:

ssh usr@host 'for n in {10..1}; do [[ -d /backup/$(($n-1)) ]] && /bin/mv /backup/$(($n-1)) /backup/${n}; done'

That right there should do it. That's assuming your starting number is 10. Depending on where $n is coming from, that can be modified. Notice that mv has the full path specified... this is because when you execute a remote session in ssh, it tends not to execute your .bashrc and .bash_profile meaning you don't have a ${PATH} yet set. There might be a way around it, but I don't know what it is.

Written out more human-readably:

for n in {10..1}
  if [[ -d /backup/$src ]]
    /bin/mv /backup/$src /backup/$dst

Since you're executing it remotely, I figured it would be easier in a single line.

share|improve this answer
This is in line with what I'm trying but, I get syntax error near unexpected token do'` – I Forgot Apr 14 '12 at 21:06
I screwed it up, I apologize. Swap the " with ' – UtahJarhead Apr 14 '12 at 21:28
Look at the current version. I tested it on my side (with commands other than mv) and this works perfectly on my side. Notice I removed /bin/bash – UtahJarhead Apr 14 '12 at 21:29
That did it. Mission accomplished. Thanks! – I Forgot Apr 14 '12 at 21:52

Place the script in a file on your local machine, e.g. and run the remote execution like this:

ssh user@remotehost <

This allows you to run the script on the remote host without actually copying it to the remote host.

I use this method for gathering hardware specifications from remote systems.

share|improve this answer
Thanks. This could work, but I am trying to keep it all in one script rather than scripts calling scripts. – I Forgot Apr 14 '12 at 21:07
Encapsulating this in a file will be the most maintainable and human-readable/modular approach. – ewwhite Apr 14 '12 at 21:10
You can do ssh user@host <<EOF [enter]your commands[enter]EOF to keep the script in line. – arjarj Apr 14 '12 at 21:10
I agree that this should have worked. But, there are all sorts of interpreter and piping issues. Thanks anyway – I Forgot Apr 14 '12 at 21:53

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.