Home  >  Article  >  Operation and Maintenance  >  What are the linux compression commands?

What are the linux compression commands?

青灯夜游
青灯夜游Original
2023-03-01 18:55:2344104browse

Linux compression command: 1. tar command, just append a "z" compression option, the syntax is "tar -z compressed package source file or directory"; 2. zip command, you can create a compressed file, and At the same time, the integrity of the original file is retained, and the syntax is "zip [option] compressed package name source file or source directory list"; 3. gzip command, the syntax is "gzip [option] source file"; 4. bzip2 command, the syntax is "bzip2 [ Option] Source file"; 5. xz command, syntax "xz compressed package name".

What are the linux compression commands?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is compression in linux

Compression refers to the use of algorithms to process files to retain the maximum file information while reducing the file size The purpose of becoming smaller. The basic principle is to create a dictionary file of the same bytes by searching for repeated bytes in the file, and represent it with a code.

Compression is similar to archive files. Compressed files are also a collection of files and directories, and this collection is also stored in a file. But their difference is that compressed files use different storage methods. Make it take up less disk space than the combined size of all files in the collection.

Since the information processed by the computer is expressed in binary form, the compression software marks the same string of characters in the binary information with special characters. As long as through reasonable mathematical calculations, the file volume can be reduced Greatly compressed. Use compression software to compress one or more files to form a file compression package, which can save storage space and facilitate transmission on the network.

Compressing a file is likely to damage the content in the file. Therefore, compression can be divided into lossy compression and lossless compression. Lossless compression is easy to understand, which means that the compressed data must be accurate; lossy compression means that even if individual data is lost, it will not have much impact on the file. Lossy compression is widely used in animation, sound and image files. Typical representatives are the video file format mpeg, the music file format mp3 and the image file format jpg.

Use a compression tool to compress the file, and the generated file is called a compressed package. The size of the file is usually only half of the original file or even smaller. It should be noted that the data in the compressed package cannot be used directly. You need to use a compression tool to restore the file data before use. This process is also called decompression.

linux compression command

There are many commonly used compression commands, such as gzip, zip, bzip2.

tar compression command

tar command is not a specialized compression command. It is often 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 capabilities, which makes sense, and appending a z compression option can compress the file.

When attaching a compression process to the tar command using the z option, tar uses gzip for compression.

Just like compressing a group of files, you can use tar to compress individual files, although this operation has no particular advantages over using gzip directly. To do this with tar, just use the tar cfz newtarfile filename command to identify the file to compress, just like you would a group of files, like this:

$ tar cfz bigfile.tgz bigfile
            ^            ^
            |            |
            +- 新的文件  +- 将被压缩的文件

$ 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

Note that the file size is significantly reduced.

If you wish, you can use the tar.gz extension, which may make the characteristics of the file more obvious, but most Linux users will most likely realize that the same as tgz means the same thing – a combination of tar and gz to show that the file is a compressed tar file. After the compression is completed, you will get both the original file and the compressed file.

To collect many files together and compress them into a "tar ball" in one command, use the same syntax, but specify the files to be included as a group rather than as individual files. Here is an example:

$ tar cfz bin.tgz bin/*
          ^         ^
          |         +-- 将被包含的文件
          + 新的文件

zip compression command

zip command creates a compressed file while preserving Integrity of the original document. The syntax is as simple as using tar, except you have to 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

gzip compression command

gzip command is very easy to use. You just need to type gzip, followed by the name of the file you want to compress. Unlike the commands described above, gzip will "encrypt" the file "in place". In other words, the original file will be replaced by the "encrypted" file.

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

bzip2压缩命令

像使用 gzip 命令一样,bzip2 将在你选择的文件“就地”压缩,不留下原始文件。

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

xz压缩命令

xz 是压缩命令团队中的一个相对较新的成员,在压缩文件的能力方面,它是一个领跑者。像先前的两个命令一样,你只需要将文件名称提供给命令。再强调一次,原始文件被就地压缩。

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

对于大文件来说,你可能会注意到 xz 将比其它的压缩命令花费更多的运行时间,但是压缩的结果却是非常令人赞叹的。

压缩命令对比

大多数人都听说过“大小不是一切”。所以,让我们比较一下文件大小以及一些当你计划如何压缩文件时的问题。

下面显示的统计数据都与压缩单个文件相关,在上面显示的示例中使用 bigfile。这个文件是一个大的且相当随机的文本文件。压缩率在一定程度上取决于文件的内容。

  • 大小减缩率

当比较时,上面显示的各种压缩命产生下面的结果。百分比表示压缩文件与原始文件的比较效果。

-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%

xz 命令获胜,最终只有压缩文件 13% 的大小,但是所有这些压缩命令都相当显著地减少原始文件的大小。

  • 是否替换原始文件

bzip2gzipxz 命令都用压缩文件替换原始文件。tarzip 命令不替换。

  • 运行时间

xz 命令似乎比其它命令需要花费更多的时间来“加密”文件。对于 bigfile 来说,大概的时间是:

命令      运行时间
tar       4.9 秒
zip       5.2 秒
bzip2    22.8 秒
gzip      4.8 秒
xz       50.4 秒

解压缩文件很可能比压缩时间要短得多。

  • 文件权限

不管你对压缩文件设置什么权限,压缩文件的权限将基于你的 umask 设置,但 bzip2 除外,它保留了原始文件的权限。

  • 与 Windows 的兼容性

zip 命令创建的文件可以在 Windows 系统以及 Linux 和其他 Unix 系统上使用(即解压),而无需安装其他工具,无论这些工具可能是可用还是不可用的。

相关推荐:《Linux视频教程

The above is the detailed content of What are the linux compression commands?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn