Archiving is the process of fetching multiple files from the same or different locations and putting them into a single file bundle. It is generally done together with compression, or immediately followed by compression. This helps in streamlining the backup process, as discussed in the following section.
Compression
File data is generally compressed to save the disk space and reduce traffic, as well as the time to transmit files over a network. Linux has multiple utilities for compression; some of them are listed in the table that follows:
Command
Description
gzip
Most popular Linux compression utility
gunzip
Utility to decompressgzip
compressed files
bzip2
Another compression utility, with better compression thangzip
bunzip2
Utility to decompress.bzip2
compressed files
xz
The most space-efficient compression utility that exists in Linux
zip
Popular utility to decompress archives from other operating systems
These utilities have different algorithms for compression and thus different efficiency and resource consumption levels (generally, more efficient techniques take more time). Decompression time does not vary much across different methods.
gzip and gunzip compression
Thegzip
utility compresses files faster than any other utility.
The followingtablelists the gzip
compression commandandits description with some examples:
Command
Description
gzip *
Compresses all files in the current directory and each compressed file is renamed with the.gz
extension
gzip -r backup/
Compresses all files in thebackup/
directory and subdirectories
gunzip myfile.gz
or
gzip -d myfile.gz
Decompressesmyfile.gz
tomyfile
The examples of the gzip
and gunzip
commands are shown in the following screenshot:
bzip2 and bunzip2 compression
The syntax of the bzip2
command is similar togzip
, but it uses a differentcompressionalgorithm and creates a smaller-sized compressed file, at the price of more time taken for compression.
The followingtable lists the bzip2
compression command and its description with some examples:
Command
Description
bzip2 *
Compresses all files in the current directory and each compressed file is renamed with the .bz2
extension
bunzip2 *.bz2
or
bzip2 -d *.bz2
Decompresses all the files with the .bz2
extension in the current directory
The examples of the bzip2
and bunzip2
commands are shown in the following screenshot:
xz compression
This is the most space-efficientcompressionutility used in Linux. The trade-off for compression is a slower speed of compression for a higher compression ratio.
The following table lists the xz
compression command and its description with some examples:
Command
Description
xz *
Compresses all files in the current directory and each compressed file is renamed with the .xz
extension
xz myfile
Compresses themyfile
file tomyfile.xz
with the default compression level (6); deletes the originalmyfile
after compression
xz -dk
Decompressesmyfile.xz
tomyfile
and preservesmyfile.xz
after decompression
xz -d *.xz
orunxz
Decompresses all files with the extension.xz
in the current working directory
The examples of xz
command usage are shown in the following screenshot:
zip
This program is notgenerallyused to compress files in Linux, but it is quite often required to decompress archives from a Windows OS.
The following table lists the zip
command and its description with some examples:
Command
Description
zip backup *
Compresses all files in the present working directory and puts them insidebackup.zip
zip -r backup.zip /home/student/abc
Archives the files and directories stored in/home/student/abc
in the backup.zip
file
unzip backup.zip
Extracts all the files from thebackup.zip
in the current directory
The examples of the zip
and unzip
commands are shown in the following screenshot:
Archiving
In addition to compression, thetar
(tape archive) utility is very often used to group files into an archive known as atarballand then compress thewholearchive together. Creating a single file bundle by putting multiple files together is known as archiving.
The various options used with thetar
command are given, withtheirdescriptions, in the following table:
tar command option
Description
c
Creates an new archive
v
Verbosity, used to see which files are being added and extracted
f
Filename of the archive to operate on
x
Extracts an archive
t
Lists the contents of an archive
z
Uses .gzip
compression (.tar.gz
)
j
Uses .bzip2
compression (.tar.bz2
), better than.gzip
J
Uses .xz
compression (.tar.xz
), better than.bzip2
The tarball archives can be compressed using.gzip
,.bzip2
, or.xz
compression withtar
command itself.
The following table lists the usage of the tar
command with compression utility:
Command
Description
Tar cvf abc.tar file1 file2 file3
Archives thefile1
,file2
, andfile2
files and puts them into one single file, abc.tar
Tar xvf abc.tar
Extracts all the files in the abc.tar
archive in the current directory
Tar tvf abc.tar
Lists all the files available inside the abc.tar
archive
Tar cvzf abc.tar.gz *
Creates anabc.tar.gz
archive of all the files in the current directory and compresses it with.gzip
Tar cvjf abc.tar.bz2 *
Creates anabc.tar.bz2
archive of all the files in the current directory and compresses it with.bzip2
Tar cvJf abc.tar.xz *
Creates anabc.tar.xz
archive of all the files in the current directory and compresses it with.xz
Tar xvf abc.tar.gz abc.tar.bz2 abc.tar.xz
Extracts all the files inabc.tar.gz
,abc.tar.bz2
, andabc.tar.xz
in the current directory
Archiving with tar
In the following example, we cover creating and extracting archives:
Archiving and compression (.gzip) using tar
In the following example, we cover creating and extracting gunzip
compressed archives:
Archiving, compression (.bzip2), and listing contents using tar
In the following example, we cover creating and extracting .bzip2
compressed archives:
Archiving and compression (.xz) using tar
In the following example, we cover creating and extracting .xz
compressed archives: