Cheatsheet#zip-tar
Zip & Tar Archiving Cheatsheet
A complete guide for file archiving and compression on Linux using tar, gzip, bzip2, xz, zstd, zip, unzip, and 7z.
Tar Archives (tar)
tar (Tape Archive) bundles multiple files and directories into a single archive file, optionally compressing it using various algorithms.
Essential Flags
-c: Create a new archive.-x: Extract files from an archive.-t: List contents of an archive without extracting.-v: Verbose (show files being processed).-f: Specify archive filename (MUST be the last flag before filename).-z: Compress withgzip(.tar.gz/.tgz).-j: Compress withbzip2(.tar.bz2).-J: Compress withxz(.tar.xz).--zstd: Compress withzstd(.tar.zst).-C: Extract to a specific directory.
Create Archives
# Create uncompressed tar archive (.tar)
tar -cvf archive.tar /path/to/directory
# Create Gzip compressed tar (.tar.gz / .tgz)
tar -czvf archive.tar.gz /path/to/directory
# Create Bzip2 compressed tar (.tar.bz2)
tar -cjvf archive.tar.bz2 /path/to/directory
# Create XZ compressed tar (.tar.xz) - High compression ratio
tar -cJvf archive.tar.xz /path/to/directory
# Create Zstd compressed tar (.tar.zst) - Very fast & modern
tar --zstd -cvf archive.tar.zst /path/to/directory
# Exclude specific folder or files from tar archive
tar -czvf archive.tar.gz --exclude='node_modules' --exclude='.git' /path/to/directoryExtract Archives
# Extract archive to current directory
tar -xzvf archive.tar.gz
# Extract archive to a target directory (/tmp/extracted)
tar -xzvf archive.tar.gz -C /tmp/extracted
# Extract a specific file from tar archive
tar -xzvf archive.tar.gz path/to/file.txtList Archive Contents
# View files inside tar archive without extracting
tar -tzvf archive.tar.gzZip & Unzip Commands
zip is the standard cross-platform compression format compatible with Windows, macOS, and Linux.
Compression
# Zip a single file
zip archive.zip file.txt
# Zip a directory recursively (-r)
zip -r archive.zip /path/to/directory
# Zip with maximum compression (-9)
zip -9 -r archive.zip /path/to/directory
# Create password-protected encrypted zip (-e)
zip -e -r secure_archive.zip /path/to/directoryExtraction
# Unzip archive to current directory
unzip archive.zip
# Unzip archive to a specific directory (-d)
unzip archive.zip -d /target/directory
# Test zip archive integrity (-t)
unzip -t archive.zip
# List contents of zip archive without extracting (-l)
unzip -l archive.zip7-Zip (7z)
7z offers high compression ratios and supports multi-part split archives.
# Install 7z (p7zip)
# Ubuntu/Debian: sudo apt install p7zip-full
# RHEL/Fedora: sudo dnf install p7zip p7zip-plugins
# Create 7z archive
7z a archive.7z /path/to/directory
# Create password-protected 7z archive
7z a -p -mhe=on secure_archive.7z /path/to/directory
# Extract 7z archive
7z x archive.7z -o/target/directory
# List contents of 7z archive
7z l archive.7zStandalone Compression Utilities (gzip, bzip2, xz, zstd)
Compress individual files directly (replaces original file with compressed .gz/.bz2/.xz file unless -k keep option is passed).
# Compress single file with Gzip (creates file.txt.gz)
gzip file.txt
# Decompress Gzip file (-d)
gzip -d file.txt.gz
gunzip file.txt.gz
# Compress file keeping original file (-k)
gzip -k file.txt
# Compress with Zstd (fastest decompression speed)
zstd file.txt
zstd -d file.txt.zst