Yash
Yash

Reputation: 3114

rsync command to delete a directory on a remote system

There is a remote system where i have read/write permissions only in one directoty. I use it to store the results of the nightly builds.

For uploading the results for a specific day, i do an rsync of <date_dir> on the remote directory using following command:

rsync -zavR <date_dir> <server_name>:/path/of/the/directory/where/i/have/permission/

Now, sometimes i need to delete the <date_dir> directory at destination. For this, I tried following command:

rsync -avh <date_dir> <server_name>:/path/of/the/directory/where/i/have/permission/ --delete

This command deletes the contents of <date_dir> at destination but the directory <date_dir> itself is not deleted.

I also tried to give --force-delete in rsync command, but it gives following error on my system:

rsync: --force-delete: unknown option

What can be the command to delete the <date_dir> directory on the remote server using rsync.

PS: I tried setting up password-less authentication using SSH but i don't have permissions to do that.

Upvotes: 0

Views: 2801

Answers (3)

atomicpirate
atomicpirate

Reputation: 695

If you can execute a remote rm command that is almost certainly safest. But there may be situations where you don't have direct shell access to the remote side, for example when the ssh key you are using is only allowed to run a restricted set of commands in authorized_keys and rm is not one of them.

You can accomplish the task with rsync alone as shown below, but you should test carefully and be extra sure of your inputs:

rsync -avh -n --delete \
  --filter="+ <date_dir>/**" \
  --filter="+ <date_dir>" \
  --filter="- *" \
  /path/to/empty/ <server_name>:/path/of/the/directory/where/i/have/permission/

The idea is to synchronize an EMPTY directory at /path/to/empty with the remote directory that you have permission to modify. Without filters, of course, that would just delete EVERYTHING in the remote directory.

So we use the fact that rsync applies filters in order, using the first matching rule. The first filter deletes the contents of <date_dir>, the second filter deletes <date_dir> itself, and the last filter saves us from deleting the rest of the directory's contents.

I've added a -n dry run flag just in case someone isn't paying close attention and pastes this command. Remove it to run the command for real.

Upvotes: 1

Yash
Yash

Reputation: 3114

SOLUTION:

In the given context following command works for me:

rsh <server_name> "rm -rf /path/of/the/directory/where/i/have/permission/<date_dir>"

Upvotes: 0

user1934428
user1934428

Reputation: 22217

Since this is the very directory you are going to copy, rsync won't delete it. --delete means to remove a directory or file which does not exist on the senders side. Obviously, date_dir exists on your local machine (otherwise rsync would complain about copying a non-existent directory). If you want to keep your local directory and only delete the remove copy, rsync is not the right tool, but you could use rsh ..... rm -r to remove the directory on the remote side.

Upvotes: 1

Related Questions