天天看點

學習Linux tar 指令:最簡單也最困難

摘要:在本文中,您将學習與tar 指令一起使用的最常用标志、如何建立和提取 tar 存檔以及如何建立和提取 gzip 壓縮的 tar 存檔。

本文分享自華為雲社群《Linux 中的 Tar 指令:壓縮和提取檔案,學會了嗎》,作者:Tiamo_T 。

Linux tar 指令如何工作?

tar 指令用于建立 .tar、.tar.gz、.tgz 或 tar.bz2 檔案,通常稱為“tarball”。擴充名 .tar.gz 和 .tgz 用于識别使用 gzip 壓縮生成的檔案,以減少檔案的大小。 擴充名為 .tar.bz2 的檔案是使用 bzip2 壓縮生成的。

Linux 發行版提供 tar 二進制檔案,無需外部指令的幫助即可支援 gzip 壓縮。正如我們将在本文中看到的那樣,這可能不适用于其他類型的壓縮。

讓我們從tar指令的三個示例開始,以熟悉最常見的标志。

建立一個包含兩個檔案的存檔

這是 tar 指令的基本示例,在這種情況下我們不使用壓縮:

tar -cf archive.tar testfile1 testfile2      

此指令建立一個名為 archive.tar 的存檔檔案,其中包含兩個檔案:testfile1 和 testfile2。

這是兩個标志的含義:

  • -c(與-create 相同):建立一個新存檔
  • -f:它允許指定一個存檔檔案(在這種情況下稱為archive.tar)

file 指令确認 archive.tar 是一個存檔:

[myuser@localhost]$ file archive.tar 
archive.tar: POSIX tar archive (GNU)      

另一個有用的标志是-v标志,它提供在 Linux 上執行tar指令期間處理的檔案的詳細輸出。

如果我們在建立存檔時也傳遞 -v 标志,讓我們看看輸出如何變化:

[myuser@localhost]$ tar -cfv archive.tar testfile1 testfile2
tar: archive.tar: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors      

奇怪,由于某種原因,我們得到了一個錯誤……

這是因為 tar 指令根據 -f 标志後面的内容建立了一個具有名稱的存檔,在這種情況下,在 -f 标志之後是v。

結果是一個名為 v 的存檔,您可以從下面的 ls 輸出中看到:

[myuser@localhost]$ ls -al
total 20
drwxrwxr-x. 2 myuser mygroup  4096 Jul 17 09:42 .
drwxrwxrwt. 6 root     root      4096 Jul 17 09:38 ..
-rw-rw-r--. 1 myuser mygroup     0 Jul 17 09:38 testfile1
-rw-rw-r--. 1 myuser mygroup     0 Jul 17 09:38 testfile2
-rw-rw-r--. 1 myuser mygroup 10240 Jul 17 09:42 v

[myuser@localhost]$ file v
v: POSIX tar archive (GNU)      

“沒有這樣的檔案或目錄”目錄是由于 tar 試圖建立一個名為 v 的存檔,其中包含三個檔案:archive.tar、testfile1 和 testfile2。

但是 archive.tar 不存在,是以出現錯誤。

這表明 tar 的标志順序是多麼重要。

讓我們交換 tar 指令中的 -f 和 -v 标志并重試:

[myuser@localhost]$ tar -cvf archive.tar testfile1 testfile2
testfile1
testfile2      

這次一切順利,詳細标志顯示了添加到我們正在建立的存檔中的兩個檔案的名稱。

說得通?

詳細列出 tar 存檔中的所有檔案

要列出 tar 存檔中的所有檔案而不提取其内容,我們将引入第四個标志:

-t : 列出檔案的内容

我們現在可以将三個标志放在一起:-t、-v和-f來檢視我們之前建立的存檔中的檔案:

[myuser@localhost]$ tar -tvf archive.tar 
-rw-rw-r-- myuser/mygroup 0 2020-07-17 09:38 testfile1
-rw-rw-r-- myuser/mygroup 0 2020-07-17 09:38 testfile2      

我應該将 Dash 與 Tar 一起使用嗎?

我注意到在某些情況下出現标志之前的破折号,但情況并非總是如此。

是以,讓我們看看是否通過破折号有什麼不同。

首先,讓我們嘗試在不使用标志前的破折号的情況下運作相同的指令:

[myuser@localhost]$ tar tvf archive.tar 
-rw-rw-r-- myuser/mygroup 0 2020-07-17 09:38 testfile1
-rw-rw-r-- myuser/mygroup 0 2020-07-17 09:38 testfile2      

輸出是相同的,這意味着不需要破折号。

隻是給您一個想法,您可以按以下方式運作 tar 指令并獲得相同的輸出:

tar -t -v -f archive.tar 
tar -tvf archive.tar
tar -tvf archive.tar
tar --list --verbose --file archive.tar      

最後一個指令使用長選項樣式作為提供給 Linux 指令的标志。

您可以看到使用标志的簡短版本要容易得多。

從存檔中提取所有檔案

讓我們引入一個額外的标志,允許提取 tar 存檔的内容。這是-x标志。

要提取我們之前建立的檔案的内容,我們可以使用以下指令:

tar -xvf archive.tar
(the two lines below are the output of the command in the shell)
testfile1
testfile2
ls -al
total 20
drwxrwxr-x 2 myuser mygroup    59 Feb 10 21:21 .
drwxr-xr-x 3 myuser mygroup    55 Feb 10 21:21 ..
-rw-rw-r-- 1 myuser mygroup 10240 Feb 10 21:17 archive.tar
-rw-rw-r-- 1 myuser mygroup    54 Feb 10 21:17 testfile1
-rw-rw-r-- 1 myuser mygroup    78 Feb 10 21:17 testfile2       

正如您所看到的,我們使用-x标志來提取檔案的内容,使用-v标志來詳細提取,使用-f标志來引用在标志之後指定的檔案檔案 (archive.tar)。

注意:如前所述,我們隻在所有标志之前輸入一次破折号字元。我們可以在每個标志之前指定破折号,而輸出将是相同的。

tar -x -v -f archive.tar      

還有一種方法可以從存檔中提取單個檔案。

在這種情況下,考慮到我們的存檔中隻有兩個檔案,這并沒有太大差別。但是,如果您有一個包含數千個檔案的存檔并且您隻需要其中一個,那麼它會産生巨大的差異。

如果您有一個備份腳本來建立過去 30 天的日志檔案存檔,并且您隻想檢視特定日期的日志檔案的内容,那麼這很常見。

要僅從 archive.tar 中提取 testfile1,您可以使用以下通用文法:

tar -xvf {archive_file} {path_to_file_to_extract}      

在我們的具體案例中:

tar -xvf archive.tar testfile1      

讓我們看看如果我建立一個包含兩個目錄的 tar 存檔會發生什麼變化:

[myuser@localhost]$ ls -ltr
total 8
drwxrwxr-x. 2 myuser mygroup 4096 Jul 17 10:34 dir1
drwxrwxr-x. 2 myuser mygroup 4096 Jul 17 10:34 dir2

[myuser@localhost]$ tar -cvf archive.tar dir*
dir1/
dir1/testfile1
dir2/
dir2/testfile2      

注意:請注意,我使用通配符 * 将名稱以“dir”開頭的任何檔案或目錄包含在存檔中。

如果我隻想提取 testfile1 指令将是:

tar -xvf archive.tar dir1/testfile1      

解壓後保留了原來的目錄結構,是以我将在dir1中得到testfile1:

[myuser@localhost]$ ls -al dir1/
total 8
drwxrwxr-x. 2 myuser mygroup 4096 Jul 17 10:36 .
drwxrwxr-x. 3 myuser mygroup 4096 Jul 17 10:36 ..
-rw-rw-r--. 1 myuser mygroup    0 Jul 17 10:34 testfile1      

一切都清楚了嗎?

減少 tar 檔案的大小

Gzip和Bzip2壓縮可用于減小 tar 存檔的大小。

啟用壓縮的其他 tar 标志是:

  • -z用于 Gzip 壓縮:長标志是–gzip
  • -j用于 Bzip2 壓縮:長标志為–bzip2

要使用詳細輸出建立名為 archive.tar.gz 的 gzipped tar 存檔,我們将使用以下指令(也是建立 tar 存檔時最常用的指令之一):

tar -czvf archive.tar.gz testfile1 testfile2      

并提取其内容,我們将使用:

tar -xzvf archive.tar.gz      

我們也可以使用 .tgz 擴充名而不是 .tar.gz,結果是一樣的。

現在,讓我們建立一個使用 bzip2 壓縮的存檔:

[myuser@localhost]$ tar -cvjf archive.tar.bz2 testfile*
testfile1
testfile2
/bin/sh: bzip2: command not found
tar: Child returned status 127
tar: Error is not recoverable: exiting now      

錯誤“bzip2: command not found”表明 tar 指令正在嘗試使用 bzip2 指令進行壓縮,但在我們的 Linux 系統上找不到該指令。

解決辦法是安裝bzip2。該過程取決于您使用的 Linux 發行版,在我的情況下是使用 yum 作為包管理器的 CentOS。

讓我們使用以下yum 指令安裝 bzip2 :

yum install bzip2      

我可以使用 which 指令确認 bzip2 二進制檔案存在:

[myuser@localhost]$ which bzip2
/usr/bin/bzip2      

現在,如果我再次使用 bzip2 壓縮運作 tar 指令:

[myuser@localhost]$ tar -cvjf archive.tar.bz2 testfile*
testfile1
testfile2
[myuser@localhost]$ ls -al
total 16
drwxrwxr-x. 2 myuser mygroup 4096 Jul 17 10:45 .
drwxrwxrwt. 6 root     root     4096 Jul 17 10:53 ..
-rw-rw-r--. 1 myuser mygroup  136 Jul 17 10:54 archive.tar.bz2
-rw-rw-r--. 1 myuser mygroup  128 Jul 17 10:45 archive.tar.gz
-rw-rw-r--. 1 myuser mygroup    0 Jul 17 10:44 testfile1
-rw-rw-r--. 1 myuser mygroup    0 Jul 17 10:44 testfile2      

一切正常!

另外,考慮到我很好奇,我想根據 Linux file 指令檢視兩個存檔(.tar.gz 和 .tar.bz2)之間的差別:

[myuser@localhost]$ file archive.tar.gz 
archive.tar.gz: gzip compressed data, last modified: Fri Jul 17 10:45:04 2020, from Unix, original size 10240
[myuser@localhost]$ file archive.tar.bz2 
archive.tar.bz2: bzip2 compressed data, block size = 900k      

如您所見,Linux 可以區分使用兩種不同壓縮算法生成的檔案。

結論

在本文中,您學習了與tar 指令一起使用的最常用标志、如何建立和提取 tar 存檔以及如何建立和提取 gzip 壓縮的 tar 存檔。

讓我們再次回顧一下所有的标志:

  • -c:建立一個新的存檔
  • -f:允許指定存檔的檔案名
  • -t:列出檔案的内容
  • -v:詳細列出已處理的檔案
  • -x:從存檔中提取檔案
  • -z:使用gzip壓縮
  • -j:使用bzip2壓縮

點選關注,第一時間了解華為雲新鮮技術~

繼續閱讀