Normally when backing data in a computer locally or to the cloud, there has been a struggle to get the most data into the most smallest space, whether that space be memory, storage devices, internet or network bandwidth. This is achieved through data compression techniques and tools like tar
, gzip
, bzip2
and xz
.
In this guide, we are going to learn how to compress, uncompress and install tar, gzip, bzip2 and xz files on Linux systems i.e Ubuntu, CentOS, Debian, Arch distributions.
File Compression and Uncompression on Linux.
This is accomplished through the following data compression techniques;
- tar
- gzip
- bzip2
- xz
1. tar
The tar
command, acronym Tape archive is used to create archive files. A tar archive can consist of separate files, one or more directory hierarchies, or a mixture of both. Tar files have extension .tar
2. gzip
The gzip
program is used to compress one or more files. When executed it replaces the original file with a compressed version of the original. A file compressed with gzip have the extension .gz
3. bzip2
The bzip2
command is similar to gzip but uses a different compression algorithm that archives a higher level of compression at the cost of compression speed. A file compressed with bzip2 have the extension .bz2
4. xz
The xz
is a general-purpose data compression tool with command line syntax similar to gzip and bzip2. It compresses or decompresses each file according to the selected operation mode. A file compressed with xz have the extension .xz
Compress/Archive files using tar, gzip, bzip2 and xz
Using tar command
The tar
command has four main operation utilities;
c:
Create a new archive
x:
Extract files from an archive
t:
List the contents of an archive
v:
Verbosely list files processed
Creating a New Archive
Using ls
command, let’s check the contents of Entertainment directory;
$ ls Entertainment/
alan.m4a 'list of songs' Music Videos
Now, let’s create a new Archive file of Entertainment directory called Backup.tar
$ tar -cvf Backup.tar Entertainment/
Entertainment/
Entertainment/Music/
Entertainment/Music/walker.m4a
Entertainment/alan.m4a
Entertainment/list of songs
Entertainment/Videos/
Entertainment/Videos/love.mp4
Using ls -lh
command, check the created archive above
$ ls -lh
total 13M
-rw-r--r-- 1 hero hero 13M Jul 9 20:50 Backup.tar
drwxr-xr-x 4 hero hero 4.0K Jul 9 20:36 Entertainment
Using -t
option, list the contents of Backup.tar archive file.
$ tar -tf Backup.tar
Entertainment/
Entertainment/Music/
Entertainment/Music/walker.m4a
Entertainment/alan.m4a
Entertainment/list of songs
Entertainment/Videos/
Entertainment/Videos/love.mp4
Using gzip command
Using gzip
command, we are going to compress Backup.tar file.
gzip Backup.tar
Now,let’s check the compressed file above using ls -lh
command;
$ ls -lh
total 12M
-rw-r--r-- 1 hero hero 12M Jul 9 20:50 Backup.tar.gz
We find that, after compressing Backup.tar file we obtain the output Backup.tar.gz with the reduced file size.
Using -k
option with gzip
to Keep (don’t delete) input files during compression;
gzip -k Backup.tar
Check if the input file (Backup.tar) is kept with ls -lh
command during compression;
$ ls -lh
total 16K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
-rw-rw-r-- 1 frank frank 56 Apr 10 11:20 Backup.tar.gz
Using -l
option with gzip
to list the following fields:
compressed size:
size of the compressed fileuncompressed size:
size of the uncompressed fileratio:
compression ratio (0.0% if unknown)uncompressed_name:
name of the uncompressed file
$ gzip -l Backup.tar.gz
compressed uncompressed ratio uncompressed_name
56 10240 99.7% Backup.tar
Using -v
option with gzip
to display the name and percentage reduction for each file compressed;
$ gzip -v Backup.tar
Backup.tar: 99.7% -- replaced with Backup.tar.gz
Using bzip2 command
Using bzip2
command, we are going to compress Backup.tar file.
bzip2 Backup.tar
Now,let’s check the compressed file above using ls -lh
command;
$ ls -lh
total 12M
-rw-r--r-- 1 hero hero 12M Jul 9 20:50 Backup.tar.bz2
After compressing Backup.tar file the output is Backup.tar.bz2 with the reduced file size.
Using -k
option with bzip2
to Keep (don’t delete) input files during compression;
bzip2 -k Backup.tar
Check if the input file (Backup.tar) is kept with ls -lh
command during compression;
$ ls -lh
total 16K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
-rw-rw-r-- 1 frank frank 46 Apr 10 11:20 Backup.tar.bz2
Using -v
option with bzip2
to display the name and percentage reduction for each file compressed;
$ bzip2 -v Backup.tar
Backup.tar: 222.609:1, 0.036 bits/byte, 99.55% saved, 10240 in, 46 out.
Using xz command
Using xz
command, we are going to compress Backup.tar file.
xz Backup.tar
Now,let’s check the compressed file above using ls -lh
command;
$ ls -lh
total 4.0K
-rw-rw-r-- 1 frank frank 108 Apr 10 11:20 Backup.tar.xz
After compressing Backup.tar file the output is Backup.tar.xz with the reduced file size.
Using -k
option with xz
to Keep (don’t delete) input files during compression;
xz -k Backup.tar
Check if the input file (Backup.tar) is kept with ls -lh
command during compression;
$ ls -lh
total 8.0K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
-rw-rw-r-- 1 frank frank 108 Apr 10 11:20 Backup.tar.xz
Using -v
option with xz
to display the name and percentage reduction for each file compressed;
$ xz -v Backup.tar
Backup.tar (1/1)
100 % 108 B / 10.0 KiB = 0.011
Unpack/Uncompress files using tar, gunzip, bunzip2 and unxz
Using tar command
Now, let’s unpack the archived file Backup.tar using tar
command;
$ tar -xvf backup.tar
Entertainment/
Entertainment/Music/
Entertainment/Music/walker.m4a
Entertainment/alan.m4a
Entertainment/list of songs
Entertainment/Videos/
Entertainment/Videos/love.mp4
Use ls -lh
command to list the contents of the current directory where we unpack Backup.tar archive file.
$ ls -lh
total 13M
-rw-r--r-- 1 hero hero 13M Jul 9 20:50 Backup.tar
drwxr-xr-x 4 hero hero 4.0K Jul 9 20:36 Entertainment
After unpacking Backup.tar archive file, we obtain our original directory Entertainment.
Using gunzip command
To Uncompress a gzip
file we use gunzip
program or gzip
with -d
option i.e gzip -d Backup.tar.gz
command. We are going to uncompress Backup.tar.gz file.
Using gunzip
command;
gunzip Backup.tar.gz
Using gzip -d Backup.tar.gz
command;
gzip -d Backup.tar.gz
Now, using ls -lh
command let’s check the uncompressed file;
$ ls -lh
total 13M
-rw-r--r-- 1 hero hero 13M Jul 9 20:50 Backup.tar
So after uncompressing Backup.tar.gz file, we obtain Backup.tar file.
Using -k
option with gunzip
to Keep (don’t delete) input files during decompression;
gunzip -k Backup.tar.gz
Check if the input file (Backup.tar.gz) is kept with ls -lh
command during decompression;
$ ls -lh
total 16K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
-rw-rw-r-- 1 frank frank 56 Apr 10 11:20 Backup.tar.gz
Using -v
option with gunzip
to display the name and percentage reduction for each file decompressed;
$ gunzip -v Backup.tar.gz
Backup.tar.gz: 99.7% -- replaced with Backup.tar
Using bunzip2 command
To Uncompress a bzip2
file we use bunzip2
program or bzip2
with -d
option i.e bzip2 -d Backup.tar.bz2
command. We are going to uncompress Backup.tar.bz2 file.
Using bunzip2
command;
bunzip2 Backup.tar.bz2
Using bzip2 -d Backup.tar.bz2
command;
bzip2 -d Backup.tar.bz2
Now, using ls -lh
command let’s check the uncompressed file;
$ ls -lh
total 13M
-rw-r--r-- 1 hero hero 13M Jul 9 20:50 Backup.tar
We find that after uncompressing Backup.tar.bz2 file, we obtain Backup.tar.
Using -k
option with bunzip2
to Keep (don’t delete) input files during decompression;
bunzip2 -k Backup.tar.bz2
Check if the input file (Backup.tar.bz2) is kept with ls -lh
command during decompression;
$ ls -lh
total 16K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
-rw-rw-r-- 1 frank frank 46 Apr 10 11:20 Backup.tar.bz2
Using -v
option with bunzip2
to display the name and percentage reduction for each file decompressed;
$ bunzip2 -v Backup.tar.bz2
Backup.tar.bz2: done
Using unxz command
To Uncompress a xz
file we use unxz
program or xz
with -d
option i.e xz -d Backup.tar.xz
command. We are going to uncompress Backup.tar.xz file.
Using unxz
command;
unxz Backup.tar.xz
Using xz -d Backup.tar.xz
command;
xz -d Backup.tar.xz
Now, using ls -lh
command let’s check the uncompressed file;
$ ls -lh
total 4.0K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
We find that after uncompressing Backup.tar.xz file, we obtain Backup.tar.
Using -k
option with unxz
to Keep (don’t delete) input files during decompression;
unxz -k Backup.tar.xz
Check if the input file (Backup.tar.xz) is kept with ls -lh
command during decompression;
$ ls -lh
total 8.0K
-rw-rw-r-- 1 frank frank 10K Apr 10 11:20 Backup.tar
-rw-rw-r-- 1 frank frank 108 Apr 10 11:20 Backup.tar.xz
Using -v
option with unxz
to display the name and percentage reduction for each file decompressed;
$ unxz -v Backup.tar.xz
Backup.tar.xz (1/1)
100 % 108 B / 10.0 KiB = 0.011
Building application from tar, gzip, bzip2 and xz package in Linux
For tar.gz
package file, open your terminal using CTRL+ALT+T
keys and navigate to the directory where the package file is located. Once you are in that directory, extract the package file using the following commands;
tar -xzvf package.tar.gz
gunzip package.tar.gz
For tar.bz2
package file, use the following commands to extract;
tar -xjvf package.tar.bz2
bunzip package.tar.bz2
For tar.xz
package file, use the following commands to extract;
tar -xJvf package.tar.xz
unxz package.tar.xz
Once you have extracted the package file navigate to that folder i.e cd package
read the file INSTALL
and/or README
to know if you need some dependencies.
Type the following commands to install the package file;
./configure
make
sudo make install
There we go, the package will install successfully.
Conclusion
The above guide summaries the use of tar
, gzip
, bzip2
and xz
GNU/Linux programs to Compress and Uncompress and Install files. For more useful examples refer to their man
pages. i.e
man tar
man gzip
man bzip2
man xz
You can also check: