Synchronizing files with rsync
- Version
- unversioned
- Review
- Draft
Summary#
rsync copies and synchronizes files locally or between hosts, normally skipping files that appear unchanged. -a copies directory trees and preserves most file attributes.
Syntax#
rsync -a source/ destination/
rsync -a --delete --dry-run --itemize-changes source/ destination/
rsync -a source user@host:destination/
A trailing slash on the source copies the directory's contents. Without it, an existing destination directory gets the source directory as a child.
Example#
(
set -eu
export LC_ALL=C
dir=$(mktemp -d)
trap 'rm -rf -- "$dir"' EXIT
cd "$dir"
mkdir -p site/img mirror
echo 'home' > site/index.html
echo 'logo' > site/img/logo.svg
echo 'stale' > mirror/old.html
rsync -a site/ mirror/
find mirror -type f | sort
echo '-- preview deletion'
rsync -a --delete --dry-run --itemize-changes site/ mirror/
test -f mirror/old.html
echo 'old.html still exists'
echo '-- source without a trailing slash'
rsync -a site mirror/
find mirror -type f | sort
)
Expected output#
mirror/img/logo.svg
mirror/index.html
mirror/old.html
-- preview deletion
*deleting old.html
old.html still exists
-- source without a trailing slash
mirror/img/logo.svg
mirror/index.html
mirror/old.html
mirror/site/img/logo.svg
mirror/site/index.html
Common mistakes#
rsync can replace destination files, and --delete removes extra files from the directories being synchronized. Run the exact command with --dry-run and --itemize-changes first, and check whether the source needs a trailing slash. The default quick check uses size and modification time. Use --checksum if files with matching size and time may still differ.
Version applicability#
This entry describes rsync from the rsync project. On macOS, check rsync --version: Apple has shipped both rsync 2.6.9 and openrsync under that name, and their options and output can differ.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0