Archiving and Compressing: tar, gzip, zip
You will, regularly, need to bundle a folder into one file (to back it up, email it, scp it, or upload it). Linux has three relevant tools: tar bundles, gzip/xz/zstd compress, and zip does both in one cross-platform format.
tar: the standard Linux archive
tar bundles many files into one. It does not compress on its own — but it pairs with gzip/bzip2/xz/zstd to make compressed archives.
Create
# Create a gzipped tarball (.tar.gz or .tgz)
tar czvf backup.tar.gz ./project
# Create a bzip2 tarball (smaller, slower)
tar cjvf backup.tar.bz2 ./project
# Create an xz tarball (smallest, slowest)
tar cJvf backup.tar.xz ./project
# Create a zstd tarball (modern, fast + good ratio)
tar --zstd -cvf backup.tar.zst ./project
Extract
# Extract any of the above (tar auto-detects compression)
tar xvf backup.tar.gz
tar xvf backup.tar.bz2
tar xvf backup.tar.xz
# Extract to a specific directory
tar xvf backup.tar.gz -C /tmp/restore
# List contents WITHOUT extracting
tar tvf backup.tar.gz
# Extract just one file
tar xvf backup.tar.gz project/config.yaml
The flag mnemonic
c = create x = extract t = list
v = verbose f = file (always required)
z = gzip j = bzip2 J = xz
“czvf” creates verbosely, “xvf” extracts verbosely. Memorize those two.
gzip / gunzip: compress single files
gzip bigfile.log # creates bigfile.log.gz, removes original
gzip -k bigfile.log # keep original
gunzip bigfile.log.gz # decompress
zcat bigfile.log.gz # show contents without decompressing
zless bigfile.log.gz # page through compressed file
zip / unzip: cross-platform
Use zip when sending to Windows or Mac users — they handle .zip natively without extra tools.
zip -r project.zip ./project # zip a folder
zip secrets.zip file1 file2 # zip specific files
zip -e secret.zip file # encrypted (prompts for password)
unzip project.zip
unzip project.zip -d /tmp # extract to specific dir
unzip -l project.zip # list contents
Compare formats
| Format | Speed | Size | When to use |
|---|---|---|---|
.tar.gz |
Fast | OK | Default. Everyone has gzip. |
.tar.xz |
Slow | Smallest | Sharing big files (kernel sources). |
.tar.zst |
Very fast | Small | Modern systems. Best balance. |
.zip |
Fast | OK | Sending to Windows/Mac users. |
Daily one-liners
# Backup home directory excluding caches
tar czvf home-backup.tar.gz
--exclude='.cache' --exclude='node_modules'
/home/alice
# Tar+SSH: copy a directory tree to a remote server
tar cz ./project | ssh server "tar xz -C /opt"
# Show the size of an archive's contents
tar tvf big.tar.gz | awk '{sum+=$3} END {print sum/1024/1024 " MB"}'
Common mistakes
- Forgetting
fintar— withoutf filenameit tries to read/write to a tape drive. - Extracting an archive that wasn’t built with a top-level folder — files spill into your current directory. Use
tar tvfto check first. - Encrypting zip with
-e— the encryption is weak. Use GPG or 7zip’s AES for real security.
What to learn next
Once files are bundled, the next concern is moving them around — between directories, between machines. cp, mv, and rsync are the next stop.