Console9

How to create a destination folder with Rsync

Create a non-existent destination directory on a remote server during an Rsync transfer using --relative or --rsync-path with mkdir.

Create a non-existent destination directory on a remote server during an Rsync file transfer without manually logging in to create it first.

Prerequisites

  • Rsync installed on both the local and remote machines. See How to Install Rsync.
  • SSH access to the remote server.
  • Write permissions on the parent directory of the destination path on the remote server.

Step-by-Step: Create a Destination Folder with Rsync

Rsync creates missing destination directories automatically when the destination path ends with a trailing slash and the parentdirectory exists. If the parent directory does not exist, Rsync fails with an error.

rsync -avz /home/user/src/ user@203.0.113.10:/home/user/existing_parent/new_folder/

Rsync creates new_folder/ inside existing_parent/ on the remote server. The existing_parent/ directory must already exist.

When Rsync needs to create a deeper directory structure where multiple levels do not exist, use one of these two methods.

Method 1: Use the --relative Flag with Rsync

  1. Add the --relative flagto preserve and create the full source path on the destination. Rsync creates any missing directory levels on the remote server.

    rsync -avz --relative /home/user/./projects/app/ user@203.0.113.10:/backup/

    Rsync interprets the . in the source path as the boundary. It creates /backup/projects/app/ on the remote server. The /backup/ directory must exist; Rsync creates projects/ and app/ automatically.

  2. Verify the directory structureon the remote server.

    ssh user@203.0.113.10 "ls -la /backup/projects/app/"

Method 2: Use --rsync-path with mkdir on the Remote Server

  1. Pass a mkdir -p commandvia the --rsync-path flag. Rsync executes this command on the remote server before starting the transfer.

    rsync -avz --rsync-path="mkdir -p /home/user/deep/nested/folder && rsync" /home/user/src/ user@203.0.113.10:/home/user/deep/nested/folder/

    Rsync runs mkdir -p on the remote server to create the full directory path, then starts the file transfer. The -p flag on mkdir creates all intermediate directories without errors if they already exist.

  2. Verify the transferby listing the destination on the remote server.

    ssh user@203.0.113.10 "ls -la /home/user/deep/nested/folder/"

How to Verify the Destination Folder Was Created

Rsync confirms a successful transfer by printing the list of transferred files. Verify the remote directory structure with an SSH command.

ssh user@203.0.113.10 "find /home/user/deep/nested/folder/ -type f | head -10"

Rsync lists the first 10 files in the created directory tree. A non-empty output confirms that the destination folder was created and files were transferred.

Common Issues When Creating Destination Folders with Rsync

Rsync fails with "No such file or directory" when the parent of the destination path does not exist and the --relative or --rsync-path flag is not used. Add --relative or --rsync-path="mkdir -p /path && rsync" to create missing directories.

Rsync fails with "Permission denied" on the remote server when the SSH user lacks write access to the parent directory. Verify ownership and permissions on the remote directory with ls -la.

Rsync's --rsync-path flag does not work when the remote shell restricts command execution. Verify that the remote SSH configuration allows running arbitrary commands.