带有示例Linux Tar命令教程

Linux tar is a very popular tool used to compress and extract files and folders. Tar is used for a long time and its primary usage intention was using for tape archiving. Tar supports primarily tar format or extension which is used to create one file for all directories and files. But through the history tar have integrated compression protocol support like gz, bzip. Tar is generally used with these popular compression protocols to compress files and folders. Typically .tar, .tar.gz and .tar.bz extensions are used to name compressed files. In this tutorial, we will look in detail on how to use tar command for different situations and operations.

Linux tar是一种非常流行的工具,用于压缩和提取文件和文件夹。 Tar已使用了很长时间,其主要用途是用于磁带存档。 Tar主要支持tar格式或扩展名,该格式或扩展名用于为所有目录和文件创建一个文件。 但是从历史上看,tar集成了gz,bzip等压缩协议。 Tar通常与这些流行的压缩协议一起使用来压缩文件和文件夹。 通常,.tar,.tar.gz和.tar.bz扩展名用于命名压缩文件。 在本教程中,我们将详细介绍如何在不同情况和操作下使用tar命令。

创建Tar存档 (Create Tar Archive)

As we stated before tar archive will create a single file for all directories and files. This is very useful for compression, transfer, backup, etc. Tar is generally used for transfer or backup operations too. In this example, we will create our desktop directory as a tar file.

如前所述,tar归档将为所有目录和文件创建一个文件。 这对于压缩,传输,备份等非常有用。Tar通常也用于传输或备份操作。 在此示例中,我们将桌面目录创建为tar文件。

$ tar -cvf tmux.tar tmux-2.2
Create Tar Archive
Create Tar Archive
创建Tar存档

Here we have created a tmux.tar which provides all files and folders in tmux-2.2 

在这里我们创建了一个tmux.tar ,它提供了tmux-2.2中的所有文件和文件夹

详细列出提取或压缩的文件和文件夹(List Extracted or Compressed Files and Folders Verbosely)

To get details of operations verbose option is should be specified. In the previous example, we are already using the verbose option with -v .

要获取操作的详细信息,应指定verbose选项。 在前面的示例中,我们已经在-v中使用了verbose选项。

$ tar -cvf tmux.tar tmux-2.2

$ tar -cvf tmux.tar tmux-2.2

使用Gzip压缩创建Tar存档 (Create Tar Archive With Gzip Compression)

Gzip is a popular and good compression algorithm used for a lot of different purposes. For example browsers and web servers uses a gzip compression algorithm too. Tar and gzip can be used together with just a single command like below.

Gzip是一种流行且良好的压缩算法,可用于许多不同目的。 例如,浏览器和Web服务器也使用gzip压缩算法。 Tar和gzip可以与以下单个命令一起使用。

$ tar cvfz tmux.tar.gz tmux-2.2
Create Tar Archive With Gzip Compression
Create Tar Archive With Gzip Compression
使用Gzip压缩创建Tar存档

使用Bzip2压缩创建Tar存档(Create Tar Archive With Bzip2 Compression)

Bzip2 is an alternative to the gzip compression algorithm. Bzip2 popular especially in Linux, Unix, and BSD platforms. It can be used the same as gzip.

Bzip2是gzip压缩算法的替代方法。 Bzip2特别在Linux,Unix和BSD平台中流行。 可以和gzip一样使用。

$ tar cvfj tmux.tar.bz2 tmux-2.2
Create Tar Archive With Bzip2 Compression
Create Tar Archive With Bzip2 Compression
使用Bzip2压缩创建Tar存档

解压缩并解压缩Tar存档(Decompress and Untar Tar Archive)

Up to now, we have compressed our files and directories. What about decompressing them? We will decompress all contents or the archives to the current working directory like below. The main parameter used to extract a tar archive is .

到目前为止,我们已经压缩了文件和目录。 将它们解压缩怎么办? 我们将所有内容或档案解压缩到当前的工作目录,如下所示。 用于提取tar存档的主要参数是x

$ tar xvf tmux.tar
Decompress or Untar Tar Archive
Decompress or Untar Tar Archive
解压缩或解压缩Tar存档

解压缩或解压缩tar.gz存档(Decompress or Untar tar.gz Archive)

Gzip compressed tar archives are similar while decompressing. We can add gzip parameter to the normal extract tar command like below.

Gzip压缩的tar存档在解压缩时类似。 我们可以将gzip参数添加到普通的extract tar命令中,如下所示。

$ tar xvfz tmux.tar.gz
Decompress or Untar tar.gz Archive
Decompress or Untar tar.gz Archive
解压缩或解压缩tar.gz存档

解压缩或解压缩tar.bz2存档(Decompress or Untar tar.bz2 Archive)

Another popular compression algorithm used with tar archive is Bzip2. Bzip2 compressed tar archives uses *.tar.bz2 file extension by default. In Order to decompress or extract or untar a bzip2 archive file, we will use the xvfj options by providing the bzipped archive file name or path like below.

tar归档文件中另一个流行的压缩算法是Bzip2。 Bzip2压缩的tar归档文件默认使用* .tar.bz2文件扩展名。 为了解压缩或解压缩bzip2存档文件,我们将通过提供如下所示的bzip存档文件名或路径来使用xvfj选项。

$ tar xvfj tmux.tar.bz2
Decompress or Untar tar.bz2 Archive
Decompress or Untar tar.bz2 Archive
解压缩或解压缩tar.bz2存档

列出或显示Tar存档的内容(List or Display Contents Of Tar Archive)

A tar archive generally holds a lot of files and folders and listing files of a tar archive is easy as compressing files and directories. We will provide -tvf option like below to list tmux.tar file contents.

tar归档文件通常包含许多文件和文件夹,而tar归档文件的列出文件很容易压缩文件和目录。 我们将提供如下所示的-tvf选项以列出tmux.tar文件的内容。

$ tar -tvf tmux.tar
List Content Of Tar Archive
List Content Of Tar Archive
Tar存档的列表内容

Tar.Gz存档的列表内容(List Content Of Tar.Gz Archive)

tar.gz contents can be listed below.  It is the same as tar because. Tar detects the compression methods automatically. So we do not need to specify the compression algorithm specifically.

tar.gz的内容可以在下面列出。 它与tar相同,因为。 Tar会自动检测压缩方法。 因此,我们不需要专门指定压缩算法。

$ tar -tvf tmux.tar.gz
List Content Of Tar.Gz Archive
List Content Of Tar.Gz Archive
Tar.Gz存档的列表内容

Tar.Bz2存档的列表内容(List Content Of Tar.Bz2 Archive)

The same as tar and gz formats. We do not specify compression algorithm specifically. It is detected automatically.

与tar和gz格式相同。 我们没有具体指定压缩算法。 它是自动检测到的。

$ tar -tvf tmux.tar.bz2
List Content Of Tar.Bz2 Archive
List Content Of Tar.Bz2 Archive
Tar.Bz2存档的列表内容

从Tar存档中提取单个文件(Extract Single File From Tar Archive)

One of the most wanted ways to extract a file from an archive is to extract only a single file. We will extract a single file by given its name to the tar like below. We will use -xvf option and provide the tar file and the file we want to extract. In this example, we will extract tmux-2.2/hooks.c file from tar file named tmux.tar

从档案中提取文件的最常用方法之一是仅提取一个文件。 我们将通过将其文件名命名为tar来提取单个文件,如下所示。 我们将使用-xvf选项并提供tar文件和我们要提取的文件。 在此示例中,我们将从名为tmux.tar tar文件中提取tmux-2.2/hooks.c文件

$ tar -xvf tmux.tar tmux-2.2/hooks.c
Extract Single File From Tar Archive
Extract Single File From Tar Archive
从Tar存档中提取单个文件

从Tar.Gz存档中提取单个文件(Extract Single File From Tar.Gz Archive)

Extracting a single file from a tar.gz archive is very similar to the previous example. We will add Gzip parameter which is z . We will use -zxvf option like below.

从tar.gz归档文件中提取单个文件与前面的示例非常相似。 我们将添加Gzip参数z 。 我们将使用-zxvf选项,如下所示。

$ tar -zxvf tmux.tar.gz tmux-2.2/hooks.c
Extract Single File From Tar.Gz Archive
Extract Single File From Tar.Gz Archive
从Tar.Gz存档中提取单个文件

从Tar.Bz2存档中提取单个文件(Extract Single File From Tar.Bz2 Archive)

Extracting single file for Bzip2 archives is similar to tar and gzip. We will use -jxvf option like below.

为Bzip2存档提取单个文件类似于tar和gzip。 我们将使用-jxvf选项,如下所示。

$ tar -jxvf tmux.tar.bz2 tmux-2.2/hooks.c
Extract Single File From Tar.Bz2 Archive
Extract Single File From Tar.Bz2 Archive
从Tar.Bz2存档中提取单个文件

从Tar存档中提取多个文件(Extract Multiple File From Tar Archive)

It can be also extracted from multiple files from a tar archive. The files that will be extracted will be listed by splitting with spaces.

也可以从tar归档文件的多个文件中提取它。 将通过空格分隔列出将要提取的文件。

$ tar -xvf tmux.tar "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"
Extract Multiple File From Tar Archive
Extract Multiple File From Tar Archive
从Tar存档中提取多个文件

从Tar.Gz存档中提取多个文件(Extract Multiple File From Tar.Gz Archive)

We can extract multiple files from a gzip archive like a tar archive. We will just put file names we want to extract at the end of the command. In this example, we will extract the file hooks.c and cfg.c

我们可以从gzip存档(例如tar存档)中提取多个文件。 我们将要提取的文件名放在命令末尾。 在此示例中,我们将提取文件hooks.ccfg.c

$ tar -zxvf tmux.tar.gz "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"
Extract Multiple File From Tar.Gz Archive
Extract Multiple File From Tar.Gz Archive
从Tar.Gz存档中提取多个文件

从Tar.Bz2存档中提取多个文件(Extract Multiple File From Tar.Bz2 Archive)

Multiple files of a bzip2 archive can be extracted similar to the tar and gzip. We will just provide bzip2 parameter with j.

可以提取bzip2归档文件的多个文件,类似于tar和gzip。 我们将只为bzip2参数提供j

$ tar -jxvf tmux.tar.bz2 "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"

使用通配符提取指定的文件类型或扩展名 (Extract Specified Files Types or Extensions with Wildcard)

Wildcards are used to specify multiple files with a similar name. We just set fixed parts of names and then put wildcard for the variable part like below. The wildcard is expressed with *.

通配符用于指定多个具有相似名称的文件。 我们只设置名称的固定部分,然后将通配符放在变量部分,如下所示。 通配符用*表示。

$ tar -zxvf  tmux.tar.gz --wildcards '*.txt'

将文件和目录添加到Tar存档 (Add Files and Directories To Tar Archive)

After creating a tar archive we may need to add extra files to the existing tar archive. For this operation, we will use for append or add operation.

创建tar归档文件后,我们可能需要向现有tar归档文件中添加其他文件。 对于此操作,我们将使用r进行附加或添加操作。

$ tar -rvf  tmux.tar test1/
Add Files and Directories To Tar Archive
Add Files and Directories To Tar Archive
将文件和目录添加到Tar存档

In this example, we have added a directory named test1 . Adding a file is the same.

在此示例中,我们添加了一个名为test1的目录。 添加文件是相同的。

获取焦油档案的大小 (Get Size Of Tar Archive)

The size of the content can get with empirical ways like counting words. Below the result is in KB. The given size is the total size after uncompressing the whole content.

内容的大小可以通过经验方法获得,例如计算单词数。 下面的结果以KB为单位。 给定的大小是解压缩整个内容后的总大小。

$ tar -czf - tmux.tar | wc -c
Get Size Of Tar Archive
Get Size Of Tar Archive
获取焦油档案的大小

获取Tar.Gz档案的大小(Get Size Of Tar.Gz Archive)

The size of the tar.gz archive can be listed below. But keep in mind that this is not an exact size. It is an approximation.

tar.gz归档文件的大小可以在下面列出。 但是请记住,这不是确切的大小。 这是一个近似值。

$ tar -czf - tmux.tar.gz | wc -c
Get Size Of Tar.Gz Archive
Get Size Of Tar.Gz Archive
获取Tar.Gz档案的大小

获取Tar.Bz2档案的大小(Get Size Of Tar.Bz2 Archive)

The size of the tar.bz archive can be listed below. But keep in mind that this is not the exact size. It is an approximation.

tar.bz归档文件的大小可以在下面列出。 但是请记住,这不是确切的大小。 这是一个近似值。

$ tar -czf - tmux.tar.bz2 | wc -c
Get Size Of Tar.Bz2 Archive
Get Size Of Tar.Bz2 Archive
获取Tar.Bz2档案的大小

翻译自: https://www.poftut.com/linux-tar-command-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值