File Compression
Compression utilities overview and usage
Section titled “Compression utilities overview and usage”0. Specs
Section titled “0. Specs”0.1. The What
Section titled “0.1. The What”When sharing files with other people, you may need to package and compress them.
There are very good tools for them:
- gzip
- bzip2
- xz
- tar
- zip
0.2. Abstract
Section titled “0.2. Abstract”Package and compress the contents of a directory (including subdirs):
tar -czvf archive.tar.gz /tmp/testdirDecompress and unpackage the archive:
tar -xzvf archive.tar.gz0.3. Create Test Environment
Section titled “0.3. Create Test Environment”All commands are tested on Debian 12 & 13 and Ubuntu Server 22.04 & 24.04 LTS
Create a temporary test directory
mkdir /tmp/testdirCreate 3 more directories d1, d2, d3
mkdir /tmp/testdir/d{1..3}Create 3 files at each directory
touch /tmp/testdir/d1/f1{1..3}touch /tmp/testdir/d2/f2{1..3}touch /tmp/testdir/d3/f3{1..3}Fill the files with random data with the given size
< /dev/urandom tr -dc "[:space:][:print:]" | head -c1M > /tmp/testdir/d1/f11< /dev/urandom tr -dc "[:space:][:print:]" | head -c2M > /tmp/testdir/d1/f12< /dev/urandom tr -dc "[:space:][:print:]" | head -c3M > /tmp/testdir/d1/f13< /dev/urandom tr -dc "[:space:][:print:]" | head -c4M > /tmp/testdir/d2/f21< /dev/urandom tr -dc "[:space:][:print:]" | head -c5M > /tmp/testdir/d2/f22< /dev/urandom tr -dc "[:space:][:print:]" | head -c6M > /tmp/testdir/d2/f23< /dev/urandom tr -dc "[:space:][:print:]" | head -c7M > /tmp/testdir/d3/f31< /dev/urandom tr -dc "[:space:][:print:]" | head -c8M > /tmp/testdir/d3/f32< /dev/urandom tr -dc "[:space:][:print:]" | head -c9M > /tmp/testdir/d3/f33Final tree
├── d1│ ├── f11│ ├── f12│ └── f13├── d2│ ├── f21│ ├── f22│ └── f23└── d3 ├── f31 ├── f32 └── f330.4. Sources
Section titled “0.4. Sources”- Man pages, the commands with —help option
- linuxconfig.org
- Deepseek
- ChatGPT
1. gzip
Section titled “1. gzip”1.1. Info
Section titled “1.1. Info”Installation (Most probably, it is already installed).
sudo apt updatesudo apt install gzipgzip compresses (and decompresses) only 1 file, it is not used to prepare a package of files. But it can be combined with tar to make a compressed package of files.
1.2. Usage
Section titled “1.2. Usage”Compress a file
(Original file is removed, a new file is created there with .gz extension)
gzip /tmp/testdir/d1/f11Decompress a file
(.gz file is removed, a new file is created there without .gz extension)
gzip -d /tmp/testdir/d1/f11.gzgunzip /tmp/testdir/d1/f11.gzCompress a file, keep the original file
gzip -k /tmp/testdir/d1/f11Decompress a file, keep the .gz file
gzip -kd /tmp/testdir/d1/f11.gzCompress all files in a directory
(All files are removed, new files are created there with .gz extensions)
gzip /tmp/testdir/d1/*Decompress all files in a directory
gzip -d /tmp/testdir/d1/*gunzip /tmp/testdir/d1/*Compress all files recursively (including subdirectories)
gzip -r /tmp/testdirDecompress all files recursively (including subdirectories)
gzip -rd /tmp/testdirSome useful options:
- -l —list: List compressed file contents
- -q —quiet: Suppress all warnings
- -1 —fast: Compress faster
- -9 —best: Compress better
2. bzip2
Section titled “2. bzip2”2.1. Info
Section titled “2.1. Info”Installation (Most probably, it is already installed).
sudo apt updatesudo apt install bzip2bzip2 compresses (and decompresses) only 1 file, it is not used to prepare a package of files. But it can be combined with tar to make a compressed package of files.
Provides higher compression ratios compared to gzip, but may be slower.
2.2. Usage
Section titled “2.2. Usage”Compress a file
(Original file is removed, a new file is created there with .bz2 extension)
bzip2 /tmp/testdir/d1/f11Decompress a file
(.bz2 file is removed, a new file is created there without .bz2 extension)
bzip2 -d /tmp/testdir/d1/f11.bz2bunzip2 /tmp/testdir/d1/f11.bz2Compress a file, keep the original file
bzip2 -k /tmp/testdir/d1/f11Decompress a file, keep the .bz2 file
bzip2 -kd /tmp/testdir/d1/f11.bz2Compress all files in a directory
(All files are removed, new files are created there with .bz2 extensions)
bzip2 /tmp/testdir/d1/*Decompress all files in a directory
bzip2 -d /tmp/testdir/d1/*bunzip2 /tmp/testdir/d1/*Some useful options:
- -q —quiet: Suppress all warnings
- -1 —fast: Compress faster
- -9 —best: Compress better
3.1. Info
Section titled “3.1. Info”Installation (Most probably, it is already installed).
sudo apt updatesudo apt install xz-utilsxz compresses (and decompresses) only 1 file, it is not used to prepare a package of files. But it can be combined with tar to make a compressed package of files.
3.2. Usage
Section titled “3.2. Usage”Compress a file
(Original file is removed, a new file is created there with .xz extension)
xz /tmp/testdir/d1/f11Decompress a file
(.xz file is removed, a new file is created there without .xz extension)
xz -d /tmp/testdir/d1/f11.xzunxz /tmp/testdir/d1/f11.xzCompress a file, keep the original file
xz -k /tmp/testdir/d1/f11Decompress a file, keep the .xz file
xz -kd /tmp/testdir/d1/f11.xzCompress all files in a directory
(All files are removed, new files are created there with .xz extensions)
xz /tmp/testdir/d1/*Decompress all files in a directory
xz -d /tmp/testdir/d1/*unxz /tmp/testdir/d1/*Some useful options:
- -l —list: List compressed file contents
- -q —quiet: Suppress all warnings
- -0: Compress faster
- -9: Compress better
4. tar
Section titled “4. tar”4.1. Info
Section titled “4.1. Info”Installation (Most probably, it is already installed)
sudo apt updatesudo apt install tartar actually is an archiving utility, it is used to package files. It can package many files to a file. It can also transparently be combined with gzip bzip2 and xz tools to compress files.
4.2. Usage With Compression
Section titled “4.2. Usage With Compression”Package and compress (with gzip) the contents of a directory (including subdirectories).
Archive file is created in the current directory.
tar -czvf archive.tar.gz /tmp/testdirDecompress and unpackage the archive
Creates the directory structure under current directory
tar -xzvf archive.tar.gz(package) compress & decompress (unpackage) with bzip2
tar -cjvf archive.tar.bz2 /tmp/testdirtar -xjvf archive.tar.bz2(package) compress & decompress (unpackage) with xz
tar -cJvf archive.tar.xz /tmp/testdirtar -xJvf archive.tar.xzSee the contents of an archive
tar -tvf archive.tar.gztar -tvf archive.tar.bz2tar -tvf archive.tar.xzExtract files to another directory
Directory must exist.
tar -xzvf archive.tar.gz -C /tmptar -xjvf archive.tar.bz2 -C /tmptar -xJvf archive.tar.xz -C /tmp4.3. Usage Without Compression (Archiving)
Section titled “4.3. Usage Without Compression (Archiving)”Tar can be used without compressing too, actually it is the main usage of the tar command.
All the conditions apply in 4.2.1., if you skip compression options (-z, -j, -J) then you get an archive without compression. This time you can add files to it, or remove files from it.
Archive a directory (and subdirectories)
tar -cvf archive.tar /tmp/testdirList the contents
tar -tvf archive.tarUnarchive it
tar -xvf archive.tarAdd a file to the archive
tar -rvf archive.tar test.txtRemove a file from the archive
tar -vf archive.tar --delete test.txt4.4. Options
Section titled “4.4. Options”Tar has tons of options. You can list them all by:
tar --helpSome selected options:
- -c: Create a new archive.
- -t: List the contents of the archive.
- -x: Extract files from the archive.
- -v: Verbose mode (show the progress and file names).
- -f: Specify the archive file name.
- -z: Use gzip compression.
- -j: Use bzip2 compression.
- -J: Use xz compression.
- -C: Change to the specified directory before extracting.
- -r: Append files to the end of an archive
- -a: use archive suffix to determine the compression program.
- —delete: delete from the archive
- —exclude=PATTERN: exclude files, given as a PATTERN
- —overwrite: overwrite existing files when extracting
- —remove-files: remove files after adding them to the archive
5. zip
Section titled “5. zip”5.1. Info
Section titled “5.1. Info”Although gzip, bzip2, xz, and tar is more than enough for compression and archiving; sometimes you may need to exchange files with the unlucky people using Wind*ws. zip tool may be helpful then.
Installation
sudo apt updatesudo apt install zip5.2. Usage
Section titled “5.2. Usage”Compress a file (Zip file is created in the current dir, original file stays there)
zip f11.zip /tmp/testdir/d1/f11Decompress a file (Decompress to the current dir by creating the directory structure in zip file, zip file stays where it is).
unzip f11.zipCompress all the files in a directory
zip d1.zip /tmp/testdir/d1/*Compress all the files in a directory including subdirectories
zip -r test.zip /tmp/testdir/Compress and encrypt
zip -er test.zip /tmp/testdir/List the contents of a zip file
unzip -l test.zipAdd new files to a zip file
zip -u d1.zip /tmp/testdir/d2/*Some options:
- -u: update; only changed or new files
- -d: delete entries in zipfile
- -m: move into zipfile (delete OS files)
- -r: recurse into directories
- -j: junk (don’t record) directory names
- -1: compress faster
- -9: compress better
- -q: quiet operation
- -v: verbose operation/print version info
- -x: exclude the following names
- -i: include only the following names
- -e: encrypt