A. linux命令:tar
原單詞 tar命令最初的設計目的是將文件備份到磁帶上(tape archive),因而得名tar
實物長這樣
tar 命令是linux系統中常用的 壓縮文件 和 解壓縮文件命令
tar 命令的參數 異常之多,咱們只學習滿足 壓縮文件 和 解壓縮文件 命令的參數就可以了
<mark style="box-sizing: border-box; background-color: rgb(255, 255, 0); color: rgb(0, 0, 0);">工作中應用於安裝軟體,搭建應用,部署環境</mark>
舉例: 將root目錄下的 adir目錄和a.txt文件 壓縮為 test.tar.gz
注意是大寫C
如果 不使用-C參數 則默認解壓至當前工作目錄
舉例: 將 test.tar.gz 壓縮文件解壓到/root目錄
-c 建立新的備份文件。
-x 從備份文件中還原文件。
-v 顯示指令執行過程。
-f 指定備份文件。
-z 通過gzip指令處理備份文件。(如果解壓文件後綴是.tar,沒有.gz則不需要使用-z參數)
-C 指定解壓到的目錄
使用xshell連接linux
在root 目錄下 新建 tarTest目錄
進入tarTest目錄
壓縮 /root/lnTest文件夾 和 /root/Centos-7.repo 文件 到 /root/tarTest目錄下,壓縮文件名為 test.tar.gz
進入/root/tarTest目錄
解壓test.tar.gz到當前目錄(/root/tarTest)
解壓test.tar.gz到/root/rmTest目錄
B. 在Linux 中如何歸檔文件和目錄
歸檔文件和目錄最常見的程序是:
tarzip
這是一個很大的話題,所以,我將分兩部分發表這篇文章。在第一部分中,我們將看到如何使用 tar 命令來歸檔文件和目錄。
使用 tar 命令歸檔文件和目錄
Tar 是一個 Unix 命令,代表 Tape Archive(磁帶歸檔)。它用於將多個文件(相同或不同大小)組合或存儲到一個文件中。在 tar 實用程序中有 4 種主要的操作模式。
c – 從文件或目錄中建立歸檔
x – 提取歸檔
r – 將文件追加到歸檔
t – 列出歸檔的內容
有關完整的模式列表,參閱 man 手冊頁。
創建一個新的歸檔
為了本指南,我將使用名為 ostechnix 的文件夾,其中包含三種不同類型的文件。
$ ls ostechnix/
file.odt image.png song.mp3
現在,讓我們為 ostechnix 目錄創建一個新的 tar 歸檔。
$ tar cf ostechnix.tar ostechnix/
這里,c 標志指的是創建新的歸檔,f 是指定歸檔文件。
同樣,對當前工作目錄中的一組文件創建歸檔文件,使用以下命令:
$ tar cf archive.tar file1 file2 file 3
提取歸檔
要在當前目錄中提取歸檔文件,只需執行以下操作:
$ tar xf ostechnix.tar
我們還可以使用 C 標志(大寫字母 C)將歸檔提取到不同的目錄中。例如,以下命令將歸檔文件提取到 Downloads 目錄中。
$ tar xf ostechnix.tar -C Downloads/
或者,轉到 Downloads 文件夾並像下面一樣提取其中的歸檔。
$ cd Downloads/
$ tar xf ../ostechnix.tar
有時,你可能想要提取特定類型的文件。例如,以下命令提取 「.png」 類型的文件。
$ tar xf ostechnix.tar --wildcards "*.png"
創建 gzip 和 bzip 格式的壓縮歸檔
默認情況下,tar 創建歸檔文件以 .tar 結尾。另外,tar 命令可以與壓縮實用程序 gzip 和 bzip 結合使用。文件結尾以 .tar 為擴展名使用普通 tar 來歸檔文件,文件以 tar.gz 或 .tgz 結尾使用 gzip 歸檔並壓縮文件,文件以 tar.bz2 或 .tbz 結尾使用 bzip 歸檔並壓縮。
首先,讓我們來創建一個 gzip 歸檔:
$ tar czf ostechnix.tar.gz ostechnix/
或者:
$ tar czf ostechnix.tgz ostechnix/
這里,我們使用 z 標志來使用 gzip 壓縮方法壓縮歸檔文件。
你可以使用 v 標志在創建歸檔時查看進度。
$ tar czvf ostechnix.tar.gz ostechnix/
ostechnix/
ostechnix/file.odt
ostechnix/image.png
ostechnix/song.mp3
這里,v 指顯示進度。
從一個文件列表創建 gzip 歸檔文件:
$ tar czf archive.tgz file1 file2 file3
要提取當前目錄中的 gzip 歸檔文件,使用:
$ tar xzf ostechnix.tgz
要提取到其他文件夾,使用 -C 標志:
$ tar xzf ostechnix.tgz -C Downloads/
現在,讓我們創建 bzip 歸檔。為此,請使用下面的 j 標志。
創建一個目錄的歸檔:
$ tar cjf ostechnix.tar.bz2 ostechnix/
或
$ tar cjf ostechnix.tbz ostechnix/
從一個列表文件中創建歸檔:
$ tar cjf archive.tar.bz2 file1 file2 file3
或
$ tar cjf archive.tbz file1 file2 file3
為了顯示進度,使用 v 標志。
現在,在當前目錄下,讓我們提取一個 bzip 歸檔。這樣做:
$ tar xjf ostechnix.tar.bz2
或者,提取歸檔文件到其他目錄:
$ tar xjf ostechnix.tar.bz2 -C Downloads
一次創建多個目錄和/或文件的歸檔
這是 tar 命令的另一個最酷的功能。要一次創建多個目錄或文件的 gzip 歸檔文件,使用以下文件:
$ tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt
上述命令創建 Downloads、 Documents 目錄和 ostechnix 目錄下的 file.odt 文件的歸檔,並將歸檔保存在當前工作目錄中。
在創建歸檔時跳過目錄和/或文件
這在備份數據時非常有用。你可以在備份中排除不重要的文件或目錄,這是 –exclude 選項所能幫助的。例如你想要創建 /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 目錄創建一個 gzip 歸檔,其中不包括 Downloads、Documents、Pictures 和 Music 目錄。要創建 bzip 歸檔,將 z 替換為 j,並在上例中使用擴展名 .bz2。
列出歸檔文件但不提取它們
要列出歸檔文件的內容,我們使用 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 標志添加/更新到現有的歸檔。看看下面的命令:
$ 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
創建 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