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:

1
dd if=/dev/zero of=100MBFile bs=102400 count=1
img

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.
1
2
3
# Create a general archive, this won't lessen the size
tar -cvf 100MBFile.tar 100MBFile    # In addition to f, other options' position can be changed at will
ll 100MB*
img

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:

1
2
tar -czvf 100MBFile.tar.gz 100MBFile
ll 100MB*
img

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:

1
2
3
# Clone 100MBFile to 100MBFileGZIP
cp 100MBFile 100MBFileGZIP
ll 100MBFile*
img

Clone a new file called 100MBFileGZIP and list the files

1
2
3
# This will use the Lempel-Ziv coding (LZ77) to compress the file
gzip 100MBFileGZIP
ll 100MBFile*
img

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:

1
2
3
# Clone 100MBFile to 100MBFileGZIP
cp 100MBFile 100MBFileBZIP2
ll 100MBFile*
img

Clone a new file called 100MBFileBZIP2 and list the files

1
2
3
# This will use the Burrows-Wheeler block sorting text compression algorithm and Huffman coding to compress the file
bzip2 100MBFileBZIP2
ll 100MBFile*
img

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:

1
2
3
# Clone 100MBFile to 100MBFileXZ
cp 100MBFile 100MBFileXZ
ll 100MBFile*
img

Clone a new file called 100MBFileXZ and list the files

1
2
xz 100MBFileXZ
ll 100MBFile*
img

Compress the file with xz command

Unpackaging and Decompressing Files

Unpackaging with tar

1
2
tar -xf 100MBFile.tar    # tar command will keep the archived file after decompressing
ll 100MBFile*
img

Unpack the file with option x

Decompressing with gunzip

1
2
gunzip 100MBFileGZIP.gz    # gunzip command will remove the archived file after decompressing
ll 100MBFile*
img

Decompress the file with gunzip command

Decompressing with bunzip2

1
2
bunzip2 100MBFileBZIP2.bz2    # bunzip2 command will remove the archived file after decompressing
ll 100MBFile*
img

Decompress the file with bunzip2 command

Decompressing with unxz

1
2
unxz 100MBFileXZ.xz    # unxz command will remove the archived file after decompressing
ll 100MBFile*
img

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:

1
2
3
4
5
6
7
echo 'This is a README.txt file for GZIP' > READMEGZIP
echo 'This is a README.txt file for BZIP2' > READMEBZIP2
echo 'This is a README.txt file for XZ' > READMEXZ
gzip READMEGZIP
bzip2 READMEBZIP2
xz READMEXZ
ll README*
img

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:

1
tar -tf 100MBFile.tar    # Don't forget the f option behind it
img

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:

1
gzip -l READMEGZIP.gz    # This is equivalent to gzip --list READMEGZIP.gz
img

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:

1
xz -l READMEXZ.xz    # This is equivalent to xz --list READMEXZ.xz
img

View compressed files in an .xz archive

Viewing with zcat, zmore, bzless, zgrep

1
2
3
zcat READMEGZIP.gz
zmore READMEGZIP.gz
zgrep -e 'is a' READMEGZIP.gz
img

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

1
2
3
bzcat READMEBZIP2.bz2
bzmore READMEBZIP2.bz2
bzgrep -e 'is a' READMEBZIP2.bz2
img

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

1
2
3
xzcat READMEXZ.xz
xzmore READMEXZ.xz
xzgrep -e 'is a' READMEXZ.xz
img

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

Buy me a coffeeBuy me a coffee