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, andv
將屏幕輸出設置為詳細,以便我進行更新,并且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