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}
do
if [[ -d /backup/$src ]]
then
/bin/mv /backup/$src /backup/$dst
fi
fi
Since you're executing it remotely, I figured it would be easier in a single line.