Chapter 14: Archives and file transfer

tar and rsync cheat sheet

The chapter on one page: what to remember, the commands to have at your fingertips, and flashcards for quick revision.

The chapter in eight sentences

Cheat sheet

tar

CommandDoes
tar -czf A.tar.gz -C DIR NAMECreate (gzip)
tar -cJf A.tar.xz … / -cjf A.tar.bz2 …xz / bzip2
tar -tvf A.tar.gzList with details
tar -xf A.tar.gz -C DEST [member]Extract (all / one)
tar --exclude='PATTERN' …Leave out
tar … --acls --selinux --xattrsKeep ACLs, labels, attributes
diff -r ORIG RESTOREDProve the restore

Compress and check

CommandDoes
gzip -k F · gunzip F.gz · gzip -l F.gzgzip
xz -k F · unxz F.xz · xz -l F.xzxz
bzip2 -k F · bunzip2 F.bz2bzip2
zip -r A.zip DIR · unzip -l A.zip · unzip -t A.zipzip
sha256sum F > S · sha256sum -c SChecksums
cmp A BAre two files identical?

Backup and transfer

CommandDoes
tar --listed-incremental=S.snar -czf N.tar.gz DIRFull or incremental
tar --listed-incremental=/dev/null -xzf N.tar.gz -C DRestore in order
rsync -a --link-dest=PREV SRC/ NEW/Hard-linked snapshot
rsync -aHAX SRC/ host:DEST/Copy with metadata
rsync -avc --dry-run SRC/ DEST/Compare by content
tar -czf - DIR | ssh host "tar -xzf - -C D"Stream to another host

Flashcards

Create, list, extract with tar?
tar -czf A.tar.gz -C DIR NAME · tar -tzf A.tar.gz · tar -xzf A.tar.gz -C DEST
Which letters pick the compression?
z gzip · j bzip2 · J xz (no letter when extracting: tar detects it)
Why f last?
Because the next word is the archive name.
Keep ACLs, SELinux labels and extended attributes?
--acls --selinux --xattrs when creating and extracting
Exclude files; extract one member?
tar --exclude='*.tmp' -czf ... · tar -xzf A.tar.gz path/in/archive
Compare sizes of gzip, bzip2 and xz?
gzip fast and biggest, bzip2 smaller, xz smallest and slowest.
Zip and unzip?
zip -r A.zip DIR · unzip -l A.zip · unzip A.zip
Make and check checksums?
sha256sum FILES > SUMS · sha256sum -c SUMS
Incremental backup?
tar --listed-incremental=state.snar -czf incrN.tar.gz DIR; restore full then incrementals
Test a restore?
Extract into an empty directory; diff -r original restored
Stream a directory to another host?
tar -czf - DIR | ssh host "tar -xzf - -C DEST"
Mirror with metadata; preview?
rsync -aHAX SRC/ host:DEST/ · add -n to preview, -c to compare by checksum