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 does not change. Compression refers to combining one or more large files or directories Through the compression algorithm, the size of the file is reduced to achieve the purpose of compression, which can save storage space. When compressing, it is usually packed first and then compressed;
tar command
The difference between adding "-" in front of the tar command parameter and not adding "-":
There is no difference in the result of executing the command without adding "-" in front of the tar command parameter. The difference is mainly in the linux style. Adding "-" belongs to System V Style, not adding "-" belongs to BSD style, so when using the tar command, the result of adding or not adding "-" to its parameters is the same, depending on the way of use;
Common parameters:
1. Packing
Example:
a. Package the ceshi.txt file, anaconda-ks.cfg file and time.sh file under /root/ into one file named "jihe.tar": b.
Check the contents of the jihe.tar file:
c. Extract The content of the jihe.tar file to the /opt directory:
If you do not specify a directory with "-C", the content will be extracted to the current directory
Two, compression
Linux mainly has three compression methods:
1.gzip: It is recognized that the compression speed is the fastest. When compressing large files, it is more obvious than other compression methods. It has the longest history and is the most widely used compression method. 2.bzip:
Compression The formed file is small, but the usability is not as good as gzip
3.xz: it is the latest compression method, which can automatically provide the best compression ratio
The suggested suffix is indicated when compressing:
Example: Compress the Golden.apk file in the /root/ directory with different compression methods
First check the size of the Golden.apk file:
You can see that the size of the Golden.apk file is 187M
a. Compress the Golden.apk file into a Golden.apk.tar.gz file with gzip compression method:
b. Compress the Golden.apk file into a Golden.apk.tar.bz2 file with bzip2 compression method:
You can see from the above figure There is an error in the red box. The reason for this error is that the bzip2 package is missing. You need to install a bzip2 package and
then recompress after the installation is complete:
During the compression process, we can find:
Compression speed: gz > bz2 > xz
Compression rate: xz > bz2 > gz
3. Decompression
First delete the Golden.apk file in the /root/ directory:
The tar command is a very smart command. When decompressing, we don’t need to specify our own compression method, it will choose the method corresponding to the compression method to decompress. A bit of a mouthful, for example:
a. Decompress Golden.apk.tar.gz to the current directory:
you don’t need to add -z when decompressing gz compressed files, just use the parameter -xf directly, and the other two compression methods are the same when decompressing , because the tar command will automatically select, the compressed file is still there 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 uncompressed files are saved, and decompress the Golden.apk.tar.bz2 file in the /opt/ directory:
Supplement:
a. In the process of packaging and compressing, we sometimes see such statements: tar: Delete the leading "/" from the member name. This is not an error, because the -P option is not added, and the original absolute path is not preserved for packaging or compression. Extracting the packaged content is the same as decompressing. Here is an example :
Compress the /root/ directory into a root.tar.gz compressed file in gzip mode:
1. The -P option is not added:
2. The -P option is added:
the same is true when decompressing, if the -P option is used when compressing the file, then the -P option must also be added when decompressing, otherwise it will also "tar: remove the leading "/" from the member name" appears, as shown below:
1. Uncompress the root.tar.gz file after compressing /root/ with the -P option without adding the -P option:
2. Add the -P option to decompress the root.tar.gz file after compressing /root/ with the -P option File:
b. When using tar compression or packaging, the purpose of excluding specified files can be achieved by adding –exclude
Pack the harry directory under the /root/ directory, but not the ha.txt file under the harry directory, as shown in the figure below: the
same is true for compressed files, and the same is true for compressing or packing the specified directories
zip command and unzip command
#Before using the zip and unzip commands, first check whether the system has installed the packages of these two commands. If not, install them yourself Check
whether the zip and unzip commands are installed:
rpm -q zip unzip
If the command has been installed, it will display the version number of the command
zip command
Basic usage:
zip [parameter] [compressed package name] [compressed directory or file path]
Common parameters:
Commonly used parameters of the unzip command
Example:
a. Display the result of decompressing the harry.zip compressed package:
each file and the content of each file will be displayed in detail
b. Display the files contained in the jihe.zip compressed package
c. Check whether the dajihe.zip compressed file is correct:
all OK means that all files are correct
d. Do not decompress the compressed file dajihe.zip, and view the contents of the compressed package (check the displayed file list and also include the compression ratio):
The information displayed with -v is more verbose than that displayed with -l
e. Unzip the dajihe.zip archive to the /opt/ directory:
Supplement: How to use the r option to increase the contents of the .tar archive file?
Let's first understand the role of the tar command -r option:
View help through the tar --help command
The role of the -r option: append the file to be archived to the end of the archive file. For example, the user has made a backup file and found that there is still a directory or some files that he forgot to back up. At this time, he can use this option to append the forgotten directory or file to the backup file.
Example of use:
Looking at the picture above, you can see that a file and a directory are marked in the home directory of my ops user. Now I will archive the directory first, and then append the file to the file.
1. Archiving operation
Generate a .tar file, and the directory is archived!
2. Append calculating_time.sh into the .tar file
3. Verify
a. View the archive information of the .tar file,
you can see that the calculating_time.sh file has been appended to the end of the archive
b. Extract Check_Configuration_20201118_PM.tar to the /mnt directory and
use the -r option to append the file to the .tar file successfully