There are many tools available for compressing files on Linux systems, but they don’t all behave the same, and not all compression effects are the same. In this article, we compare five of these tools.

There are several commands for compressing files on Linux. One of the newest and most effective methods is XZ, but all have the advantage of saving disk space and maintaining backup files for later use. In this article, we will compare these compression commands and point out significant differences.

tar

The tar command is not a special compression command. It is commonly used to pull multiple files into a single file for easy transfer to another system, or to back up files as a related group. It also provides compression, which makes sense, adding a z-compression option to compress files.

When using the z option to attach compression to tar, tar uses gzip for compression.

Just like compressing a set of files, you can use tar to compress individual files, although this operation has no particular advantage over using Gzip directly. To do this with tar, simply use the tar CFZ newtarfile filename command to identify the file to be compressed as if it were a group of files, like this:

$tar CFZ bigfile. TGZ bigfile ^ ^ | | + - + - new file will be compressed file $ls-l bigfile*
-rw-rw-r-- 1 shs shs 103270400 Apr 16 16:09 bigfile
-rw-rw-r-- 1 shs shs 21608325 Apr 16 16:08 bigfile.tgz
Copy the code

Notice that the file size is significantly reduced.

If you wish, you can use the tar.gz extension, which may make the file more distinctive, but most Linux users will probably realize that it means the same as TGZ – a combination of tar and gz to show that the file is a compressed tar file. After the compression is complete, you will have both the original file and the compressed file.

To gather many files together and compress a “tar ball” in a single command, use the same syntax, but specify that the files to be included are a group rather than a single file. Here’s an example:

$tar CFZ bin. TGZ bin / * ^ ^ | + - + new file will be includedCopy the code

zip

The zip command creates a compressed file while preserving the integrity of the original file. The syntax is as simple as using tar, except that you must remember that your original file name should be the last argument on the command line.

$ zip ./bigfile.zip bigfile
updating: bigfile (deflated 79%)
$ ls -l bigfile bigfile.zip
-rw-rw-r-- 1 shs shs 103270400 Apr 16 11:18 bigfile
-rw-rw-r-- 1 shs shs  21606889 Apr 16 11:19 bigfile.zip
Copy the code

gzip

The gzip command is very easy to use. All you need to do is type gzip followed by the name of the file you want to compress. Unlike the commands described above, gzip will “encrypt” files “in place.” In other words, the original file will be replaced with an “encrypted” file.

$ gzip bigfile
$ ls -l bigfile*
-rw-rw-r-- 1 shs shs  21606751 Apr 15 17:57 bigfile.gz
Copy the code

bzip2

Like using the gzip command, bzip2 will compress the file you select “in place,” leaving no original files.

$ bzip bigfile
$ ls -l bigfile*
-rw-rw-r-- 1 shs shs  18115234 Apr 15 17:57 bigfile.bz2
Copy the code

xz

Tib is a relatively new member of the compression command team and is a front-runner in terms of its ability to compress files. As with the previous two commands, you only need to supply the file name to the command. Again, the original file is compressed in place.

$ xz bigfile
$ ls -l bigfile*
-rw-rw-r-- 1 shs shs 13427236 Apr 15 17:30 bigfile.xz
Copy the code

For large files, you may notice that XZ takes more time to run than other compression commands, but the compression results are impressive.

contrast

Most people have heard that size isn’t everything. So, let’s compare file sizes and some of the issues when planning how to compress files.

The statistics shown below are all related to compressing a single file, using BigFile in the example shown above. This file is a large and fairly random text file. The compression rate depends partly on the content of the file.

Size reduction rate

When compared, the various compression lives shown above produce the following results. The percentage represents how the compressed file compares to the original file.

-rw-rw-r-- 1 shs shs 103270400 Apr 16 14:01 bigfile
------------------------------------------------------
-rw-rw-r-- 1 shs shs 18115234 Apr 16 13:59 bigfile.bz2    ~17%
-rw-rw-r-- 1 shs shs 21606751 Apr 16 14:00 bigfile.gz     ~21%
-rw-rw-r-- 1 shs shs 21608322 Apr 16 13:59 bigfile.tgz    ~21%
-rw-rw-r-- 1 shs shs 13427236 Apr 16 14:00 bigfile.xz     ~13%
-rw-rw-r-- 1 shs shs 21606889 Apr 16 13:59 bigfile.zip    ~21%
Copy the code

The Xz command won, ending up with only 13% of the compressed file size, but all of these compression commands reduced the size of the original file quite significantly.

Whether to replace the original file

The bzip2, gzip, and xz commands all replace the original file with a compressed file. The tar and zip commands are not replaced.

The elapsed time

The Xz command seems to take more time to “encrypt” the file than other commands. For bigfile, the approximate time is:

Run time tar 4.9 s zip 5.2 s bzip2 22.8 s gzip 4.8 s xz 50.4 sCopy the code

Unzipping a file is likely to take much less time than compression.

File permissions

No matter what permissions you set for the compressed file, permissions for the compressed file will be based on your umask Settings, except for Bzip2, which retains permissions for the original file.

Compatibility with Windows

The files created by the zip command can be used (that is, decompressed) on Windows systems as well as Linux and other Unix systems without the need to install other tools, whether they may be available or not.

Unzip file

The commands for decompressing files are similar to those for compressing files. After we run the above compression commands, these commands are used to unzip bigfile:

  • Tar:tar xf bigfile.tgz
  • Zip:unzip bigfile.zip
  • Gzip:gunzip bigfile.gz
  • Bzip2:bunzip2 bigfile.gz2
  • Xz:xz -d bigfile.xzunxz bigfile.xz

Run the compression comparison yourself

If you want to run some tests yourself, grab a large, replaceable file and compress it using each of the commands shown above — preferably a new subdirectory. You may need to install XZ first if you want to include it in your tests. This script might be easier to compress, but it might take a few minutes to complete.

#! /bin/bash
Ask the user for the file name
echo -n "filename> "
read filename

# You need this because some commands will replace the original file
cp $filename $filename2 -Clean up first (lest the previous result is still available)
rm $filename.*

tar cvfz ./$filename.tgz $filename > /dev/null
zip $filename.zip $filename > /dev/null
bzip2 $filename
Restore the original file
cp $filename2 -$filename
gzip $filename
Restore the original file
cp $filename2 -$filename
xz $filename

# display result
ls -l $filename. *# Replace the original file
mv $filename2 -$filename
Copy the code

Via: www.networkworld.com/article/353…

Written by Sandra Henry-Stocker, lujun9972

This article is originally compiled by LCTT and released in Linux China