rsync - macOS


Overview

rsync is a UNIX-like command designed for efficiently transferring and synchronizing files between two locations, even over remote networks. It uses a proprietary algorithm to minimize data transfer by only sending the differences between the source and destination files, enhancing speed and optimizing network usage.

Syntax

rsync [options] source destination

Options/Flags

  • -a, –archive: Preserves file attributes (permissions, ownership, etc.) and device files.
  • -v, –verbose: Increases verbosity, displaying detailed progress information.
  • -z, –compress: Compresses file data before transfer.
  • -h, –human-readable: Displays file sizes in human-readable format.
  • -n, –dry-run: Simulates the transfer without making any actual changes.
  • -u, –update: Only transfers files that are newer or don’t exist at the destination.

Examples

  • Copy files to a remote server:
rsync -av ~/Documents/project remote_server:/path/to/destination
  • Synchronize files between two local directories:
rsync -av ~/source_dir ~/destination_dir
  • Exclude certain files or directories from transfer:
rsync -av --exclude="*.txt" source destination

Common Issues

  • Permission denied: Ensure you have the necessary permissions to access the source and destination locations.
  • Network connectivity: Verify that there is a stable network connection between the systems.
  • Destination directory not found: Create the destination directory before running the rsync command.

Integration

  • Use rsync with cron: Schedule regular file backups by integrating rsync with crontab using a command like:
crontab -e
0 0 * * * rsync -av ~/Documents/project remote_server:/backup
  • scp: Securely copy files over SSH.
  • tar: Archive and compress files.
  • find: Locate files in a directory tree.