Master Linux File Compression: Zip, Rar, and Tar Commands Explained
This guide walks through the most common Linux compression and archiving tools—zip, unzip, rar, unrar, and tar—detailing file formats, command syntax, options for compression levels, encryption, and extraction, plus practical examples for each utility.
Introduction
Linux provides a variety of command‑line tools for creating and extracting compressed archives. The most frequently used formats are .zip, .rar, .7z, .tar, .gz, .xz, .bz2 and their combined variants such as .tar.gz or .tar.xz.
Common Archive Extensions
*.zip – created by the zip program
*.rar – created by the rar program
*.7z – created by the 7‑Zip program
*.tar – uncompressed tar archive
*.gz – gzip‑compressed file
*.xz – xz‑compressed file
*.bz2 – bzip2‑compressed file
*.tar.gz – tar archive compressed with gzip
*.tar.xz – tar archive compressed with xz
*.tar.bz2 – tar archive compressed with bzip2
*.tar.7z – tar archive compressed with 7‑Zip
1. Using zip to Create Archives
Basic packaging of a directory (recursive, quiet, output file): zip -r -q -o test.zip /home/test Check size with du -h test.zip and file type with file test.zip Set compression level (‑9 highest, ‑1 fastest) and exclude existing zip files:
zip -r -9 -q -o test_9.zip /home/test -x ~/*.zip
zip -r -1 -q -o test_1.zip /home/test -x ~/*.zipCreate an encrypted archive with -e: zip -r -q -e test.zip /home/test Convert line endings for Windows compatibility with -l (LF → CR+LF):
zip -r -l -o test.zip /home/test2. Extracting with unzip
Extract to current directory: unzip test.zip Quiet extraction to a specific folder: unzip -q test.zip -d ziptest List archive contents without extracting: unzip -l test.zip Force UTF‑8 to GBK conversion for Chinese filenames:
unzip -O GBK 中文压缩文件.zip3. Working with rar and unrar
Install tools (Debian/Ubuntu):
sudo apt-get update
sudo apt-get install rar unrarCreate a rar archive (add current directory): rar a test.rar . Delete a file from the archive: rar d test.rar .bashrc List archive contents: rar l test.rar Extract with full paths: unrar x test.rar Extract without preserving paths:
mkdir tmp
unrar e test.rar tmp/4. Using tar for Packing and Compression
Create a plain tar archive (‑c create, ‑f file): tar -cf test.tar ~ Extract to a specific directory (‑x extract, ‑C target):
mkdir tardir
tar -xf test.tar -C tardirList contents without extracting (‑t): tar -tf test.tar Preserve permissions and follow symlinks (‑p, ‑h): tar -cphf etc.tar /etc Compress with gzip (‑z) to produce .tar.gz: tar -czf etc.tar.gz ~ Extract a .tar.gz archive (‑x, ‑z):
tar -xzf etc.tar.gz5. Quick Reference for Compression Parameters
| Archive format | tar option |
|---------------|-----------|
| *.tar.gz | -z |
| *.tar.xz | -J |
| *.tar.bz2 | -j |Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
