Skip to content

Instantly share code, notes, and snippets.

@harishkannarao
Last active June 17, 2025 08:58
Show Gist options
  • Select an option

  • Save harishkannarao/217d677ecb374115bdc648dd89c9ab2c to your computer and use it in GitHub Desktop.

Select an option

Save harishkannarao/217d677ecb374115bdc648dd89c9ab2c to your computer and use it in GitHub Desktop.
Practical rsync command and options

Copy new files from source to target and skip files if it already exist in target

rsync --dry-run -avzurh --stats --delete --progress /tmp/source/ /tmp/target/

Explanation of flags:

  • -a -> Archive mode, preserve the file system properties
  • -v -> Verbose output
  • -vv -> Higher Verbose output
  • -vvv -> Even Higher Verbose output
  • -vvvv -> Highest Verbose output
  • -z -> Use compression mode for data transfer, speeds up for transfer over network
  • -h -> Output in human readable format
  • -u -> Skip if a file already exist in target directory, makes it efficient to copy only the missing files in target directory. Without this flag, it will overwrite the file in the target directory
  • -r -> recursive mode, copies subdirectory
  • --delete -> delete the additional files or directories which are present in target directory which are not present in source directory
  • --dry-run -> do a dry run without making any changes to the file system
  • --stats -> give the stats at the end of the process
  • --progress -> show progress while copying the files

Examples

Copy the source directory itself into the target directory (missing trailing slash at the end)

rsync -v -azurh /tmp/source /tmp/target

Copy from local source directory to remote directory

rsync -vv -azurh /tmp/source/ remote_user@example.com:/tmp/remote-target/

Copy from remote source directory to local target directory

rsync -vvv -azurh remote_user@example.com:/tmp/remote-source/ /tmp/target/

Copy from remote source directory to remote target directory

rsync -vvvv -azurh remote_user@server1.example.com:/tmp/remote-source/ remote_user@server2.example.com:/tmp/remote-target/

Include and Exlcude

Copies all files except logs

rsync -v -azurh --exclude '*.log' /tmp/source/ /tmp/target/

Copies only log files, text files and excludes all other files

rsync -v -azurh --include '*.log' --include '*.txt' --exclude '*' /tmp/source/ /tmp/target/

Copies only important logs file and excludes all other log files

rsync -v -azurh --include '*sensitive*.log' --include '*important*.log' --exclude '*.log' /tmp/source/ /tmp/target/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment