This article was sponsored by Xiuyi linfeng and first launched in the dark world.
The tar command is generally used for packaging and decompression. You can view the help documentation as follows:
Tar-help
Man tar
Tar has the following important parameters:
-C. Create a new archive
-F when used together with the-C option, the created tar file uses the specified file name of this option. When used together with the-x option, the archive specified by this option is decompressed.This parameter must be placed at the end of all parameters..
-T display the file list in the TAR file
-V shows the archiving or archiving progress
-Z uses gzip to compress the tar file
-J uses Bzip2 to compress the tar file
-X decompress the tar package,
-C: Decompress the package to the new directory. The command is in uppercase.
-P: Retain the original permissions and attributes of the backup data.
Note that-C,-T, and-x cannot appear simultaneously in a single command..
Now we will first explain how to use tar packaging. Note that here we use all gzip files as an example. If it is a bzip file, modify the parameter-Z to-J.
Pack the ilanni into the package. The package contains the ilanni.tar.gz file, and store the package in the/testtar directory:
Tar-cvzf/testtar/ilanni.tar.gz ilanni
Or
Tar-CZF/testtar/ilanni.tar.gz ilanni
The-V parameter is optional.
We can see from the above two images that the package has been successful. We just packed the local file into another directory. If we package the file under directory A to directory B, the following is true:
Tar-CZF/testtar/tar.tar.gz/tar
The purpose of this command is to package/tar.gz and store it in the/testtar directory.
All of the preceding operations are performed on the same server. If you want to package local files and upload them to another server. The procedure is as follows:
SSH [email protected] Tar-CZF/KS/tar.tar.gz/tar/
This command is used to package the/TAR Directory to the/KS directory of the 192.168.1.102 server.
Through the above two images, we can see that/tar has been successfully packaged into the/KS directory of the 192.168.1.102 server.
The above is to package the directory, if it is to package a separate file is also very simple. Let's test, if two files are in different directories, package them into a compressed package. As follows:
Tar-zvcf test.tar.gz./tftp.txt./ilanni/iso.txt
Tar-tvf test.tar.gz
We can clearly see from the above picture. Now we have packaged the tftp.txt file under the current directory and the iso.txt file under the ilanni directory under the Directory into the same compressed package.
The above describes how to use tar packaging. Next we will introduce how to decompress the compressed package using tar. Different commands are required for different compressed packages, such as gzip and Bzip2, based on the network statement.
Use tar-zxf ilanni.tar.gz to decompress the gzip package. Use tar-jxf ks.bz2 to decompress the bzip package.
In fact, there is no need for that. As long as it is a tar compressed package, we can directly use tar-XF for decompression. As follows:
Tar-XF ilanni.tar.gz
Tar-XF ks.bz2
We can see that as long as the tar package is decompressed, we can directly decompress it using tar-XF.
We decompress the compressed package to the same directory. Next we will test and unzip the compressed package to another directory. As follows:
Tar-XF ilanni.tar.gz-C/testtar/
This command is used to extract ilanni.tar.gz from the current directory to the/testtar/directory.
We can see that ilanni.tar.gz has been successfully decompressed to the/testtar/directory.
We decompress the entire directory. If the compressed package is a folder and there are many files in it, we just want to extract one of them. The procedure is as follows:
First, check the directory structure of the compressed package and run the following command:
Tar-tvf ilanni.tar.gz
Note that the yellow part is markedIlanni/tftp.txtNow, we need to extract tftp.txt, and do not extract other files. In addition, this path must be recorded and used for extraction.
Run the following command to decompress the package:
Tar-XF ilanni.tar.gz ilanni/tftp.txt
It can be seen that the tftp.txt file has been decompressed.
If multiple files are not extracted. You can use commands of the type. As follows:
Tar-XF ilanni.tar.gz ilanni/{tftp.txt,iso.txt}
Now, we can see that tftp.txtand iso.txt are used only. The two files are extracted.
The basic usage of tar has ended.
Rotten mud: Tar packaging and decompression for centos command Learning