Using Compression in Linux
Compression is a common task in Linux, used to reduce the size of files and directories for storage or transfer. There are several compression tools available, each with its own strengths. Below, we'll cover some of the most commonly used tools: gzip, bzip2, and xz. We'll also mention why Windows-style zip tools are less commonly used in Linux.Gzip
gzip is a widely used compression tool in Linux. It offers a good balance between compression speed and efficiency.Compressing a file:
Code:
gzip filename
Decompressing a file:
Code:
gunzip filename.gz
Bzip2
bzip2 provides better compression ratios than gzip, but it is slower.Compressing a file:
Code:
bzip2 filename
Decompressing a file:
Code:
bunzip2 filename.bz2
Xz
xz offers high compression ratios and is often used for distributing software packages.Compressing a file:
Code:
xz filename
Decompressing a file:
Code:
unxz filename.xz
Tar
tar is often used in combination with compression tools to archive multiple files and directories while preserving file permissions and ownership.Creating a tar.gz archive:
Code:
tar -czvf archive.tar.gz directory/
Extracting a tar.gz archive:
Code:
tar -xzvf archive.tar.gz
Creating a tar.bz2 archive:
Code:
tar -cjvf archive.tar.bz2 directory/
Extracting a tar.bz2 archive:
Code:
tar -xjvf archive.tar.bz2
Creating a tar.xz archive:
Code:
tar -cJvf archive.tar.xz directory/
Extracting a tar.xz archive:
Code:
tar -xJvf archive.tar.xz
Zip
While Windows-style zip tools can be installed on Linux, they are not commonly used because they do not preserve file permissions and ownership. However, they can still be useful for compatibility with Windows systems.Compressing a file:
Code:
zip archive.zip filename
Decompressing a file:
Code:
unzip archive.zip