41. Archiving - Compressing and Decompressing Files
Contents
By compressing files and directories into a compressed file, we can easily move and back up the compressed file.
Packaging and Compressing Files
Before we start selecting tools, we need to compare the pros and cons of each compression tool, so let’s do it. We should take into account the feelings of these people. We need to get their consent before we can interview them.
Let’s prepare a file:
|
|

Packaging with tar
The tar command is a standard compression and decompression tool.
The suffix of the tar file is .tar
Here are some options provided by the tar command:
Option | Meaning |
---|---|
-c or ‑‑create | Create a new archive. |
-z or ‑‑gzip | Filter the archive through gzip. |
-v or ‑‑verbose | Verbosely list files processed. |
-x or ‑‑extract or –get | extract files from an archive. |
-f or ‑‑file | Use archive file or device ARCHIVE, this option must be at the end. |
|
|

The archived file's size is greater than the origin
Compressing with -z option
By adding the z option, the tar command compresses the file with the gzip algorithm:
|
|

The archived file's size is way more less than the origin
Compressing with gzip
Since gzip will delete the original file after compression, we need to clone a new file:
|
|

Clone a new file called 100MBFileGZIP and list the files
|
|

Compress the file with gzip command
Compressing with bzip2
An upgraded version of gzip.
Since bzip2 will delete the original file after compression, we need to clone a new file:
|
|

Clone a new file called 100MBFileBZIP2 and list the files
|
|

Compress the file with bzip2 command
Compressing with xz
A tool with a high compression ratio.
Since xz will delete the original file after compression, we need to clone a new file:
|
|

Clone a new file called 100MBFileXZ and list the files
|
|

Compress the file with xz command
Unpackaging and Decompressing Files
Unpackaging with tar
|
|

Unpack the file with option x
Decompressing with gunzip
|
|

Decompress the file with gunzip command
Decompressing with bunzip2
|
|

Decompress the file with bunzip2 command
Decompressing with unxz
|
|

Decompress the file with unxz command
View Packed Files and Compressed Files Contents
Let’s prepare some text files that can be viewed in a compressed file:
|
|

Prepare three text files for the view commands provided by gzip, bzip2 and xz utility
Viewing Files with tar
List option for tar command:
Option | Meaning |
---|---|
-t or ‑‑list | list the contents of an archive. |
We can view the files packed in the .tar archive with option -t:
|
|

View compressed files in an .tar archive
Viewing Files with gzip
List option for gzip command:
Option | Meaning |
---|---|
-l or ‑‑list | List each compressed file. |
We can view the files compressed in the .gz archive with option -l or –list:
|
|

View compressed files in an .gz archive
Viewing Files with xz
List option for xz command:
Option | Meaning |
---|---|
-l or ‑‑list | Print information about compressed files. |
We can view the files compressed in the .xz archive with option -l or –list:
|
|

View compressed files in an .xz archive
Viewing with zcat, zmore, bzless, zgrep
|
|

View the .gz archive file's text content with zcat, zmore and zgrep commands
zless will be another option.
Viewing with bzcat, bzmore, bzless, bzgrep
|
|

View the .bz2 archive file's text content with bzcat, bzmore and bzgrep commands
bzless will be another option.
Viewing with xzcat, xzmore, xzless, xzgrep
|
|

View the .xz archive file's text content with xzcat, xzmore and xzgrep commands
xzless will be another option.
References 11.2 dd: Convert and copy a file, GNU tar: an archiver tool, GNU Gzip, bzip2 Documentation, XZ Utils
Author Dong Chen
LastMod Wed Mar 13 2019