Archives Compression
Archives & Compression — Quick Reference
Section titled “Archives & Compression — Quick Reference”tar — Pack / Unpack Archives
Section titled “tar — Pack / Unpack Archives”The most common tool. Often combined with gzip (.tar.gz / .tgz) or bzip2 (.tar.bz2).
Create Archives
Section titled “Create Archives”tar -czf archive.tar.gz ./dir # create gzip archivetar -cjf archive.tar.bz2 ./dir # create bzip2 archive (smaller, slower)tar -cf archive.tar ./dir # create uncompressed archivetar -czf archive.tar.gz file1 file2 # archive specific filesExtract Archives
Section titled “Extract Archives”tar -xzf archive.tar.gz # extract gzip archivetar -xjf archive.tar.bz2 # extract bzip2 archivetar -xf archive.tar # extract uncompressedtar -xzf archive.tar.gz -C /dest/ # extract to specific directoryInspect Without Extracting
Section titled “Inspect Without Extracting”tar -tzf archive.tar.gz # list contents of gzip archivetar -tf archive.tar # list contents of uncompressedCommon Flags
Section titled “Common Flags”| Flag | Meaning |
|---|---|
-c | Create archive |
-x | Extract archive |
-t | List contents |
-z | Use gzip compression |
-j | Use bzip2 compression |
-J | Use xz compression |
-f | Specify filename (always last) |
-v | Verbose — show files as processed |
-C | Change to directory before extracting |
zip / unzip
Section titled “zip / unzip”zip archive.zip file1 file2 # zip specific fileszip -r archive.zip ./dir # zip a directory recursivelyzip -r archive.zip . -x "*.git*" # exclude a pattern
unzip archive.zip # extract to current directoryunzip archive.zip -d /dest/ # extract to specific directoryunzip -l archive.zip # list contents without extractingunzip -o archive.zip # overwrite existing filesgzip / gunzip — Compress Single Files
Section titled “gzip / gunzip — Compress Single Files”Replaces the original file by default.
gzip file.txt # compress → file.txt.gz (removes original)gzip -k file.txt # keep originalgzip -d file.txt.gz # decompress (same as gunzip)gunzip file.txt.gz # decompress
gzip -9 file.txt # maximum compressiongzip -1 file.txt # fastest compression
# Compress multiple filesgzip file1.txt file2.txtxz — High Compression
Section titled “xz — High Compression”Better compression ratio than gzip, but slower.
xz file.txt # compress → file.txt.xzxz -d file.txt.xz # decompressxz -k file.txt # keep originalxz -9 file.txt # maximum compressionzcat / zless — Read Compressed Files Without Extracting
Section titled “zcat / zless — Read Compressed Files Without Extracting”zcat file.txt.gz # print contents of gzip filezcat file.txt.gz | grep "error" # grep inside compressed logzless file.txt.gz # page through gzip fileQuick Reference — File Extensions
Section titled “Quick Reference — File Extensions”| Extension | Tool | Command to extract |
|---|---|---|
.tar | tar | tar -xf file.tar |
.tar.gz / .tgz | tar + gzip | tar -xzf file.tar.gz |
.tar.bz2 | tar + bzip2 | tar -xjf file.tar.bz2 |
.tar.xz | tar + xz | tar -xJf file.tar.xz |
.gz | gzip | gunzip file.gz |
.zip | zip | unzip file.zip |
.xz | xz | xz -d file.xz |
tar -xzfis the one you’ll use 90% of the time.- Add
-vwhile learning to see what’s happening; drop it in scripts. - Use
tar -tzf archive.tar.gzbefore extracting to know where files will land — avoids spilling into your current directory. zcat logs.gz | grep ERROR | tail -100lets you search compressed logs without unpacking them.- For large files, prefer
xzfor storage andgzipwhen speed matters.