[Familiar with linux] compressed and packaged, familiar with linux
We often use compression and decompression when using Windows. to compress a file, right-click and choose add to compressed file ], decompress the package, right-click it, and select extract to current folder. Click it. However, in a linux operating system that does not have a graphical interface installed, you cannot use the dot. How can this problem be solved? This article explains how to use the compression and packaging tools in linux.
Why File compression?
The purpose of compression is to convert a file into a smaller file by using the compression algorithm, which reduces the space occupied by the file on the hard disk, CPU clock cycle is particularly consumed. Because the CPU requires a large amount of computing, all compression is also an operation to change the time for space, at the same time, files can be transmitted faster through a slow network connection.
Common compression tools
Many compression and decompression tools are provided in the linux operating system. Each compression tool uses different algorithms when performing compression. The better the design, the higher the compression degree. The older compression tools are compress (not commonly used now). Common compression tools include gzip, bzip, xz, and zip. We can use the suffix name to identify which tool is used to compress the compressed file. For example, if compress is used to compress the file, the file suffix is. z. Other compressed file extensions are as follows:
Gzip, gunzip, and zcat
Gzip is the most commonly used compression tool, and gunzip is the corresponding decompression tool. Zcatyou can view the content of a compressed file in the. GZ format.
Syntax: gzip [OPTION]... FILE... common options:-d: extract, equivalent to gunzip;-#: Specify the compression ratio. The default value is 6. The larger the number, the larger the compression ratio (1-9 ), compression ratio = size of the file before compression/size of the file after compression;-c: output the compression result to the standard output. Gzip-c file>/path/to/somefile.gz
Example:
Copy/etc/init. d/functions to the tmp directory and perform gzip compression:
[root@localhost tmp]# cp /etc/init.d/functions /tmp/[root@localhost tmp]# lltotal 16-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions[root@localhost tmp]# gzip functions [root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4694 Sep 3 06:20 functions.gz
You can also use gunzip. To make it easier to remember, we recommend that you use the-d option directly:
[root@localhost tmp]# gzip functions [root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4694 Sep 3 06:20 functions.gz[root@localhost tmp]# gunzip functions.gz [root@localhost tmp]# lltotal 16-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions
Specify the compression ratio:
[root@localhost tmp]# gzip -9 functions [root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4686 Sep 3 06:20 functions.gz
After the compression ratio is set to 9, the compression ratio of 6 is reduced by only 8 bytes. Generally, the dynamic compression ratio is not required, because 6 is already the best choice.
Use-c to output the result to the standard output. We will see a bunch of garbled characters. What is the use of the-c option?
The original file will be deleted when the file is compressed. If you want to retain the original file, you can use the-c option!
[root@localhost tmp]# gzip -c functions > functions.gz[root@localhost tmp]# lltotal 24-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions-rw-r--r--. 1 root root 4694 Sep 3 06:39 functions.gz
You can use zcat to view the File Content Without decompression:
[Root @ localhost tmp] # zcat functions.gz #-*-Shell-script-*-# functions This file contains functions to be used by most or all # shell scripts in the/etc /init. d directory. # TEXTDOMAIN = initscripts # Make sure umask is saneumask 022 # Set up a default search path. PATH = "/sbin:/usr/sbin:/bin:/usr/bin "...... (omitted)
Bzip2, bunzip2, and bzcat
Similar to gzip, bzip2 is the compression tool and bunzip2 is the decompression tool. bzcat is also used to view the file content without any pressure on the file.
Syntax: bzip2 [OPTION]... FILE... common options:-d: extract, equivalent to bunzip2-#: Specify the compression ratio. The default value is 6. The larger the number, the larger the compression ratio (1-9)-k: keep. Compress and retain the original file, bzip2 does not need to use output redirection to the specified file like gzip, which makes it much easier.
Let's take an example:
Copy/etc/init. d/functions to the tmp directory and use bzip2 for compression:
[root@localhost tmp]# bzip2 functions [root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4763 Sep 3 06:20 functions.bz2
This indicates that bzip2 will delete the original file by default, saving disk space.
Let's take a look at the decompression method:
[root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4763 Sep 3 06:20 functions.bz2[root@localhost tmp]# [root@localhost tmp]# bunzip2 functions.bz2 [root@localhost tmp]# lltotal 16-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions[root@localhost tmp]# bzip2 functions [root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4763 Sep 3 06:20 functions.bz2[root@localhost tmp]# bzip2 -d functions.bz2 [root@localhost tmp]# lltotal 16-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions
Okay, I suggest you remember a-d option! Now we can use the following-k options:
[root@localhost tmp]# lltotal 16-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions[root@localhost tmp]# bzip2 -k functions [root@localhost tmp]# lltotal 24-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions-rw-r--r--. 1 root root 4763 Sep 3 06:20 functions.bz2
Use bzcat to view the file content without opening the compressed file:
[Root @ localhost tmp] # bzcat functions.bz2 #-*-Shell-script-*-# functions This file contains functions to be used by most or all # shell scripts in the/etc /init. d directory. # TEXTDOMAIN = initscripts # Make sure umask is saneumask 022 # Set up a default search path. PATH = "/sbin:/usr/sbin:/bin:/usr/bin" export PATH ...... (omitted)
Xz, unxz, and xzcat
The rookie in the compression tool, xz is the compression tool, unxz is the decompression tool, and xzcat is also viewing the file content without opening the compressed file.
Syntax: xz [OPTION]... FILE... common options:-d: extract-#: Specify the compression ratio. The default value is 6-k: compress and retain the original FILE.
For example! Copy/etc/init. d/functions to the tmp directory and use xz compression:
[root@localhost tmp]# xz functions [root@localhost tmp]# lltotal 8-rw-r--r--. 1 root root 4576 Sep 3 06:20 functions.xz
Decompress:
[Root @ localhost tmp] # unxz functions. xz # decompress [root @ localhost tmp] # lltotal 16-rw-r -- r --. 1 root 15131 Sep 3 06:20 functions [root @ localhost tmp] # xz functions [root @ localhost tmp] # lltotal 8-rw-r -- r --. 1 root 4576 Sep 3 functions. xz [root @ localhost tmp] # xz-d functions. xz # decompress [root @ localhost tmp] # lltotal 16-rw-r -- r --. 1 root 15131 Sep 3 06:20 functions
Use the-k option to compress and retain the original file:
[root@localhost tmp]# xz -k functions [root@localhost tmp]# lltotal 24-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions-rw-r--r--. 1 root root 4576 Sep 3 06:20 functions.xz
Try xzcat:
[Root @ localhost tmp] # xzcat functions. xz #-*-Shell-script-*-# functions This file contains functions to be used by most or all # shell scripts in the/etc/init. d directory. # TEXTDOMAIN = initscripts # Make sure umask is saneumask 022 # Set up a default search path. PATH = "/sbin:/usr/sbin:/bin:/usr/bin" export PATH ...... (omitted)
Extension: When we use the man manual, we will find another tool lzma, unlzma, and lzcat, whose suffix is. lzma: Remember xz. It has a certain relationship with lzma. For details, see the man manual.
lzma is equivalent to xz --format=lzmaunlzma is equivalent to xz --format=lzma --decompresslzcat is equivalent to xz --format=lzma --decompress --stdout
When we look for kernel files on the Linux kernel official website, the tools used for File compression are gzip and xz. We can also see that the compression ratio of xz is greater.
Https://www.kernel.org/pub/linux/kernel/v4.x/
One problem now is that only individual files are compressed. Can these tools compress directories?
Create the test directory under the tmp file and copy several files to it:
[root@localhost tmp]# ll /tmp/test/total 332-rw-r--r--. 1 root root 15131 Sep 3 06:59 functions-rw-------. 1 root root 318014 Sep 3 06:59 messages-rw-r--r--. 1 root root 1054 Sep 3 06:58 passwd
Now try to compress the directory:
[root@localhost tmp]# gzip /tmp/test/gzip: /tmp/test/ is a directory -- ignored[root@localhost tmp]# bzip2 /tmp/test/bzip2: Input file /tmp/test/ is a directory.[root@localhost tmp]# xz /tmp/test/xz: /tmp/test/: Is a directory, skipping
No. What should I do? Let's talk about tar next!
Packaging tool tar
The tar command is used to archive files. You can upload multiple files to a directory with a suffix of .tar. The archive files do not compress the files, but may increase the file size slightly. Therefore, compression is generally performed after archiving !. Let's take a look at how to use tar!
Syntax: tar [OPTION]... FILE... method: (1) Create an archive-c-f/path/to/somefile.tar file... -cf/path/to/somefile.tar file... (2) Expand archive-xf/path/from/somefile.tar-C/path/to/somedir (3) view the file list of the archive file-tf/path/to/somefile.tar archive and then compress it. Combined with the previous compression tool, you can compress multiple files. (4) archive compression-z: gzip2-zcf/path/to/somefile.tar.gz file... -zxf/path/to/somefile.tar.gz-C/path/to/somedir # z can be removed-j: bzip2-jcf/path/to/somefile.tar.bz2 file... -jxf/path/to/somefile.tar.bz2-C/path/to/somedir # j can be removed-J: xz-Jcf/path/to/somefile.tar. xz file... -Jxf/path/to/somefile.tar. xz-C/path/to/somedir # J can be removed
Next we will package/tmp/test into a tar file using tar, and then compress tar into A. xz compressed file:
[root@localhost tmp]# tar -cf test.tar test/[root@localhost tmp]# lltotal 340drwxr-xr-x. 2 root root 53 Sep 3 06:59 test-rw-r--r--. 1 root root 348160 Sep 3 07:35 test.tar[root@localhost tmp]# xz test.tar [root@localhost tmp]# lltotal 28drwxr-xr-x. 2 root root 53 Sep 3 06:59 test-rw-r--r--. 1 root root 26748 Sep 3 07:35 test.tar.xz
Use unxz decompress and archive to/root:
[Root @ localhost tmp] # unxz test.tar. xz # decompress [root @ localhost tmp] # lltotal 340drwxr-xr-x. 2 root 53 Sep 3 06:59 test-rw-r --. 1 root 348160 Sep 3 test.tar [root @ localhost tmp] # tar-xf test.tar-C/root/# expand and archive to the specified directory [root @ localhost tmp] # ll/ root/total 4-rw -------. 1 root 1707 Aug 10 anaconda-ks.cfgdrwxr-xr-x. 2 root 53 Sep 3 06:59 test
This is a little troublesome. in the production environment, we usually use the options-z,-j,-J to achieve compression and archiving.
[root@localhost tmp]# tar -zcf test.tar.gz test/[root@localhost tmp]# lltotal 48drwxr-xr-x. 2 root root 53 Sep 3 06:59 test-rw-r--r--. 1 root root 46416 Sep 3 07:48 test.tar.gz[root@localhost tmp]# tar -zxf test.tar.gz -C /root/[root@localhost tmp]# ll /root/test/total 332-rw-r--r--. 1 root root 15131 Sep 3 06:59 functions-rw-------. 1 root root 318014 Sep 3 06:59 messages-rw-r--r--. 1 root root 1054 Sep 3 06:58 passwd
All the two groups of commands tar-zcf, tar-zxf, tar-Jcf, and tar-Jxf are very useful and the most commonly used combination.
Zip and unzip
A compression tool shared by windows and Linux allows you to compress and decompress files between these two operating systems. Here, we will take a simple look:
[Root @ localhost tmp] # lltotal 0drwxr-xr-x. 2 root 53 Sep 3 06:59 test [root @ localhost tmp] # zip test.zip test/adding: test/(stored 0%) [root @ localhost tmp] # lltotal 4drwxr-xr-x. 2 root 53 Sep 3 06:59 test-rw-r --. 1 root 160 Sep 3 08:05 test.zip [root @ localhost tmp] # unzip test.zip-d/root/# use the-d option to decompress the package to the specified folder Archive: test.zip creating: /root/test/[root @ localhost tmp] # ll/root/total 4-rw -------. 1 root 1707 Aug 10 anaconda-ks.cfgdrwxr-xr-x. 2 root 6 Sep 3 06:59 test