Managing archives and compressed files

May 19, 2021

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 decompress gzip compressed files

bzip2

Another compression utility, with better compression than gzip

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

The gzip utility compresses files faster than any other utility.

The following table lists the gzip compression command and its 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 the backup/ directory and subdirectories

gunzip myfile.gz or

gzip -d myfile.gz

Decompresses myfile.gz to myfile

The examples of the gzip and gunzip commands are shown in the following screenshot:

Compression

bzip2 and bunzip2 compression

The syntax of the bzip2 command is similar to gzip, but it uses a different compression algorithm and creates a smaller-sized compressed file, at the price of more time taken for compression.

The following table 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:

Compression

xz compression

This is the most space-efficient compression utility 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 the myfile file tomyfile.xzwith the default compression level (6); deletes the originalmyfileafter compression

xz -dk

Decompresses myfile.xz to myfile and preserves myfile.xz after decompression

xz -d *.xz

or unxz

Decompresses all files with the extension .xz in the current working directory

The examples of xz command usage are shown in the following screenshot:

Compression

zip

This program is not generally used 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 inside backup.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 the backup.zip in the current directory

The examples of the zip and unzip commands are shown in the following screenshot:

Compression

Archiving 

In addition to compression, the tar (tape archive) utility is very often used to group files into an archive known as a tarball and then compress the whole archive together. Creating a single file bundle by putting multiple files together is known as archiving.

The various options used with the tar command are given, with their descriptions, 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 with tar 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 the file1, file2, and file2 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 an abc.tar.gz archive of all the files in the current directory and compresses it with.gzip

Tar cvjf abc.tar.bz2 *

Creates an abc.tar.bz2 archive of all the files in the current directory and compresses it with.bzip2

Tar cvJf abc.tar.xz *

Creates an abc.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 in abc.tar.gz, abc.tar.bz2, and abc.tar.xz in the current directory

Archiving with tar 

In the following example, we cover creating and extracting archives:

Compression
Archiving and compression (.gzip) using tar 

In the following example, we cover creating and extracting gunzip compressed archives:

Compression
Archiving, compression (.bzip2), and listing contents using tar 

In the following example, we cover creating and extracting .bzip2 compressed archives:

Compression
Archiving and compression (.xz) using tar 

In the following example, we cover creating and extracting .xz compressed archives:

Compression

Related Articles

How to Install MongoDB on CentOS 8 Linux

How to Install MongoDB on CentOS 8 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB, which can handle big data. As a MongoDB is a NoSQL database, It stores data in JSON-like documents where fields can...

read more
How to Install OpenCV on CentOS 8 Linux

How to Install OpenCV on CentOS 8 Linux

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

seven + 14 =