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 compress
The 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.z
Format changed.gz
Format ).
You can see that there are so many entries for bzip2 compression. You have many options, including zip, gzip, and xz.
$ apropos compress |grep^bz
bzcat (1)- decompresses files to stdout
bzcmp (1)- compare bzip2 compressed files
bzdiff (1)- compare bzip2 compressed files
bzegrep (1)- search possibly bzip2 compressed files for a regular expression
bzexe (1)- compress executable files in place
bzfgrep (1)- search possibly bzip2 compressed files for a regular expression
bzgrep (1)- search possibly bzip2 compressed files for a regular expression
bzip2 (1)- a block-sorting file compressor, v1.0.6
bzless (1)-file perusal filter for crt viewing of bzip2 compressed text
bzmore (1)-file perusal filter for crt viewing of bzip2 compressed text
On my Ubuntu system,apropos compress
The 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.
$ time zip bigfile.zip bigfile
adding: bigfile (default0%)
real 0m0.055s
user 0m0.000s
sys 0m0.016s
$ ls-l bigfile*
-Rw-r -- 1 root 012 bigfile
-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.
( 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 contains0
File, you will have a pretty shocking compression ratio. Under such extreme circumstances, all three common compression tools have very good results.
-rw-rw-r--1 shs shs 10485760Dec812:31 zeroes.txt
-rw-rw-r--1 shs shs 49Dec817:28 zeroes.txt.bz2
-rw-rw-r--1 shs shs 10219Dec817:28 zeroes.txt.gz
-rw-rw-r--1 shs shs 1660Dec812:31 zeroes.txt.xz
-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.
-rw-r--r--1 shs shs 13522Dec1118:58 image.jpg
-rw-r--r--1 shs shs 13875Dec1118:58 image.jpg.bz2
-rw-r--r--1 shs shs 13441Dec1118:58 image.jpg.gz
-rw-r--r--1 shs shs 13508Dec1118:58 image.jpg.xz
-rw-r--r--1 shs shs 13581Dec1118:58 image.jpg.zip
When compressing large text files, you will see a significant difference.
$ ls-l textfile*
-rw-rw-r--1 shs shs 8740836Dec1118:41 textfile
-rw-rw-r--1 shs shs 1519807Dec1118:41 textfile.bz2
-rw-rw-r--1 shs shs 1977669Dec1118:41 textfile.gz
-rw-rw-r--1 shs shs 1024700Dec1118:41 textfile.xz
-rw-rw-r--1 shs shs 1977808Dec1118:41 textfile.zip
In this case,xz
The file size is effectively reduced compared with other compression commands, as is the second bzip2 command.
View compressed files
Thesemore
Command (bzmore
So that you can view the contents of the compressed file without extracting the file.
bzmore (1)-file perusal filter for crt viewing of bzip2 compressed text
lzmore (1)- view xz or lzma compressed (text) files
xzmore (1)- view xz or lzma compressed (text) files
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.
$ xzmore textfile.xz |head-1
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 workcmp
Anddiff
Instead of comparing specific algorithms. For example,xzdiff
The 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