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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390116.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1894. 找到需要补充粉笔的学生编号

1894. 找到需要补充粉笔的学生编号 一个班级里有 n 个学生,编号为 0 到 n - 1 。每个学生会依次回答问题,编号为 0 的学生先回答,然后是编号为 1 的学生,以此类推,直到编号为 n - 1 的学生,然后老师会重复…

[No0000B0]ReSharper操作指南1/16-入门与简介

安装指南 在安装之前,您可能需要检查系统要求。 ReSharper是一个VisualStudio扩展。它支持VisualStudio2010,2012,2013,2015和2017.安装完成后,您将在VisualStudio的主菜单中找到新的ReSharper条目。大多数ReSharper命令都可以在这个菜单中找到。但是&a…

更改H2元素的颜色

In coding there are often many different solutions to a given problem. This is especially true when it comes to styling an HTML element.在编码中,对于给定问题通常有许多不同的解决方案。 在样式化HTML元素时,尤其如此。 One of the easiest …

[CTSC2008]图腾totem

(图腾这题做的我头疼 233) 记 f(xxxx) 为 xxxx 出现的次数,那么题目就是要求 f(1324) - f(1243) - f(1432) 最有难度的是把上面的式子转化一下,变成 f(1x2x) - f(14xx) - f(12xx) f(1234) 这点除非对 f 的求法能一眼看出来&#…

Box Shadow CSS教程–如何向任何HTML元素添加投影

We can add a drop shadow to any HTML element using the CSS property box-shadow. Heres how. 我们可以使用CSS属性box-shadow将阴影添加到任何HTML元素。 这是如何做。 添加基本​​投影 (Adding a Basic Drop Shadow) Lets first set up some basic HTML elements to add…

数据结构学习笔记(一)——《大话数据结构》

第一章 数据结构绪论 基本概念和术语 数据 描述客观事物的符号,计算机中可以操作的对象,能被计算机识别并输入给计算机处理的符号的集合。包括整型、实型等数值类型和字符、声音、图像、视频等非数值类型。 数据元素 组成数据的、有一定意义的基本单位&a…

6. Z 字形变换

6. Z 字形变换 将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下: P A H N A P L S I I G Y I R之后,你的输出需要从…

java的垃圾回收机制包括:主流回收算法和收集器(jvm的一个主要优化方向)

2019独角兽企业重金招聘Python工程师标准>>> java的垃圾回收机制是java语言的一大特色,解放了开发人员对内存的复杂控制,但如果你想要一个高级java开发人员,还是需要知道其机制,所谓不仅要会用还要知道其原理这样才能用…

北京dns服务器ip地址_什么是DNS? 域名系统,DNS服务器和IP地址概念介绍

北京dns服务器ip地址介绍 (Introduction) By the end of this article, you should have a better understanding of:在本文末尾,您应该对以下内容有更好的了解: What DNS is and what it does 什么是DNS及其作用 What DNS servers do DNS服务器做什么 …

767. 重构字符串

767. 重构字符串 给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。 若可行,输出任意可行的结果。若不可行,返回空字符串。 示例 1: 输入: S “aab” 输出: “aba” 示例 2: 输入: S “aaab” 输出: “…

长生生物狂犬病疫苗造假

这两天暴发的长生生物狂犬病疫苗造假案真是很厉害,世人都说投资不过山海关还真有一定道理。 市场上长生生物的狂犬病疫苗约占1/4左右,是一个非常大的用量。 你别说,疫苗真的是非常适合造假: 1. 狂犬病有一定潜伏期,几天…

小程序 杂记

调试打印测试的方法: 方法1、显示提示框 (微信自带的API) wx.showToast({title: 成功,icon: success,duration: 2000 }) 方法2、js的console.log()方法 //test.js Page({onLoad: function(option){console.log(option.query)} }) wx.showToa…

使用fetch封装ajax_如何使用Fetch在JavaScript中进行AJAX调用

使用fetch封装ajaxI will be sharing bite sized learnings about JavaScript regularly in this series. Well cover JS fundamentals, browsers, DOM, system design, domain architecture and frameworks. 在本系列中,我将定期分享有关JavaScript的小知识。 我们…

RxJS笔记

RxJS 《深入浅出RxJS》读书笔记遗留问题 Observable的HOT与COLD对应的实际场景,以及在编码中的体现chapter1 html部分 测试你对时间的感觉按住我一秒钟然后松手你的时间:毫秒jquery实现 var time new Date().getTime(); $("#hold-me").moused…

滚动一定的高度底色递增

$(window).scroll(function() {var swipeHeight 200;//完全变色高度var scrollTop $(document).scrollTop();//页面滚动高度var x scrollTop/swipeHeight;$(".head-bg").css({"opacity":x}); }) 转载于:https://www.cnblogs.com/lhj-blog/p/8521525.htm…

@hot热加载修饰器导致static静态属性丢失(已解决)

react开发的时候,引入热加载,用了修饰器的引入方式,发现了一个很有意思的问题,网上并没有相关文章,所以抛出来探讨下。 一段很简单的测试代码。但是经过babel编码后,变得很有意思。假设编码成es2016&#x…

49. 字母异位词分组

49. 字母异位词分组 给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母都恰好只用一次。 示例 1: 输入: strs [“eat”, “tea”, “tan”…

python 入门程序_非Python程序员的Python速成课程-如何快速入门

python 入门程序This article is for people who already have experience in programming and want to learn Python quickly.本文适用于已经有编程经验并希望快速学习Python的人们。 I created this resource out of frustration when I couldnt find an online course or a…

cmd命令操作Oracle数据库

//注意cmd命令执行的密码字符不能过于复杂 不能带有特殊符号 以免执行不通过 譬如有!#¥%……&*之类的 所以在Oracle数据库设置密码是不要太复杂 /String Database "ORCL"; 不指向地址程序只能安装在数据库服务器上才能执行到命令String …

OpenCV学习(7.16)

写了个实现摄像头上画线并输出角度的东西……虽然很简单,但脑残的我还是debug了很长时间。 1 // 圆和直线.cpp : 定义控制台应用程序的入口点。2 //3 4 #include "stdafx.h"5 6 using namespace std;7 using namespace cv;8 9 void onMouse(int event, in…