In Linux, the suffix of archive file is tar, and the suffix of compressed package file is tar tar.gz . In Linux, if we want to compress a file, we first need to archive it into tar, and then we can change it into suffix tar.gz The compressed file of. Archiving is to put different folders or files into the same archive file with the suffix tar, which is equivalent to merging documents. The suffix of compressed package in Linux is tar.gz It is not a suffix such as zip in Windows system. We can use the tar command to archive a file. The command for archiving is as follows:
tar -cvf example.tar example
In this command, we file the example folder into example.tar At the same time, the parameter – CVF represents:
c: Create create file
v: Display details
f: File represents a file
The structure of the file directory in our home directory is as follows: there is an example directory, and there is also an example directory example.tar catalog:
The following is the command entered on the terminal before. After printing the file directory structure through ls, you can see the increase example.tar catalog:
You can also archive multiple files into the same package to merge and archive the files. The command is as follows:
tar -cvf exmple.tar file1 file2 file3
Of course, we only archive files now, that is, we put different or single files in one file. If we want to compress these files, we must archive them first, because the compression command can only be used after archiving.
Here we introduce the command to compress the file:
tar -zcvf example.tar.gz example
In this way, we can compress the file. The suffix of the compressed file is tar.gz , indicating that this is a compressed file.
2、 Tar command unzip the file
After we archive a file, we can unzip it when we need it. We’ve created it before example.tar Now we will unzip the archive file. The unzip command is as follows:
tar -xvf example.tar
So that we can file the example.tar File decompression, in the parameter – xvf, where X represents the meaning of extract. If we want to unzip it to a folder, use:
tar -xvf example.tar -C foldername
-After the C parameter, add the name of the folder you want to unzip.
What if we get a compressed package that is archived and compressed at the same time? That is, the suffix is tar.gz How can we unzip it? Method is similar to the above, using the command:
tar -zxvf example.tar.gz
That is, if you want to unzip to a specific folder, you can also use the – C parameter, and then add the corresponding folder.
As shown below, we first create an exam folder, then pack and compress it. After deleting the original file, we can see whether we can restore the compressed package to the original file exam folder, as shown in the following figure: