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.pswp.cn/news/390116.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390116.shtml
英文地址,請注明出處:http://en.pswp.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…