Rsync snippets

Code snippets for Rsync.

Copy from remote machine to local machine

To transfer a folder from the remote machine over to the local machine:

$ rsync -a USER@HOST:/home/remote_username/remote_directory /home/local_username/local_directory

This operation is called pullas it pulls the remote directory to the local directory.

Copy from local machine to remote machine

To transfer a folder from the local machine over to the remote machine:

$ rsync -a /home/local_username/local_directory USER@HOST:/home/remote_username/remote_directory

This operation is called pushas it pushes the local directory to the remote directory.

Specify a different port

Use -p to specify a different port for the remote server host when using Rsync:

$ rsync -avz ssh -p 1234 /home/local_username/local_directory USER@HOST:/home/remote_username/remote_directory

Resume progress and transfer

Unlike SCP, Rsync can resume transfers:

$ rsync -P -a /home/local_username/source_directory /home/local_username/destination_directory

Exclude files

To exclude certain files, such as .gitignore, use the --exclude parameter:

$ rsync /home/local_username/source_directory /home/local_username/destination_directory --include="**.gitignore" --exclude="/.git" --filter=":- .gitignore" --delete-after

Show progress during Rsync transfer

Use the -p option to show progress during a Rsync transfer:

$ rsync -av -p /home/local_username/source_directory/ /home/local_username/destination_directory/

-p is the same as -progress:

$ rsync -av -progress /home/local_username/source_directory/ /home/local_username/destination_directory/

Rsync progress example

For more information on each individual file progress, use the --info=progress2 option:

$ rsync -av --info=progress2 /home/local_username/source_directory/ /home/local_username/destination_directory/

Rsync info progress2 example

Delete source files & folder after Rsync transfer

To delete the source files & folders after the Rsync transfer is done, use the -remove-source-files parameter:

$ rsync -v --remove-source-files /home/local_username/source_directory/rsync.zip /home/local_username/destination_directory/

In this example, the rsync.zip ZIP archive will be copied to the destination_directory/ folder and then it would be deleted.