1. 程式人生 > 其它 >使用tar 命令進行檔案的歸檔和壓縮

使用tar 命令進行檔案的歸檔和壓縮

歸檔和壓縮檔案

歸檔和壓縮檔案的好處:節約硬碟的資源 ,加快檔案傳輸速率
tar 命令 作用:打包、壓縮檔案

這幅圖的就是說 123456 這幾個檔案打包成了一個 a.tar 檔案,但是這個 a.tar 還是很大,繼續用
gzip 進行壓縮,變成了一個更小的壓縮檔案。

作用:打包、壓縮檔案;tar 檔案是把幾個檔案和(或)目錄集合在一個檔案裡,該存檔檔案可以通
過使用 gzip、bzip2 或 xz 等壓縮工具進行行壓縮後傳輸

檢視 man tar 這裡 man 不是男人的意思,是 manual 手冊的縮寫
用法:tar [OPTION…] [FILE]…
引數:

-c  create  建立檔案
-x  -extract [ˈekstrækt]   提取  解壓還原檔案
-v  --verbose  顯示執行詳細過程
-f  --file  指定備份檔案
-t  --list  列出壓縮包中包括哪些檔案,不解包,檢視包中的內容
-C  (大寫)--directory

指定解壓位置

示例1:給/boot/grub2 目錄 打包

[root@xuegod63 ~]#   tar -cvf grub.tar /boot/grub2/    # tar 的引數前可以不使用‘-’
[root@xuegod63 ~]#   tar cvf grub.tar /boot/grub2/
[root@xuegod63 ~]# tar cf grub.tar /boot/grub2/

tar: 從成員名中刪除開頭的“/”(就是把/根路徑刪除掉了,就變成了 boot/grub2 相對路徑了,解
壓時會解壓到當前目錄,如果不刪除,那就是/boot/grub2,當解壓的時候就是絕對路徑了,就會覆蓋
系統中此路徑的檔案)

[root@xuegod63 ~]# tar -cf grub.tar /boot/grub2/

tar: 從成員名中刪除開頭的“/”

[root@xuegod63 ~]# ls grub.tar
[root@xuegod63 ~]# tar xvf grub.tar #解壓縮
boot/grub2/
boot/grub2/device.map
[root@xuegod63 ~]# ls  boot    #得到 boot  目錄
grub2

示例2:把兩個目錄或目錄+檔案打包成一個歸檔包:

root@xuegod63 ~]# mkdir ./back
[root@xuegod63 ~]# cp /etc/passwd ./back/
[root@xuegod63 ~]# tar -cvf back.tar /boot/grub   /root/back/ /etc/passwd
tar:  從成員名中刪除開頭的“/”
-rw-r--r-- root/root  1024 2020-06-28 19:46 boot/grub2/grubenv  ###輸出內容
-rw-r--r-- root/root  5130 2020-06-28 19:46 boot/grub2/grub.cfg
drwxr-xr-x root/root   0 2020-07-03 14:23 root/back/
-rw-r--r-- root/root    2735 2020-07-03 14:23 root/back/passwd
-rw-r--r-- root/root    2735 2020-06-28 19:49 etc/passwd

例 3:不解包,檢視 tar 中的內容:

[root@xuegod63 ~]# tar -tvf back.tar             # List all files in archive.tar verbosely.

例 4:對比加 v 的效果

root@xuegod63 ~]# tar -xf back.tar
[root@xuegod63 ~]# tar -xvf back.tar
boot/grub/
boot/grub/splash.xpm.gz

tar 歸檔+壓縮

語法:tar czvf newfile.tar.gz SOURCE
語法:tar czvf 壓縮後的檔名(tar.gz tar.bz2) 需要壓縮的檔案或目錄
常用引數:

-z, --gzip   以 gzip 方式壓縮   副檔名:  tar.gz
-j :  以 bz2 方式壓縮的   副檔名:tar.bz2
-J:  以 xz 方式壓縮      副檔名:tar.xz

示例1:建立.tar.gz 包

[root@xuegod63 ~]# tar cvf /root/etc.tar /etc
[root@localhost test]# tar zcvf /root/etc.tar.gz /etc   #歸檔,注意備份的名字字尾
[root@localhost test]# tar zxvf /root/etc.tar.gz    #解壓縮
[root@localhost test]# tar xvf  /root/etc.tar.gz    #解壓縮

示例2:建立.tar.bz2 包

語法:  #tar jcvf newfile.tar.bz2  SOURCE
[root@xuegod63 ~]#   tar -jcvf ~/etc.tar.bz2 /etc
[root@xuegod63 ~]#   tar -jxvf ~/etc.tar.bz2    #解壓縮
[root@xuegod63 ~]#   tar -xvf ~/etc.tar.bz2    #解壓縮
[root@xuegod63 ~]#   tar jxvf ~/etc.tar.bz2 -C  /opt    #解壓到 opt  目錄下

示例3:建立.tar.xz 包

[root@xuegod63 ~]#   tar -Jcvf ~/etc.tar.xz /etc
[root@xuegod63 ~]#   tar -Jxvf ~/etc.tar.xz     #tar.xz  這類包,解壓縮
[root@xuegod63 ~]#   tar -xvf ~/etc.tar.xz

對比三種壓縮方式後壓縮比例:

[root@xuegod63 ~]# time tar zcf /root/etc.tar.gz /etc
[root@xuegod63 ~]# time tar jcf /root/etc.tar.bz2 /etc
[root@xuegod63 ~]# time tar Jcf /root/etc.tar.xz /etc
[root@xuegod63 ~]# ll -h etc.tar*   (*為萬用字元,代表任意字元任意次)
-rw-r--r-- 1 0 root  28M 5 月   10 12:10 etc.tar
-rw-r--r-- 1 0 root  8.7M 5 月   10 12:14 etc.tar.bz2#常用
-rw-r--r-- 1 0 root  9.8M 5 月   10 12:11 etc.tar.gz  #常用
-rw-r--r-- 1 0 root  7.0M 5 月   10 12:16 etc.tar.xz   #這個壓縮比例最高,壓縮的時間是最長

檢視原始檔大小

[root@xuegod63 ~]# du -sh /etc
31M /etc

etc.tar 包為 28M ,實際幾乎沒怎麼壓縮。xz 格式化為 7.0M,傳輸的時候效率提高很多。

今天就為大家分享到這,下面有驚喜哦。

想要免費領取這本書籍加我傳送資訊:k8s書籍 即可免費領取此書籍。