How to Use File compression in Linux

Source: Internet
Author: User

How to Use File compression in Linux

Linux provides many options for File compression. The key is to select the one that best suits you.

If you have any questions about the File compression commands or options that can be used in Linux, you may want to take a look.apropos compressThe output of this command. If you have the opportunity to do this, you will be surprised to have so many commands to compress and decompress files. In addition, there are many commands to compare and test compressed files, in addition, you can search the compressed file content, or even convert the compressed file from one format to another (for example.zFormat changed.gzFormat ).

You can see that there are so many entries for bzip2 compression. You have many options, including zip, gzip, and xz.

  1. $ apropos compress |grep^bz
  2. bzcat (1)- decompresses files to stdout
  3. bzcmp (1)- compare bzip2 compressed files
  4. bzdiff (1)- compare bzip2 compressed files
  5. bzegrep (1)- search possibly bzip2 compressed files for a regular expression
  6. bzexe (1)- compress executable files in place
  7. bzfgrep (1)- search possibly bzip2 compressed files for a regular expression
  8. bzgrep (1)- search possibly bzip2 compressed files for a regular expression
  9. bzip2 (1)- a block-sorting file compressor, v1.0.6
  10. bzless (1)-file perusal filter for crt viewing of bzip2 compressed text
  11. bzmore (1)-file perusal filter for crt viewing of bzip2 compressed text

On my Ubuntu system,apropos compressThe command returns more than 60 commands.

 

Compression Algorithm

There is no universal solution for compression. Some compression tools are lossy compression. For example, some compression is used to reduce the size of mp3 files, which can make the listener feel similar to the sound source. However, the algorithm used to compress or archive user files on Linux Command lines must be able to precisely restore the original data. In other words, they must be lossless.

How is this done? Let's assume that there are 300 identical characters in a row that can be compressed into strings like "300x", but this algorithm is not very useful for most files, because it is impossible for a file to contain long sequences of the same character than a completely random sequence. The compression algorithm is much more complicated. Since the early Unix compression was introduced for the first time, it became more and more complex.

 

Compression commands on Linux

The most common File compression commands in Linux include:zip,gzip,bzip2,xz. All these compression commands work in a similar way, but you need to weigh how many files are compressed (how much space is saved), compression time, compression file compatibility on other systems you need to use.

Sometimes it takes a lot of time and effort to compress a file. In the following example, the compressed file is actually larger than the original file. This is not a common case, but it may happen-especially when the file content reaches a certain degree of randomness.

  1. $ time zip bigfile.zip bigfile
  2. adding: bigfile (default0%)
  3. real 0m0.055s
  4. user 0m0.000s
  5. sys 0m0.016s
  6. $ ls-l bigfile*
  7. -Rw-r -- 1 root 012 bigfile
  8. -Rw ------- 1 root 16412 at PM bigfile.zip

Note that the compressed version of this file (bigfile.zip) Compared with the original file (bigfile. If compression increases the file size or reduces the proportion, the only benefit may be that it facilitates online backup. If you see the following information after compressing the file, you will not benefit from the compression.

  1. ( defalted 1%)

The file content plays an important role in the File compression process. In the above example, the file size is increased because the file content is too random. Compressing a file only contains0File, you will have a pretty shocking compression ratio. Under such extreme circumstances, all three common compression tools have very good results.

  1. -rw-rw-r--1 shs shs 10485760Dec812:31 zeroes.txt
  2. -rw-rw-r--1 shs shs 49Dec817:28 zeroes.txt.bz2
  3. -rw-rw-r--1 shs shs 10219Dec817:28 zeroes.txt.gz
  4. -rw-rw-r--1 shs shs 1660Dec812:31 zeroes.txt.xz
  5. -rw-rw-r--1 shs shs 10360Dec812:24 zeroes.zip

What is impressive is that you are unlikely to see more than 10 million bytes of data compressed to a file smaller than 50 bytes, because such a file is basically impossible.

In a more realistic situation, the size difference is generally different, but the difference is not significant, for example, for jpg images that are not very large.

  1. -rw-r--r--1 shs shs 13522Dec1118:58 image.jpg
  2. -rw-r--r--1 shs shs 13875Dec1118:58 image.jpg.bz2
  3. -rw-r--r--1 shs shs 13441Dec1118:58 image.jpg.gz
  4. -rw-r--r--1 shs shs 13508Dec1118:58 image.jpg.xz
  5. -rw-r--r--1 shs shs 13581Dec1118:58 image.jpg.zip

When compressing large text files, you will see a significant difference.

  1. $ ls-l textfile*
  2. -rw-rw-r--1 shs shs 8740836Dec1118:41 textfile
  3. -rw-rw-r--1 shs shs 1519807Dec1118:41 textfile.bz2
  4. -rw-rw-r--1 shs shs 1977669Dec1118:41 textfile.gz
  5. -rw-rw-r--1 shs shs 1024700Dec1118:41 textfile.xz
  6. -rw-rw-r--1 shs shs 1977808Dec1118:41 textfile.zip

In this case,xzThe file size is effectively reduced compared with other compression commands, as is the second bzip2 command.

 

View compressed files

ThesemoreCommand (bzmoreSo that you can view the contents of the compressed file without extracting the file.

  1. bzmore (1)-file perusal filter for crt viewing of bzip2 compressed text
  2. lzmore (1)- view xz or lzma compressed (text) files
  3. xzmore (1)- view xz or lzma compressed (text) files
  4. zmore (1)-file perusal filter for crt viewing of compressed text

To decompress the File Content and Display it to you, these commands perform a lot of calculations. But on the other hand, they will not leave the decompressed files on your system, they are just the part needed for instant decompression.

  1. $ xzmore textfile.xz |head-1
  2. Hereis the agenda for tomorrow's staff meeting:

 

Compare compressed files

There are several compression toolboxes that contain a different command (for example:xzdiff), The tools will hand over the workcmpAnddiffInstead of comparing specific algorithms. For example,xzdiffThe command is as simple as comparing files of the bz2 type with files of the Tibetan type.

 

How to choose the best Linux compression Tool

How to select a compression tool depends on your work. In some cases, the selection depends on the data content you compress. In more cases, it depends on the practice in your organization, unless you are highly sensitive to disk space. The following are general suggestions:

Zip is most suitable for files that need to be shared to or used in Windows.

Gzip may be the best file you want to use on Unix/Linux systems. Although bzip2 is nearing popularization, gzip still seems to exist for a long time.

Bzip2 uses different algorithms than gzip and generates files smaller than gzip, but it takes a longer time to compress them.

Xz can usually provide the best compression rate, but it will take quite a long time. It is newer than other tools and may not exist on the system you work on.

 

Note:

You have many choices when compressing files, but in rare cases, it cannot effectively save disk storage space.

Via: https://www.networkworld.com/article/3240938/linux/how-to-squeeze-the-most-out-of-linux-file-compression.html

Author: Sandra Henry-Stocker Translator: singledo Proofreader: wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.