You could accomplish both tasks in the same command by using the --delete
flag to rsync
.
> ls -1 localdir/
a.foo
b.foo
> ssh remote-host "ls -1 remotedir/"
c.foo
> rsync -a --delete localdir/ remote-host:remotedir/
> ssh remote-host "ls -1 remotedir/"
a.foo
b.foo
The --delete
option removes files from the destination directory that don't exist on the source. There are some choices about when the files are deleted and how to handle excluded files.
> man rysnc
...
--delete delete extraneous files from dest dirs
--delete-before receiver deletes before transfer (default)
--delete-during receiver deletes during xfer, not before
--delete-after receiver deletes after transfer, not before
--delete-excluded also delete excluded files from dest dirs
The man page also contains this warning:
This option can be dangerous if used incorrectly! It is a very good idea to run first using the --dry-run option
(-n
) to see what files would be deleted to make sure important files aren't listed.