devutils/ATFQ/book ATFQ git

Part III · Shell and commands

28.6 tar

alsotararchivetarballtar.gztgzuntarextract archive

TL;DR

  • c creates, x extracts, t lists. f names the archive file and v prints each file name.
  • z for gzip (.tar.gz), J for xz (.tar.xz), --zstd for zstd (.tar.zst). When extracting, GNU tar detects the compression itself.
tar czf backup.tar.gz ~/projects
tar tf backup.tar.gz | head              # look inside first
tar xf backup.tar.gz -C /tmp/restore     # extract into a folder

Pitfalls

Deep dive 2 questions

Why tar and gzip, and not zip?

tar comes from tape backups ("tape archive"): it writes files one after the other, with their owner, rights and links, into a single stream. Compression is a separate step applied to the whole stream. That compresses better than zip, which compresses each file on its own, but to get one file out, tar has to read through the archive up to it.

Which compression?

gzip is fast and available everywhere. xz makes the smallest files but is slow to create them. zstd comes close to xz at its higher levels and decompresses much faster; Arch Linux uses it for its packages.

Commands
tar
See also
28.7 rsync