Archives & compression
tar, gzip, xz, zstd and zip: create, list, extract and choose the right format.
Backing up a website, shipping logs to a colleague, downloading a source release, deploying a build: all involve archives (many files bundled into one) and compression (making them smaller). On Linux these are usually two separate tools working together: tar bundles, and gzip, xz or zstd compress. You'll also meet zip for sharing with Windows users.
Practice project:
tar: the tape archiver#
tar was designed for tape drives, hence the name, and its one-letter options read like a sentence:
Create
Mnemonic: create zipped file. The archive name comes right after f, then what to put in it. tar -czf project project.tar.gz (swapped) tries to archive a file called project.tar.gz into an archive called project: an error, or worse, an overwrite.
List
Add v (tar -tvzf) for permissions, owners, sizes and dates, like ls -l.
Extract
Modern GNU tar detects the compression itself when extracting, so tar -xf anything.tar.xz works too. Extract a single file by naming it, or print it to stdout with -O:
Exclude files
Typical exclusions: --exclude=node_modules --exclude=.git --exclude='*.log'.
Choosing a compressor#
Same folder, different compressors (sizes in bytes):
(Columns trimmed to size and name. A file of sequential numbers compresses unusually well; real ratios depend on your data, and already-compressed files like JPEG, MP4 or ZIP barely shrink.)
zstd may need sudo apt install zstd (Fedora: sudo dnf install zstd).
Compressing single files#
gzip, xz and zstd also work on their own, one file at a time:
The z family reads compressed files directly: zcat, zless, zgrep "ERROR" app.log.gz. That's how you search rotated logs like /var/log/syslog.2.gz. For xz, xz/unxz/xzcat; for zstd, zstd/unzstd/zstdcat (zstd keeps the original by default).
zip and unzip#
For archives that Windows and macOS users can open with a double-click:
zip compresses each file separately and doesn't preserve Linux permissions and owners as faithfully as tar, so use tar for Linux-to-Linux backups and deployments. zip -e adds a password, but its default encryption is weak; use gpg or 7z for real secrecy.
Real-world recipes#
- as the file name means stdin/stdout, which is what makes the streaming tricks work. -C /var/www mysite stores paths as mysite/... rather than var/www/mysite/.... GNU tar strips the leading / from absolute paths (with a warning), so restoring an archive never overwrites system files unless you extract with -C /.
Safety when extracting#
- List first (
tar -tf,unzip -l). Well-behaved archives contain one top-level folder; badly made ones dump hundreds of files into your current directory (a "tarbomb"). - Extract unknown archives into an empty directory:
mkdir tmp && tar -xf thing.tar.gz -C tmp. - Don't extract archives from untrusted sources as root: tar preserves ownership and permissions for root, and paths containing
../could write outside the target.
Common mistakes#
- Putting the archive name somewhere other than straight after
f. - Forgetting that
gzip filedeletes the original. - Compressing already-compressed media and expecting savings.
- Using
zipfor server backups and losing permissions. - Archiving a live database's files directly. Dump it first (
mysqldump,pg_dump) and archive the dump.
What's next#
Archives are one way to package things up. Next you'll customise the shell itself, with environment variables, PATH, aliases and .bashrc.
Check your understanding
Quick quiz
1.Which command creates a gzip-compressed archive
site.tar.gzof the foldersite/?2.You run
gzip notes.txt. What happens to the original file?3.Before extracting an archive you downloaded, what is a good habit?
Finished reading?
Mark this lesson complete to track your progress.