How to create a destination folder using Rsync

Use Rsync to create a destination folder (even if it doesn't exist) on the remote server.

When syncing with Rsync, you create a destination folder on the remote server even if that destination folder doesn't exit yet.

For example, the usual Rsync sync command requires the /my_directory folder to be created already:

$ rsync FILE USER@HOST:/my_existing_directory/

The --relative option can be used to sync to the remote machine even if the directory isn't created yet. Using the --relative parameter, we only specify the root of the destination folder (which must exist) and not the directory structure of the source which will be created:

$ rsync -a --relative /my_existing_directory/my_not_existing_folder/ USER@HOST:/my_existing_directory/my_not_existing_folder/

The above command will create a directory with the following path /my_existing_directory/my_not_existing_folder/. In this example, the my_existing_directory/ folder is already created on the remote machine.

You can also use the rsync-path parameter available in Rsync. This specifies what program will run on the remote machine when starting Rsync.

$ rsync -a --rsync-path="mkdir -p /my_existing_directory/my_not_existing_folder/ && rsync" SOURCE_FOLDER USER@REMOTE:/my_existing_directory/my_not_existing_folder/