Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 have a script that uses scp to backup some files of my PC into another PC connected in LAN, something like that:

scp some_directories host@ip-address:backupFolder/

I use a public Key so it works without insert any password, I have just to execute it.

The problem is that if I backup some file from my PC and then I remove it (from my PC not from the remote one), when I execute this script again it copy the new files and overwrites the existing ones but does not remove the previous backuped files that are no longer present in my PC.

So I need a way to remove files in the remote PC via some script. The only way I know is to login in with ssh host@ip and then to use rm but clearly I can't write a script like this:

ssh host@ip-address
rm -r backupFolder/

because in this way the rm command look for the backupFolder in my PC and not in the remote one.

share|improve this question
up vote 0 down vote accepted

You description is a bit vague. BUt I reckon what you want to archive is to sync the remote backup folder with your PC (sync directories).

In this case, the most suitable tool would be rsync instead of scp.

Good news is that rsync works perfectly fine over SSH, your public key authentication will keep working like a charm.

You can use command like below to keep your backup on the remote server in sync with your PC

rsync -avz --delete --progress --stats some_directories host@ip-address:backupFolder/

NOTE: be careful with rsync's trailing slash => /. Read the man pages.

share|improve this answer
Thank you very much I didn't know this good command – Red Jun 5 '13 at 11:36

Just append the command you want to run remotely.

ssh user@ip-address rm -r backupFolder/

This runs the single command on the remote machine. The manpage of the SSH client tells you this syntax:

     ssh [-1246AaCfgKkMNnqsTtVvXxYy] ...more options... [user@]hostname [command]

As you mentioned in your question already, one should use public key authentication to login without being asked for a password. For other visitors, see How can I set up password-less SSH login? for that.

share|improve this answer

Either you could have a script using

ssh host@ip-address << EOF
`rm foo`

Or you can pipe commands to ssh using grave accents in the terminal, bypassing the need for writing a script at all. For example:

ssh host@ip-address:folder `rm foo`


share|improve this answer

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.