Copy/Sync a File on a Local Computer In the above example, you can see that if the destination is not already existed rsync will create a directory automatically for the destination.
Rsync is a command-line tool for copying files and directories between local and remote systems that should be in every Linux sysadmin's toolbox.
You also have an empty directory called dir2 . To sync the contents of dir1 to dir2 on the same system, you will run rsync and use the -r flag, which stands for “recursive” and is necessary for directory syncing: rsync -r dir1/ dir2.
If you have more than the last leaf directory to be created, you can either run a separate ssh ... mkdir -p
first, or use the --rsync-path
trick as explained here :
rsync -a --rsync-path="mkdir -p /tmp/x/y/z/ && rsync" $source user@remote:/tmp/x/y/z/
Or use the --relative
option as suggested by Tony. In that case, you only specify the root of the destination, which must exist, and not the directory structure of the source, which will be created:
rsync -a --relative /new/x/y/z/ user@remote:/pre_existing/dir/
This way, you will end up with /pre_existing/dir/new/x/y/z/
And if you want to have "y/z/" created, but not inside "new/x/", you can add ./
where you want --relative
to begin:
rsync -a --relative /new/x/./y/z/ user@remote:/pre_existing/dir/
would create /pre_existing/dir/y/z/.
Assuming you are using ssh to connect rsync, what about to send a ssh command before:
ssh user@server mkdir -p existingdir/newdir
if it already exists, nothing happens
The -R, --relative
option will do this.
For example: if you want to backup /var/named/chroot
and create the same directory structure on the remote server then -R
will do just that.
From the rsync
manual page (man rsync
):
--mkpath create the destination's path component
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With