Guide Linux Beginner

Archiving & Compression Commands

tar, gzip, zip, and rsync — bundling, compressing, and transferring files and directories on Linux.

3 min read

tar: The Standard Archive Tool

tar (tape archive) bundles multiple files/directories into a single file — by itself it doesn’t compress; that’s usually layered on via gzip/bzip2/xz.

tar -cvf archive.tar directory/          # create an archive (c=create, v=verbose, f=file)
tar -xvf archive.tar                       # extract an archive
tar -tvf archive.tar                         # list contents WITHOUT extracting

tar -czvf archive.tar.gz directory/            # create + gzip compress (z = gzip)
tar -xzvf archive.tar.gz                         # extract a .tar.gz

tar -cjvf archive.tar.bz2 directory/               # create + bzip2 compress (j = bzip2, better ratio, slower)
tar -cJvf archive.tar.xz directory/                  # create + xz compress (J = xz, best ratio, slowest)

tar -xzvf archive.tar.gz -C /target/directory/         # extract into a specific directory
tar -czvf archive.tar.gz --exclude="*.log" directory/    # exclude matching files

Compression Utilities: gzip / bzip2 / xz

ToolExtensionSpeedCompression Ratio
gzip.gzFastGood
bzip2.bz2SlowerBetter
xz.xzSlowestBest
gzip file.txt              # compresses to file.txt.gz, REMOVES the original
gzip -k file.txt              # keep the original file too
gunzip file.txt.gz               # decompress
zcat file.txt.gz                   # view compressed content without extracting
zgrep "error" logfile.gz              # grep directly inside a gzipped file

zip / unzip (Cross-Platform Friendly)

zip -r archive.zip directory/       # create a zip, recursively
unzip archive.zip                     # extract
unzip -l archive.zip                    # list contents without extracting
unzip archive.zip -d /target/             # extract to a specific directory

rsync: Efficient Sync & Backup

rsync transfers only the differences between source and destination, making it far more efficient than cp/scp for repeated syncs.

rsync -avh src/ dst/                     # archive mode, verbose, human-readable sizes
rsync -avh --delete src/ dst/               # mirror EXACTLY — deletes files in dst/ not present in src/
rsync -avz src/ user@remote:/path/            # sync to a remote host over SSH, with compression
rsync -avh --dry-run src/ dst/                  # preview what WOULD be transferred, no changes made
rsync -avh --exclude="*.log" src/ dst/            # exclude matching files
FlagMeaning
-aArchive mode: recursive + preserves permissions, timestamps, symlinks
-vVerbose
-zCompress data during transfer (useful over slow networks)
-hHuman-readable sizes
--deleteMake destination an exact mirror of source
--dry-runShow what would happen without doing it

Production Considerations

  • Always run rsync --delete with --dry-run first when mirroring — it’s easy to point source/destination the wrong way and delete production data.
  • tar archives don’t preserve extended ACLs by default on all systems — verify permissions after restoring critical backups.
  • For log files, zgrep/zcat avoid the extra disk I/O of fully decompressing before searching — useful when investigating incidents against large rotated .gz logs.

Quick Interview Answer

tar bundles files into a single archive, and is almost always paired with a compressor — gzip (fast, .tar.gz), bzip2 (better ratio), or xz (best ratio, slowest). rsync is the go-to tool for syncing or backing up because it only transfers the differences between source and destination, and --dry-run lets you preview a sync — especially important before using the destructive --delete flag.”

Common Mistakes

  • Running rsync --delete in the wrong direction and wiping out the source instead of the destination.
  • Forgetting tar itself doesn’t compress — a plain .tar file is the same size as its contents combined.
  • Using cp -r for large repeated backups instead of rsync, which re-copies everything instead of just the changes.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form