在Linux中如何歸檔文件和目錄
在我們之前的教程中,我們討論了如何使用 gzip 和 bzip2 壓縮和解壓縮文件。在本教程中,我們將學習如何在 Linux 歸檔文件。歸檔和壓縮有什么不同嗎?你們中的一些人可能經(jīng)常認為這些術(shù)語有相同的含義。但是,這兩者完全不同。歸檔是將多個文件和目錄(相同或不同大?。┙M合成一個文件的過程。另一方面,壓縮是減小文件或目錄大小的過程。歸檔通常用作系統(tǒng)備份的一部分,或者將數(shù)據(jù)從一個系統(tǒng)移至另一個系統(tǒng)時。希望你了解歸檔和壓縮之間的區(qū)別?,F(xiàn)在,讓我們進入主題。
歸檔文件和目錄
歸檔文件和目錄最常見的程序是:
- tar
- zip
這是一個很大的話題,所以,我將分兩部分發(fā)表這篇文章。在***部分中,我們將看到如何使用 tar 命令來歸檔文件和目錄。
使用 tar 命令歸檔文件和目錄
Tar 是一個 Unix 命令,代表 Tape Archive(磁帶歸檔)。它用于將多個文件(相同或不同大?。┙M合或存儲到一個文件中。在 tar 實用程序中有 4 種主要的操作模式。
c
– 從文件或目錄中建立歸檔x
– 提取歸檔r
– 將文件追加到歸檔t
– 列出歸檔的內(nèi)容
有關(guān)完整的模式列表,參閱 man 手冊頁。
創(chuàng)建一個新的歸檔
為了本指南,我將使用名為 ostechnix
的文件夾,其中包含三種不同類型的文件。
$ ls ostechnix/
file.odt image.png song.mp3
現(xiàn)在,讓我們?yōu)?ostechnix
目錄創(chuàng)建一個新的 tar 歸檔。
$ tar cf ostechnix.tar ostechnix/
這里,c
標志指的是創(chuàng)建新的歸檔,f
是指定歸檔文件。
同樣,對當前工作目錄中的一組文件創(chuàng)建歸檔文件,使用以下命令:
$ tar cf archive.tar file1 file2 file 3
提取歸檔
要在當前目錄中提取歸檔文件,只需執(zhí)行以下操作:
$ tar xf ostechnix.tar
我們還可以使用 C
標志(大寫字母 C)將歸檔提取到不同的目錄中。例如,以下命令將歸檔文件提取到 Downloads
目錄中。
$ tar xf ostechnix.tar -C Downloads/
或者,轉(zhuǎn)到 Downloads
文件夾并像下面一樣提取其中的歸檔。
$ cd Downloads/
$ tar xf ../ostechnix.tar
有時,你可能想要提取特定類型的文件。例如,以下命令提取 “.png” 類型的文件。
$ tar xf ostechnix.tar --wildcards "*.png"
創(chuàng)建 gzip 和 bzip 格式的壓縮歸檔
默認情況下,tar 創(chuàng)建歸檔文件以 .tar
結(jié)尾。另外,tar
命令可以與壓縮實用程序 gzip
和 bzip
結(jié)合使用。文件結(jié)尾以 .tar
為擴展名使用普通 tar 來歸檔文件,文件以 tar.gz
或 .tgz
結(jié)尾使用 gzip
歸檔并壓縮文件,文件以 tar.bz2
或 .tbz
結(jié)尾使用 bzip
歸檔并壓縮。
首先,讓我們來創(chuàng)建一個 gzip 歸檔:
$ tar czf ostechnix.tar.gz ostechnix/
或者:
$ tar czf ostechnix.tgz ostechnix/
這里,我們使用 z
標志來使用 gzip 壓縮方法壓縮歸檔文件。
你可以使用 v
標志在創(chuàng)建歸檔時查看進度。
$ tar czvf ostechnix.tar.gz ostechnix/
ostechnix/
ostechnix/file.odt
ostechnix/image.png
ostechnix/song.mp3
這里,v
指顯示進度。
從一個文件列表創(chuàng)建 gzip 歸檔文件:
$ tar czf archive.tgz file1 file2 file3
要提取當前目錄中的 gzip 歸檔文件,使用:
$ tar xzf ostechnix.tgz
要提取到其他文件夾,使用 -C
標志:
$ tar xzf ostechnix.tgz -C Downloads/
現(xiàn)在,讓我們創(chuàng)建 bzip 歸檔。為此,請使用下面的 j
標志。
創(chuàng)建一個目錄的歸檔:
$ tar cjf ostechnix.tar.bz2 ostechnix/
或
$ tar cjf ostechnix.tbz ostechnix/
從一個列表文件中創(chuàng)建歸檔:
$ tar cjf archive.tar.bz2 file1 file2 file3
或
$ tar cjf archive.tbz file1 file2 file3
為了顯示進度,使用 v
標志。
現(xiàn)在,在當前目錄下,讓我們提取一個 bzip 歸檔。這樣做:
$ tar xjf ostechnix.tar.bz2
或者,提取歸檔文件到其他目錄:
$ tar xjf ostechnix.tar.bz2 -C Downloads
一次創(chuàng)建多個目錄和/或文件的歸檔
這是 tar
命令的另一個最酷的功能。要一次創(chuàng)建多個目錄或文件的 gzip 歸檔文件,使用以下文件:
$ tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt
上述命令創(chuàng)建 Downloads
、 Documents
目錄和 ostechnix
目錄下的 file.odt
文件的歸檔,并將歸檔保存在當前工作目錄中。
在創(chuàng)建歸檔時跳過目錄和/或文件
這在備份數(shù)據(jù)時非常有用。你可以在備份中排除不重要的文件或目錄,這是 –exclude
選項所能幫助的。例如你想要創(chuàng)建 /home
目錄的歸檔,但不希望包括 Downloads
、 Documents
、 Pictures
、 Music
這些目錄。
這是我們的做法:
$ tar czvf ostechnix.tgz /home/sk --exclude=/home/sk/Downloads --exclude=/home/sk/Documents --exclude=/home/sk/Pictures --exclude=/home/sk/Music
上述命令將對我的 $HOME
目錄創(chuàng)建一個 gzip 歸檔,其中不包括 Downloads
、Documents
、Pictures
和 Music
目錄。要創(chuàng)建 bzip 歸檔,將 z
替換為 j
,并在上例中使用擴展名 .bz2
。
列出歸檔文件但不提取它們
要列出歸檔文件的內(nèi)容,我們使用 t
標志。
$ tar tf ostechnix.tar
ostechnix/
ostechnix/file.odt
ostechnix/image.png
ostechnix/song.mp3
要查看詳細輸出,使用 v
標志。
$ tar tvf ostechnix.tar
drwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/
-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt
-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png
-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3
追加文件到歸檔
文件或目錄可以使用 r
標志添加/更新到現(xiàn)有的歸檔??纯聪旅娴拿睿?/p>
$ tar rf ostechnix.tar ostechnix/ sk/ example.txt
上面的命令會將名為 sk
的目錄和名為 exmple.txt
添加到 ostechnix.tar
歸檔文件中。
你可以使用以下命令驗證文件是否已添加:
$ tar tvf ostechnix.tar
drwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/
-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt
-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png
-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3
drwxr-xr-x sk/users 0 2018-03-26 19:52 sk/
-rw-r--r-- sk/users 0 2018-03-26 19:39 sk/linux.txt
-rw-r--r-- sk/users 0 2018-03-26 19:56 example.txt
TL;DR
創(chuàng)建 tar 歸檔:
- 普通 tar 歸檔:
tar -cf archive.tar file1 file2 file3
- Gzip tar 歸檔:
tar -czf archive.tgz file1 file2 file3
- Bzip tar 歸檔:
tar -cjf archive.tbz file1 file2 file3
提取 tar 歸檔:
- 普通 tar 歸檔:
tar -xf archive.tar
- Gzip tar 歸檔:
tar -xzf archive.tgz
- Bzip tar 歸檔:
tar -xjf archive.tbz
我們只介紹了 tar
命令的基本用法,這些對于開始使用 tar
命令足夠了。但是,如果你想了解更多詳細信息,參閱 man 手冊頁。
$ man tar
好吧,這就是全部了。在下一部分中,我們將看到如何使用 Zip 實用程序來歸檔文件和目錄。