Catalog My learning Blog
Linux Command Line Tutorial for Developers · Phase 2 · Lesson 12 · 3 min read

Linux tar, gzip and zip Commands

On this page
  1. Archiving vs compressing: tar vs gzip
  2. tar flags you'll use: create, extract, list
  3. gzip vs xz vs zstd, measured
  4. zip and unzip
  5. Common tar mistakes
  6. Check yourself

Archiving vs compressing: tar vs gzip

  • tar (tape archive) bundles many files and directories into one stream, keeping permissions, ownership and timestamps.
  • A compressor (gzip, xz, zstd, bzip2) shrinks a stream.

tar can call the compressor for you, producing .tar.gz, .tar.xz or .tar.zst files.

tar flags you'll use: create, extract, list

FlagMeaning
-cCreate an archive
-xExtract
-tList contents
-f fileArchive file name (must be followed by the name)
-z, -J, --zstdgzip, xz, zstd compression (-a picks by file extension)
-vVerbose
-C dirChange to a directory before extracting or adding
Shell
tar -czf myapp-1.4.2.tar.gz myapp-1.4.2/
tar -tzf myapp-1.4.2.tar.gz
Code
myapp-1.4.2/
myapp-1.4.2/data.txt
myapp-1.4.2/README.md
myapp-1.4.2/bin/
myapp-1.4.2/bin/myapp

Always list before extracting an archive you didn't make: you'll see whether it extracts into its own directory or scatters files into the current one (a "tarbomb"), and spot absolute paths or ../ entries.

Shell
mkdir out && tar -xzf myapp-1.4.2.tar.gz -C out
tar bundles files with their metadata into one stream, a compressor shrinks it, and extraction reverses both; listing first shows where files will land.
tar bundles files with their metadata into one stream, a compressor shrinks it, and extraction reverses both; listing first shows where files will land.

gzip vs xz vs zstd, measured

Compressing a 1.29 MB text file (the numbers 1 to 200,000, one per line) on the course VM:

Code
gzip  428,481 bytes   28 ms
xz     47,560 bytes  205 ms
zstd  107,311 bytes    8 ms
CompressorStrengthUse for
gzipUniversal, moderate ratio and speedCompatibility with anything
xzBest ratio, slowRelease downloads compressed once, downloaded often
zstdVery fast with a good ratio; tunable levelsBackups, logs, build artifacts, anything you compress often
bzip2Older; mostly supersededLegacy archives

Highly repetitive data like this compresses far better than typical files, so measure on your own data. Already-compressed formats (JPEG, MP4, zip files) barely shrink further.

zip and unzip

zip -r site.zip public/ and unzip site.zip are handy for sharing with Windows and macOS users or for formats that are zip inside (JAR files, many document formats). zip compresses each file separately, so listing and extracting single files is quick, but the overall ratio is usually lower than tar plus a compressor.

Common tar mistakes

  • tar -cvzf vs tar -cvfz: -f must be followed by the file name; putting z after f makes tar try to write to a file named z.
  • Absolute paths: archive with relative paths (tar -czf x.tar.gz -C /opt myapp) so extraction goes where you choose.
  • Permissions and owners: extracting as root restores the archived owners; as a normal user, files become yours. Use --same-owner or --no-same-owner deliberately.
  • Verify backups: list the archive (tar -tzf) and test-restore regularly; a backup you can't extract isn't one.

Check yourself

Listing with tar -tf shows where files will land, so you avoid scattering hundreds of files into the current directory, and lets you catch dangerous absolute or ../ paths before they overwrite something outside the target folder.

zstd, which is very fast at both compressing and decompressing and still compresses well, often better than gzip. xz compresses smaller but much more slowly.

-f takes the next word as the archive name, so tar tries to create an archive called z and treats backup.tar.gz as a file to add. Put f last: tar -czf backup.tar.gz dir/.

Use -C, as in tar -xzf backup.tar.gz -C /restore. tar changes into that directory before extracting, which must already exist, so files land there instead of in the current directory.

Windows and macOS open zip files natively without extra tools, so it's the best format for sharing with non-Linux users. tar archives remain better for Linux backups because they preserve permissions and ownership.