Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rsync a file to a remote directory that doesn't exist?

Tags:

rsync

Suppose I want to rsync file foo.txt on my local machine to file /home/me/somedirectory/bar.txt on a remote computer, and that somedirectory/ doesn't yet exist. How do I do this?

I tried rsync -e ssh -z foo.txt remotemachine:/home/me/somedirectory/bar.txt, but I get a rsync: push_dir#3 "/home/me/somedirectory" failed: No such file or directory (2) error.

(Copying the file without renaming it works, though. That is, this runs fine: rsync -e ssh -z foo.txt remotemachine:/home/me/somedirectory/`)

like image 377
grautur Avatar asked Dec 01 '11 00:12

grautur


People also ask

Will rsync Skip existing files?

Rsync with --ignore-existing-files: We can also skip the already existing files on the destination. This can generally be used when we are performing backups using the –link-dest option, while continuing a backup run that got interrupted. So any files that do not exist on the destination will be copied over.

How do I rsync from one directory to another?

If you want to copy a directory with its sub-directory and all contents from one location to another within your system, you can do so as by typing rsync followed by the source and destination directory. Note: Specifying “/” after the source directory only copies the contents of the directory.

Does rsync work both ways?

rsync works in one direction, so we need to run it twice to sync directories in both directions.

Does rsync create destination directory?

In the above example, you can see that if the destination is not already existed rsync will create a directory automatically for the destination.


2 Answers

Just put a trailing slash on your target dir. Something like this:

rsync foo.txt remotemachine:somedirectory/

Assuming that "/home/me" is your home dir on the remote machine, there is no need to specify it in the command line. Also, you don't need to clutter up your rsync with the -e unless you just like to do that.

like image 175
James Parks Avatar answered Sep 29 '22 18:09

James Parks


You can do this process successfully in 2 stepes:-

1] rsync -e ssh -z foo.txt remotemachine:/home/me/somedirectory/ this will copy the foo.txt and create directory somedirectory on destination.

then

2] rsync -e ssh -z --delete-after foo.txt remotemachine:/home/me/somedirectory/bar.txt

and here you can delete foo.txt on destination by using --delete-after option. you can see it's usage from man pages. This option must be used with -r option This serves your purpose.

or if second command doesn't work then use :- rsync -e ssh -z foo.txt remotemachine:/home/me/somedirectory/bar.txt and delete foo.txt manually.

like image 40
Akshay Patil Avatar answered Sep 29 '22 18:09

Akshay Patil