您的位置:首页 > 运维架构 > Linux

linux tar cvf_Linux中的Tar命令:Tar CVF和Tar XVF通过示例命令进行了解释

2020-08-21 10:28 1061 查看

linux tar cvf

The name

tar
is, by most accounts, short for tape archive. The "tapes" in question would be all those magnetic storage drives that were popular all the way back in the 1950s.

在大多数情况下,

tar
是磁带归档的缩写。 有问题的“磁带”将是所有早在1950年代就很流行的磁存储驱动器。

That suggests that the

tar
tool might be a bit old and past its prime. But the truth is that, over all the years and through all the seismic changes to the IT world,
tar
has lost none of its power and value.

这表明

tar
工具可能有点陈旧,已经过时了。 但事实是,多年来,在IT世界发生的所有重大变化中,
tar
失去其力量和价值。

In this article, based on content from my Linux in Action book, I'm going to show you the basics of tar archive creation, compression, and restoration. Let's start at the beginning.

在本文中,基于Linux in Action书中的内容 ,我将向您展示tar归档文件创建,压缩和还原的基础知识。 让我们从头开始。

创建档案 (Creating archives)

This example will take all the files and directories within and below the current work directory and build an archive file that I’ve cleverly named

archivename.tar
.

此示例将获取当前工作目录内和下的所有文件和目录,并构建一个我巧妙地命名为

archivename.tar
的存档文件。

Here I use three arguments after the tar command:

在这里,我在tar命令之后使用三个参数:

  • the

    c
    tells tar to create a new archive,

    c
    告诉tar创建一个新的存档,

  • v
    sets the screen output to verbose so I’ll get updates, and

    v
    将屏幕输出设置为详细,以便我进行更新,并且

  • f
    points to the filename I’d like to give the archive.

    f
    指向我要提供存档的文件名。

The

*
is what tells
tar
to include all files and local directories recursively.

*
是告诉
tar
递归包括所有文件和本地目录的原因。

$ tar cvf archivename.tar *
file1
file2
file3
directory1
directory1/morestuff
directory1/morestuff/file100
directory1/morestuff/file101

The tar command will never move or delete any of the original directories and files you feed it – it only makes archived copies.

tar命令将永远不会移动或删除您提供给它的任何原始目录和文件-它只会生成存档副本。

You should also note that using a dot (.) instead of an asterisk (*) in the previous command would include even hidden files (whose filenames begin with a dot) in the archive.

您还应该注意,在上一个命令中使用点(。)而不是星号(*)会在归档文件中甚至包含隐藏文件(文件名以点开头)。

If you’re following along on your own computer (as you definitely should), then you’ll see a new file named archivename.tar. The .tar filename extension isn’t necessary, but it’s always a good idea to clearly communicate the purpose of a file in as many ways as possible.

如果您按照自己的意愿(当然应该)在自己的计算机上进行跟踪,则将看到一个名为archivename.tar的新文件。 .tar文件扩展名不是必需的,但是以尽可能多的方式清楚地传达文件的目的始终是一个好主意。

Extracting your archive in order to restore the files is easy: Just use

xvf
instead of
cvf
. That, in the example, will save new copies of the original files and directories in the current location.

提取存档以恢复文件很容易:只需使用

xvf
而不是
cvf
。 在该示例中,该操作会将原始文件和目录的新副本保存在当前位置。

$ tar xvf archivename.tar
file1
file2
file3
directory1
directory1/morestuff
directory1/morestuff/file100
directory1/morestuff/file101

Of course, you can also have tar send your extracted files to some other place using the

-C
argument, followed by the target location:

当然,您也可以让tar使用

-C
参数,然后是目标位置,将提取的文件发送到其他位置:

$ tar xvf archivename.tar -C /home/mydata/oldfiles/

You won’t always want to include all the files within a directory tree in your archive.

您将不会总是希望将所有文件都包含在存档中的目录树中。

Suppose you’ve produced some videos, but they're currently kept in directories along with all kinds of graphic, audio, and text files (containing your notes). The only files you need to back up are the final video clips using the .mp4 filename extension.

假设您已经制作了一些视频,但是当前它们与各种图形,音频和文本文件(包含您的笔记)一起保存在目录中。 您唯一需要备份的文件是使用.mp4文件扩展名的最终视频剪辑。

Here’s how to do that:

这样做的方法如下:

$ tar cvf archivename.tar *.mp4

That’s great. But those video files are enormous. Wouldn’t it be nice to make that archive a bit smaller using compression?

那很棒。 但是那些视频文件很大。 使用压缩将档案压缩得更小不是很好吗?

Say no more! Just run the previous command with the

z
(zip) argument. That will tell the gzip program to compress the archive.

别说了! 只需使用

z
(zip)参数运行前面的命令。 这将告诉gzip程序压缩档案。

If you want to follow convention, you can also add a

.gz
extension in addition to the
.tar
that’s already there. Remember: clarity.

如果要遵循约定,除了已经存在的

.tar
之外,还可以添加
.gz
扩展名。 记住:清晰度。

Here’s how that would play out:

播放结果如下:

$ tar czvf archivename.tar.gz *.mp4

If you try this out on your own .mp4 files and then run ls -l on the directory containing the new archives, you may notice that the

.tar.gz
file isn’t all that much smaller than the
.tar
file, perhaps 10% or so. What’s with that?

如果您对自己的.mp4文件进行尝试,然后在包含新档案的目录上运行ls -l,您可能会注意到

.tar.gz
文件并不比
.tar
文件小很多,也许是10 % 或者。 那是什么

Well, the

.mp4
file format is itself compressed, so there’s a lot less room for gzip to do its stuff.

好的,

.mp4
文件格式本身是经过压缩的,因此gzip可以减少其处理工作的空间。

As tar is fully aware of its Linux environment, you can use it to select files and directories that live outside your current working directory. This example adds all the

.mp4
files in the
/home/myuser/Videos/
directory:

由于tar完全了解其Linux环境,因此您可以使用它来选择位于当前工作目录之外的文件和目录。 本示例将所有

.mp4
文件添加到
/home/myuser/Videos/
目录中:

$ tar czvf archivename.tar.gz /home/myuser/Videos/*.mp4

Because archive files can get big, it might sometimes make sense to break them down into multiple smaller files, transfer them to their new home, and then re-create the original file at the other end. The split tool is made for this purpose.

由于存档文件可能很大,因此有时将它们分解成多个较小的文件,然后将它们转移到新位置,然后在另一端重新创建原始文件,这可能是有意义的。 分割工具就是为此目的而制造的。

In this example,

-b
tells Linux to split the archivename.tar.gz file into 1 GB-sized parts. The operation then names each of the parts—archivename.tar.gz.partaa, archivename.tar.gz.partab, archivename.tar.gz.partac, and so on:

在此示例中,

-b
告诉Linux将archivename.tar.gz文件拆分为1 GB大小的部分。 然后,该操作将命名每个部分,分别是archivename.tar.gz.partaa,archivename.tar.gz.partab,archivename.tar.gz.partac,依此类推:

$ split -b 1G archivename.tar.gz "archivename.tar.gz.part"

On the other side, you re-create the archive by reading each of the parts in sequence (cat archivename.tar.gz.part*), then redirect the output to a new file called archivename.tar.gz:

另一方面,您可以通过依次读取每个部分(cat archivename.tar.gz.part *)来重新创建档案,然后将输出重定向到名为archivename.tar.gz的新文件中:

$ cat archivename.tar.gz.part* > archivename.tar.gz

流式文件系统档案 (Streaming file system archives)

Here’s where the good stuff starts. I’m going to show you how to create an archive image of a working Linux installation and stream it to a remote storage location — all within a single command. Here’s the command:

这是好东西开始的地方。 我将向您展示如何创建可正常运行的Linux安装的存档映像并将其流式传输到远程存储位置-全部都在一个命令中。 这是命令:

# tar czvf - --one-file-system / /usr /var \
--exclude=/home/andy/ | ssh username@10.0.3.141 \
"cat > /home/username/workstation-backup-Apr-10.tar.gz"

Rather than trying to explain all that right away, I’ll use smaller examples to explore it one piece at a time.

我不会尝试立即解释所有内容,而是会使用较小的示例来一次探索它。

Let’s create an archive of the contents of a directory called importantstuff that’s filled with, well, really important stuff:

让我们创建一个目录的内容的存档,该目录称为Importantstuff,里面充满了非常重要的东西:

$ tar czvf - importantstuff/ | ssh username@10.0.3.141 "cat > /home/username/myfiles.tar.gz"
importantstuff/filename1
importantstuff/filename2
[...]
username@10.0.3.141's password:

Let me explain that example. Rather than entering the archive name right after the command arguments (the way you’ve done until now), I used a dash (czvf -).

让我解释一下这个例子。 我没有在命令参数后面输入存档名称(直到现在为止仍然如此),而是使用了破折号(czvf-)。

The dash outputs data to standard output. It lets you push the archive filename details back to the end of the command and tells tar to expect the source content for the archive instead.

破折号将数据输出到标准输出。 它使您可以将归档文件的文件名详细信息推回命令的末尾,并告诉tar期望归档文件的源内容。

I then piped (|) the unnamed, compressed archive to an ssh login on a remote server where I was asked for my password.

然后,我将未命名的压缩归档通过管道(|)传送到要求我输入密码的远程服务器上的ssh登录名上。

The command enclosed in quotation marks then executed cat against the archive data stream, which wrote the stream contents to a file called myfiles.tar.gz in my home directory on the remote host.

然后用引号引起来的命令对归档数据流执行,该归档数据流将流内容写入到远程主机上我的主目录中名为myfiles.tar.gz的文件中。

One advantage of generating archives this way is that you avoid the overhead of a middle step. There’s no need to even temporarily save a copy of the archive on the local machine. Imagine backing up an installation that fills 110 GB of its 128 GB of available space. Where would the archive go?

以这种方式生成档案的一个优点是,避免了中间步骤的开销。 甚至无需将存档的副本临时保存在本地计算机上。 想象一下备份一个128 GB可用空间中的110 GB的安装。 存档会去哪里?

That was just a directory of files. Suppose you need to back up an active Linux OS to a USB drive so you can move it over to a separate machine and drop it into that machine’s main drive.

那只是文件目录。 假设您需要将活动的Linux操作系统备份到USB驱动器,以便可以将其移至单独的计算机上并将其放入该计算机的主驱动器中。

Assuming there’s already a fresh installation of the same Linux version on the second machine, the next copy/paste operation will generate an exact replica of the first.

假设第二台计算机上已经有相同Linux版本的全新安装,则下一个复制/粘贴操作将生成第一台计算机的精确副本。

NOTE: This won’t work on a target drive that doesn’t already have a Linux file system installed. To handle that situation, you’ll need to use

dd
.

注意:此操作不适用于尚未安装Linux文件系统的目标驱动器。 要处理这种情况,您需要使用

dd

The next example creates a compressed archive on the USB drive known as

/dev/sdc1
.

下一个示例在称为

/dev/sdc1
的USB驱动器上创建一个压缩存档。

The

--one-file-system
argument excludes all data from any file system besides the current one. This means that pseudo partitions like
/sys/
and
/dev/
won’t be added to the archive. If there are other partitions that you want to include (as you’ll do for
/usr/
and
/var/
in this example), then they should be explicitly added.

--one-file-system
参数排除当前文件系统之外的任何文件系统中的所有数据。 这意味着伪分区(例如
/sys/
/dev/
不会添加到存档中。 如果要包含其他分区(如本例中的
/usr/
/var/
),则应显式添加它们。

Finally, you can exclude data from the current file system using the

--exclude
argument:

最后,您可以使用

--exclude
参数从当前文件系统中排除数据:

# tar czvf /dev/sdc1/workstation-backup-Apr-10.tar.gz \
--one-file-system \
/ /usr /var \
--exclude=/home/andy/

Now let’s go back to that full-service command example. Using what you’ve already learned, archive all the important directories of a file system and copy the archive file to a USB drive. It should make sense to you now:

现在,让我们回到该提供全方位服务的命令示例。 使用已经学过的知识,归档文件系统的所有重要目录,然后将归档文件复制到USB驱动器。 现在对您来说应该有意义:

# tar czvf - --one-file-system / /usr /var \
--exclude=/home/andy/ | ssh username@10.0.3.141 \
"cat > /home/username/workstation-backup-Apr-10.tar.gz"

There's much more administration goodness in the form of books, courses, and articles available at my bootstrap-it.com.

我的bootstrap-it.com上提供了书籍,课程和文章形式的管理优势。

翻译自: https://www.freecodecamp.org/news/tar-command-linux-tar-cvf-tar-xvf/

linux tar cvf

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: