Home >Operation and Maintenance >Linux Operation and Maintenance >How to use linux tar command
In Linux, the tar command can save many files together to a separate tape or disk for archiving. The syntax is "tar [option] source file or directory"; it can also restore the required files from the archive file. File, which is the reverse process of packaging, is called unpacking, the syntax is "tar [option] compressed package"; it can also be packaged and compressed at the same time, the syntax is "tar [option] compressed package source file or directory".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
In the Linux system, the most commonly used archiving (packaging) command is tar, which can save many files together into a single Archive to tape or disk. Not only that, this command can also restore the required files from the archive, which is the reverse process of packaging, called unpacking.
Packages archived using the tar command are usually called tar packages (tar package files all end with ".tar").
The tar command performs packaging operations
When the tar command is used for packaging operations, the basic format of the command is:
[root@localhost ~]#tar [选项] 源文件或目录
The commonly used options of this command and their respective meanings are shown in Table 1.
Options | Meaning |
---|---|
Package multiple files or directories. | |
Append the tar file to the archive. | |
Specifies the file name of the package. The extension of the package is used to identify the format for the administrator, so the extension must be specified correctly; | |
Displays the process of packaging files; |
Example 1: Packing files and directories
[root@localhost ~]# tar -cvf anaconda-ks.cfg.tar anaconda-ks.cfgThe option "-cvf" is generally used, remember to use it when packaging Specify the file name after packaging, and use ".tar" as the extension. The same is true for packaging directories:
Example 2: Pack and compress directories
First of all, let’s make it clear that the compression command cannot directly compress directories. You must first use the tar command to package the directory, and then use the gzip command or the bzip2 command to compress the packaged file. For example:[root@localhost ~]#ll -d test test.tar drwxr-xr-x 2 root root 4096 6月 17 21:09 test -rw-r--r-- 1 root root 10240 6月 18 01:06 test.tar #我们之前已经把test目录打包成test.tar文件 [root@localhost ~]# gzip test.tar [root@localhost ~]# ll test.tar.gz -rw-r--r-- 1 root root 176 6月 18 01:06 test.tar.gz #gzip命令会把test.tar压缩成test.tar.gz
The tar command is used to unpack the tar package
When the tar command is used to unpack the tar package, the The basic format is as follows:[root@localhost ~]#tar [选项] 压缩包When used to unpack, the commonly used options and meanings are shown in Table 2.
Meaning | |
---|---|
Unpack the tar package. | |
Specify the package name of the tar package to be decompressed. | |
Only checks which files or directories are in the tar package and does not unpack the tar package. | |
Specifies the unpacking location. | |
Display the specific process of unpacking. |
The above is the detailed content of How to use linux tar command. For more information, please follow other related articles on the PHP Chinese website!