1. 程式人生 > >tar打包工具的使用

tar打包工具的使用

切換 rect 變更 tar -xvf 命名 inux .gz list CP

zip壓縮文件

zip壓縮,壓縮文件後原文件不會刪除

[root@localhost mulu]# zip wen.txt.zip wen.txt     zip  壓縮命名   要壓縮的文件
  adding: wen.txt (deflated 73%)
[root@localhost mulu]# ll -h
總用量 3.7M
-rw-r--r-- 1 root root 2.9M 6月 21 15:54 wen.txt
-rw-r--r-- 1 root root 783K 6月 24 12:54 wen.txt.zip

zip 壓縮目錄

[root@localhost tmp]# zip -r mulu.zip 1.txt mulu/        -r指定壓縮目錄的選項
  adding: 1.txt (stored 0%)
  adding: mulu/ (stored 0%)
  adding: mulu/wen.txt (deflated 73%)
  adding: mulu/wen.txt.zip (stored 0%)
[root@localhost tmp]# ll -h         壓縮目錄和文件後
總用量 1.6M
-rw-r--r-- 1 root root 0 6月 24 12:57 1.txt
drwxr-xr-x 2 root root 40 6月 24 12:54 mulu
-rw-r--r-- 1 root root 1.6M 6月 24 12:57 mulu.zip

zip解壓

[root@localhost tmp]# unzip mulu.zip 
Archive: mulu.zip     使用unzip解壓時,如果原文件和目錄存在的話,會提示是否覆蓋
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: 

zip不支持查看壓縮的文件內的內容,但是可以查看一個壓縮包裏有哪些文件,使用unzip -l查看


[root@localhost tmp]# unzip -l mulu.zip 
Archive: mulu.zip
  Length Date Time Name
--------- ---------- ----- ----
        0 06-24-2018 12:57 1.txt
        0 06-24-2018 12:54 mulu/
  3017434 06-21-2018 15:54 mulu/wen.txt
   800798 06-24-2018 12:54 mulu/wen.txt.zip
--------- -------
  3818232 4 files

tar打包工具

tar -cvf 打包名稱 要打包的內容
tar -xcf 包名
tar -tf 包名 查看打包文件內部的文件數量
tar -cvf 包名 --exclude 1.txt 要打包的目錄/ 打包一個目錄排除掉指定的文件不進行打包

[root@localhost tmp]# tar -cvf mulu.tar mulu  
mulu/
mulu/wen.txt
mulu/wen.txt.zip
[root@localhost tmp]# ll -h
總用量 5.2M
-rw-r--r-- 1 root root 0 6月 24 12:57 1.txt
drwxr-xr-x 2 root root 40 6月 24 13:00 mulu
-rw-r--r-- 1 root root 3.7M 6月 24 13:11 mulu.tar
-rw-r--r-- 1 root root 1.6M 6月 24 12:57 mulu.zip
[root@localhost tmp]# tar -xvf mulu.tar 
mulu/
mulu/wen.txt
mulu/wen.txt.zip
[root@localhost tmp]# tar -tf mulu.tar    查看打包文件內的文件信息 
mulu/
mulu/wen.txt
mulu/wen.txt.zip
[root@localhost tmp]# tar -cvf mu.tar --exclude 1.txt mulu /打包一個目錄排除掉指定文件
mulu.tar
mulu.zip

打包並壓縮

tar -zcf 壓縮包名稱 要壓縮的目錄/文件 以zip格式壓縮
tar -z位置是指定壓縮格式的,z表示為gzip,j表示為bzip2,-c壓縮,-x解壓

tar是在Linux中使用得非常廣泛的文檔打包格式。它的好處就是它只消耗非常少的CPU以及時間去打包文件,但它僅僅只是一個打包工具,並不負責壓縮。下面是如何打包一個目錄:

[root@localhost ]# tar -cvf archive_name.tar directory_to_compress

下面是如何解包的命令:

[root@localhost ]# tar -xvf archive_name.tar.gz

上面這個解包命令將會將文檔解開在當前目錄下面。當然,你也可以用這個命令來更改解包的路徑:

[root@localhost ]# tar -xvf archive_name.tar -C /tmp/extract_here/

tar.gz 它在壓縮時不會占用太多CPU的,而且可以得到一個非常理想的壓縮率。可以使用下面的命令去壓縮一個目錄:

[root@localhost ]# tar -zcvf  archive_name.tar.gz  directory_to_compress

tar.gz解壓縮:

[root@localhost ]# tar -zxvf archive_name.tar.gz

上面這個解包命令將會將文檔解壓在當前目錄下面。當然,也可以用這個命令來更改解包的路徑:

[root@localhost ]# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

tar.bz2

這種壓縮格式是所有方式中壓縮率最好的。當然,這也就意味著,它比前面的方式要占用更多的CPU與時間。下面的命令就是如何使用tar.bz2進行壓縮。

[root@localhost ]# tar -jcvf archive_name.tar.bz2 directory_to_compress

上面這個解包命令將會將文檔解開在當前目錄下面。也可以用這個命令來更改解包的路徑:

[root@localhost ]# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/
下面對tar命令中一些常用重要的參數進行總結:

-c或–create 建立新的備份文件。 
-C<目的目錄>或–directory=<目的目錄> 切換到指定的目錄。 
-f<備份文件>或–file=<備份文件> 指定備份文件。 
-j或–bzip2 以bz2的算法來壓縮或者解壓文件。 
-k或–keep-old-files 解開備份文件時,不覆蓋已有的文件。 
-m或–modification-time 還原文件時,不變更文件的更改時間。 
-N<日期格式>或–newer=<日期時間> 只將較指定日期更新的文件保存到備份文件裏。 
-r或–append 新增文件到已存在的備份文件的結尾部分。 
-t或–list 列出備份文件的內容。 
-u或–update 僅置換較備份文件內的文件更新的文件。 
-v或–verbose 顯示指令執行過程。 
-w或–interactive 遭遇問題時先詢問用戶。 
-W或–verify 寫入備份文件後,確認文件正確無誤。 
-x或–extract或–get 從備份文件中還原文件。 
-z或–gzip或–ungzip 通過gzip指令處理備份文件。 
-Z或–compress或–uncompress 通過compress指令處理備份文件。

tar打包工具的使用