Choosing a Compressor
gzip vs bzip2 vs xz vs zip
They all shrink files, trading speed against ratio (how small).
| Tool | tar flag | Speed | Ratio | Use when |
|---|---|---|---|---|
gzip | z | ⚡ Fast | Good | The safe default |
bzip2 | j | Slow | Better | Space matters more than time |
xz | J | Slowest | Best | Archiving big data long-term |
zip | - | Fast | Good | Sharing with Windows users |
$ tar -czf a.tar.gz dir/ # gzip (z)
$ tar -cJf a.tar.xz dir/ # xz (J) - smallest
$ zip -r a.zip dir/ # zip (cross-platform)
$ unzip a.zip
Standalone (on a single file): gzip file → file.gz, and gunzip file.gz to reverse.
Tip: Rule of thumb:gzipfor everyday (fast, universal),xzwhen you want it as small as possible and don't mind waiting,zipwhen a Windows user is on the other end.
Analogy: Speed vs ratio is like packing a bag: throw everything in fast (gzip), or fold meticulously so more fits but it takes longer (xz).