Working with compressed files is essential for saving disk space and transferring data efficiently. This chapter covers the most common archiving and compression tools in Linux.
1. tar
– Tape Archive (Basic Archiving)
The standard tool for combining multiple files into one archive (.tar
).
Create an Archive
# -c = create, -v = verbose, -f = filename
tar -cvf archive_name.tar file1 file2 dir1/
Extract an Archive
# -x = extract
tar -xvf archive_name.tar
List Contents
# -t = list
tar -tvf archive_name.tar
2. gzip
/gunzip
– Compression (.gz
)
Compresses files (but doesn’t bundle multiple files like tar
).
Compress a File
# Creates file.txt.gz
gzip file.txt
Decompress
# Restores file.txt
gunzip file.txt.gz
tar
+ gzip
Together (.tar.gz
)
# Create compressed archive
tar -czvf archive.tar.gz files/
# Extract
tar -xzvf archive.tar.gz
3. zip
/unzip
– Cross-Platform Compression (.zip
)
Commonly used for Windows-compatible archives.
Create a ZIP Archive
# Include files/dirs
zip archive.zip file1.txt dir1/
Extract ZIP
unzip archive.zip
List Contents
unzip -l archive.zip
4. xz
– High-Ratio Compression (.xz
)
Slower but better compression than gzip
.
Compress a File
# Creates file.txt.xz
xz file.txt
Decompress
# Or: xz -d file.txt.xz
unxz file.txt.xz
tar
+ xz
(.tar.xz
)
# Create
tar -cJvf archive.tar.xz files/
# Extract
tar -xJvf archive.tar.xz
-J
Compress using xz
. xz
is a compression format and a compression tool, similar to gzip
or bzip2
, but with better compression ratios (i.e., smaller file sizes), especially for large files.
5. 7z
– Ultra-High Compression (.7z
)
Even better compression (requires p7zip
package).
Install (if missing)
# Debian/Ubuntu
sudo apt install p7zip-full
# RHEL/CentOS
sudo yum install p7zip
Create Archive
# a = add
7z a archive.7z file1 dir1/
Extract
# a = add
7z a archive.7z file1 dir1/
Compression Types and features
Format | Use Case | Compression | Platform Notes |
---|---|---|---|
.tar | Uncompressed bundling | None | Native to Unix/Linux |
.tar.gz | Best balance (speed + compression) | Moderate | Very common, fast |
.zip | Sharing with Windows users | Moderate | Cross-platform, widely supported |
.tar.xz | Maximum compression (slow) | High | Smaller size, slower to create |
.7z | Extreme compression | Very High | Requires p7zip or 7-Zip |
Note that .tar
is just a container it is not compressing the contents.
Summary Cheat Sheet
Command | Action / Output |
---|---|
tar -cvf file.tar files/ | Create .tar (uncompressed) |
tar -xvf file.tar | Extract .tar |
gzip file | Create .gz |
gunzip file.gz | Extract .gz |
tar -czvf file.tar.gz files/ | Create .tar.gz (gzip) |
tar -xzvf file.tar.gz | Extract .tar.gz |
xz file | Create .xz |
unxz file.xz | Extract .xz |
tar -cJvf file.tar.xz files/ | Create .tar.xz (xz) |
tar -xJvf file.tar.xz | Extract .tar.xz |
zip archive.zip files/ | Create .zip |
unzip archive.zip | Extract .zip |
7z a archive.7z files/ | Create .7z (7-Zip) |
7z x archive.7z | Extract .7z |
Leave a Reply