Archiving & Compression Commands
tar, gzip, zip, and rsync — bundling, compressing, and transferring files and directories on Linux.
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
| Tool | Extension | Speed | Compression Ratio |
|---|---|---|---|
gzip | .gz | Fast | Good |
bzip2 | .bz2 | Slower | Better |
xz | .xz | Slowest | Best |
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
| Flag | Meaning |
|---|---|
-a | Archive mode: recursive + preserves permissions, timestamps, symlinks |
-v | Verbose |
-z | Compress data during transfer (useful over slow networks) |
-h | Human-readable sizes |
--delete | Make destination an exact mirror of source |
--dry-run | Show what would happen without doing it |
Production Considerations
- Always run
rsync --deletewith--dry-runfirst when mirroring — it’s easy to point source/destination the wrong way and delete production data. tararchives don’t preserve extended ACLs by default on all systems — verify permissions after restoring critical backups.- For log files,
zgrep/zcatavoid the extra disk I/O of fully decompressing before searching — useful when investigating incidents against large rotated.gzlogs.
Quick Interview Answer
“
tarbundles files into a single archive, and is almost always paired with a compressor —gzip(fast,.tar.gz),bzip2(better ratio), orxz(best ratio, slowest).rsyncis the go-to tool for syncing or backing up because it only transfers the differences between source and destination, and--dry-runlets you preview a sync — especially important before using the destructive--deleteflag.”
Common Mistakes
- Running
rsync --deletein the wrong direction and wiping out the source instead of the destination. - Forgetting
taritself doesn’t compress — a plain.tarfile is the same size as its contents combined. - Using
cp -rfor large repeated backups instead ofrsync, 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