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. |
|
|
Compressing with -z option
By adding the z option, the tar command compresses the file with the gzip algorithm:
|
|
Compressing with gzip
Since gzip will delete the original file after compression, we need to clone a new file:
|
|
|
|
Compressing with bzip2
An upgraded version of gzip.
Since bzip2 will delete the original file after compression, we need to clone a new file:
|
|
|
|
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:
|
|
|
|
Unpackaging and Decompressing Files
Unpackaging with tar
|
|
Decompressing with gunzip
|
|
Decompressing with bunzip2
|
|
Decompressing with unxz
|
|
View Packed Files and Compressed Files Contents
Let’s prepare some text files that can be viewed in a compressed file:
|
|
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:
|
|
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:
|
|
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:
|
|
Viewing with zcat, zmore, bzless, zgrep
|
|
zless will be another option.
Viewing with bzcat, bzmore, bzless, bzgrep
|
|
bzless will be another option.
Viewing with xzcat, xzmore, xzless, xzgrep
|
|
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