What file is linux tar?

青灯夜游
青灯夜游Original
2023-04-07 15:57:514062browse

tar is a compressed file format on UNIX/Linux systems and a file format for packages archived using the tar command. In Linux, the tar command can save many files together to a separate tape or disk for archiving; this command can also restore the required files from the archive file, which is the reverse process of packaging, called unpacking.

What file is linux tar?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

tar is a file packaging tool on Unix and Unix-like systems. It can merge multiple files into one file. The packaged file name is also "tar". tar stands for uncompressed tar file. A tar file that has been compressed has the extension of the compressed file appended. For example, a gzip-compressed tar file has an extension of ".tar.gz".

What is tar file?

tar is a compressed file format on UNIX/Linux systems, and a tar file is a compressed file. In Linux systems, you can directly decompress and use this compressed file. It can also be opened using common decompression software such as WinRAR under Windows. tar is actually equivalent to the common rar and zip formats.

Detailed explanation of Linux tar packaging command

In the Linux system, the most commonly used archiving (packaging) command is tar, which can convert many files Save together to a separate tape or disk for archiving. 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.

Table 1 tar packaging command common options and their meanings
Options Meanings
-c Package multiple files or directories.
-A Append the tar file to the archive.
-f package name 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;
-v Displays the process of packaging files;

It should be noted that when using the tar command to specify options, you do not need to enter "-" in front of the options. For example, using the "cvf" option has the same effect as "-cvf".

Here are a few examples to show you how to use the tar command to package files and directories.
[Example 1] Package files and directories.

[root@localhost ~]# tar -cvf anaconda-ks.cfg.tar anaconda-ks.cfg
#把anacondehks.cfg打包为 anacondehks.cfg.tar文件

The option "-cvf" is generally used. Remember that you need to specify the file name after packaging when packaging, and use ".tar" as the extension. The same is true for packaging directories:

[root@localhost ~]# ll -d test/
drwxr-xr-x 2 root root 4096 6月 17 21:09 test/
#test是我们之前的测试目录
[root@localhost ~]# tar -cvf test.tar test/
test/
test/test3
test/test2
test/test1
#把目录打包为test.tar文件

The tar command can also package multiple files or directories, as long as they are separated by spaces. For example:

[root@localhost ~]# tar -cvf ana.tar anaconda-ks.cfg /tmp/
#把anaconda-ks.cfg文件和/tmp目录打包成ana.tar文件包

[Example 2] Pack and compress the directory.

First of all, let me state that the compression command cannot directly compress the directory. You must first use the tar command to package the directory, and then use the gzip command or 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 basic format of this command is as follows:

[root@localhost ~]#tar [选项] 压缩包

When used to unpack, the commonly used options and meanings are shown in Table 2.

Table 2 tar unpacking common options and their meanings
Options Meaning
-x Unpack the tar package.
-f Specify the package name of the tar package to be decompressed.
-t Only checks which files or directories are in the tar package and does not unpack the tar package.
-C Directory Specifies the unpacking location.
-v Display the specific process of unpacking.

其实解打包和打包相比,只是把打包选项 "-cvf" 更换为 "-xvf"。我们来试试:

[root@localhost ~]# tar -xvf anaconda-ks.cfg. tar
#解打包到当前目录下

如果使用 "-xvf" 选项,则会把包中的文件解压到当前目录下。如果想要指定解压位置,则需要使用 "-C(大写)" 选项。例如:

[root@localhost ~]# tar -xvf test.tar -C /tmp
#把文件包test.tar解打包到/tmp/目录下

如果只想查看文件包中有哪些文件,则可以把解打包选项 "-x" 更换为测试选项 "-t"。例如:

[root@localhost ~]# tar -tvf test.tar
drwxr-xr-x root/root 0 2016-06-17 21:09 test/
-rw-r-r- root/root 0 2016-06-17 17:51 test/test3
-rw-r-r- root/root 0 2016-06-17 17:51 test/test2
-rw-r-r- root/root 0 2016-06-17 17:51 test/test1
#会用长格式显示test.tar文件包中文件的详细信息

tar命令做打包压缩(解压缩解打包)操作

你可能会觉得 Linux 实在太不智能了,一个打包压缩,居然还要先打包成 ".tar" 格式,再压缩成 ".tar.gz" 或 ".tar.bz2" 格式。其实 tar 命令是可以同时打包压缩的,前面的讲解之所打包和压缩分开,是为了让大家了解在 Linux 中打包和压缩的不同。

当 tar 命令同时做打包压缩的操作时,其基本格式如下:

[root@localhost ~]#tar [选项] 压缩包 源文件或目录

此处常用的选项有以下 2 个,分别是:

  • -z:压缩和解压缩 ".tar.gz" 格式;
  • -j:压缩和解压缩 ".tar.bz2"格式。

【例 1】压缩与解压缩 ".tar.gz"格式。

[root@localhost ~]# tar -zcvf tmp.tar.gz /tmp/
#把/temp/目录直接打包压缩为".tar.gz"格式,通过"-z"来识别格式,"-cvf"和打包选项一致

解压缩也只是在解打包选项 "-xvf" 前面加了一个 "-z" 选项。

[root@localhost ~]# tar -zxvf tmp.tar.gz
#解压缩与解打包".tar.gz"格式

前面讲的选项 "-C" 用于指定解压位置、"-t" 用于查看压缩包内容,在这里同样适用。

【例 2】压缩与解压缩 ".tar.bz2" 格式。

和".tar.gz"格式唯一的不同就是"-zcvf"选项换成了 "-jcvf",如下所示:

[root@localhost ~]# tar -jcvf tmp.tar.bz2 /tmp/
#打包压缩为".tar.bz2"格式,注意压缩包文件名
[root@localhost ~]# tar -jxvf tmp.tar.bz2
#解压缩与解打包".tar.bz2"格式

把文件直接压缩成".tar.gz"和".tar.bz2"格式,才是 Linux 中最常用的压缩方式,这是大家一定要掌握的压缩和解压缩方法。

tar 命令最初被用来在磁带上创建备份,现在可以在任何设备上创建备份。利用 tar 命令可以把一大堆的文件和目录打包成一个文件,这对于备份文件或是将几个文件组合成为一个文件进行网络传输是非常有用的。

相关推荐:《Linux视频教程

The above is the detailed content of What file is linux tar?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn