Linux compression and packaging commands-tar, zip, unzip

The difference between packaging and compression:

Packaging refers to putting multiple files or directories together to form a total package, which is convenient for storage and transmission, but the size is unchanged. Compression refers to the use of one or more large files or directories to compress files through a compression algorithm. The volume is reduced to achieve the purpose of compression, which can save storage space. When compressing, it is usually packaged and then compressed;

tar command

The difference between adding "-" and not adding "-" in front of tar command parameters:

There is no difference in the result of executing the command without adding "-" in front of the tar command parameter. As long as the difference lies in the Linux style, adding "-" belongs to the System V style, and not adding "-" belongs to the BSD style, so when using the tar command When adding or not adding "-" to its parameters, the result is the same, depending on personal usage;

Common parameters:

Common parameters of tar command
-with Whether it also has the gz attribute
-j Whether it also has bz2 attributes
-J Whether it has xz attributes at the same time
-x Unzip and extract the packaged content
-t View the contents of the compressed package
-c Create a compressed, packaged document
-C Switch to the specified directory, which means that the contents of the specified decompression package and the directory where the packaged contents are stored
-v Show compressed or packaged content
-f Use the file name, followed by the name of the compressed file, as long as the tar command is used, the -f option must be used, and the -f parameter must be listed behind other parameters when used, on the far right
-p Keep the original permissions and attributes of the backup data, often used to back up (-c) important configuration files
-P Keep absolute path

One, packaging

Examples:

a. Package the ceshi.txt file, anaconda-ks.cfg file and time.sh file under /root/ into a single file named "jihe.tar":

b. View the contents of the jihe.tar file:

c. Extract the contents of the jihe.tar file to the /opt directory:

If you do not specify the directory with "-C", the content will be extracted to the current directory

Two, compression

There are three main compression methods in Linux:
1.gzip: It is recognized that the compression is the fastest. When compressing large files, it is more obvious than other compression methods. The oldest and most widely used compression method
2.bzip: Compression The resulting file is small, but the usability is not as good as gzip
3.xz: it is the latest compression method and can automatically provide the best compression rate

The recommended compression is marked with a suffix:

parameter effect naming method
-with Used for gzip compression File name.tar.gz
-j Used for bzip2 compression File name.tar.bz2
-J Used for xz compression File name.tar.xz

Example: Use different compression methods to compress the Golden.apk file in the /root/ directory

First check the size of the Golden.apk file:

You can see that the size of the Golden.apk file is 187M

a. Use gzip compression to compress the Golden.apk file into a Golden.apk.tar.gz file:

b. Use the bzip2 compression method to compress the Golden.apk file into a Golden.apk.tar.bz2 file:

As can be seen from the above figure, there is an error in the red box. The reason for this error is that the bzip2 package is missing, and a bzip2 package needs to be installed.

Recompress after installation:

During the compression process, we can find:

Compression speed: gz> bz2> xz
Compression ratio: xz> bz2> gz

Three, unzip

First delete the Golden.apk file in the /root/ directory:

The tar command is a very clever command. When we decompress, we don’t need to specify our compression method. It will choose the method corresponding to the compression method to decompress. This is a bit confusing, for example:

a. Unzip Golden.apk.tar.gz to the current directory:

You don’t need to add -z when decompressing files in gz compression mode, just use the parameter -xf. The other two compression methods are the same when decompressing, because the tar command will automatically select the compressed file after decompression. If you do not specify where to save the decompressed file, it will be directly decompressed in the current directory

b. Specify the directory where the decompressed files are saved, and decompress the Golden.apk.tar.bz2 file in the /opt/ directory:

supplement:

a. In the process of packaging and compression, we sometimes see this statement: tar: delete the beginning "/" from the member name, this is not an error, because the -P option is not added, and the original is not retained To package or compress the absolute path of the file, extracting the packaged content is the same as decompressing. Here is an example:

Compress the /root/ directory into a root.tar.gz compressed file by gzip:

1. Without the -P option:

2. Add the -P option:

The same is true when decompressing the file. If the -P option is used when compressing the file, then the -P option must be added when decompressing, otherwise the "tar: remove the beginning "/"" from the member name will appear, As shown below:

1. Decompress the root.tar.gz file after using the -P option to compress /root/ without the -P option:

2. Add the -P option to decompress the root.tar.gz file after using the -P option to compress /root/:


b. When using tar compression or packaging, you can add --exclude to achieve the purpose of excluding the specified files

Pack the harry directory in the /root/ directory, but do not pack the ha.txt file in the harry directory, as shown below:

The same is true for compressed files, and the same is true for excluding specified directories from compression or packaging

zip command and unzip command

#Before using the zip and unzip commands, check whether the system has installed the packages of these two commands. If not, install them yourself.
Check whether there are commands to install the zip and unzip commands:

rpm -q zip unzip

If the command is installed, the version number of the command will be displayed

zip command

Basic usage:

zip [parameter] [compressed package name] [path of compressed directory or file]

Common parameters:

Common parameters of the zip command
-m After compressing the file, delete the original file
-O Set the latest change time of all files in the compressed file to the compressed time
-q Quiet mode, do not display the process of instruction execution during compression
-r Recursive compression, processing all sub-files and files in a custom directory together
-x "File list", exclude files in the file list when compressing

 

Examples:

a. Compress the directory, compress the harry directory under the /root/ directory into a compressed package named harry.zip:

1. Normal compression, without the -q option, display the compression process:

2. With the -q option, output in quiet mode without showing the compression process:

b. Compress multiple files, compress the ceshi.txt file, anaconda-ks.cfg file and the Golden.apk file in the /root/ directory into a compressed package named jihe.zip:

Because all compressed files are files, you don’t need to add the -r option

c. Compress the files and directories, compress the ceshi.txt file, anaconda-ks.cfg file and the harry directory under /root/ into a compressed package named dajihe.zip:

d. Compress the harry directory in the /root/ directory into a harry1.zip compressed package, excluding the ha.txt file in the harry directory:

unzip command

Basic usage:

unzip [parameter] [compressed file] (-d [directory]) //If the content in parentheses is not used, the unzipped file is in the current working directory

Common parameters:

Common parameters of unzip command
-c Display the decompressed result on the screen (display the content of each file in each directory), and at the same time make appropriate conversions to the characters, but the compressed package is not decompressed
-l Display the files contained in the compressed file
-t Check whether the compressed file is correct
-v Show detailed information of compressed files during execution
-q Quiet mode, no information is displayed during execution
-d Specify the directory where the file is stored after decompression
-x Specify not to process those files in compressed files

Examples:

a. Display the result of decompressing the harry.zip compressed package:

Specifically, each file and the content of each file will be displayed

b. Display the files contained in the lock in the jihe.zip compressed package

c. Check whether the compressed dajihe.zip file is correct:

All OK means all files are correct

d. Do not decompress the compressed file dajihe.zip, check the contents of the compressed package (check the displayed file list also contains the compression ratio):

#The information displayed with -v is more detailed than the information displayed with -l

e. Unzip the dajihe.zip compressed package to the /opt/ directory:

Supplement: How to use the r option to increase the content of the .tar archive file?

Let's first understand the role of the tar command -r option:

View help through the tar --help command

-r选项的作用:把要存档的文件追加到档案文件的未尾。例如用户已经作好备份文件,又发现还有一个目录或是一些文件忘记备份了,这时可以使用该选项,将忘记的目录或文件追加到备份文件中

使用实例:

看上图,可以看到我ops用户家目录下标记了一个文件,一个目录,我现在先将目录归档,然后再将文件追加到档案里面

1.归档操作

生成.tar文件,目录归档完毕!

2.将calculating_time.sh追加进入.tar文件中

3.验证

a.查看.tar文件的归档信息

可以看到calculating_time.sh文件已经被追加到档案的末尾了

b.提取Check_Configuration_20201118_PM.tar 到/mnt目录下

使用-r选项追加文件至.tar文件成功

Guess you like

Origin blog.csdn.net/weixin_44901564/article/details/99682926