Centos7-檔案歸檔與壓縮
阿新 • • 發佈:2018-12-16
1.tar
1.1命令與引數
- 用法:tar [引數] [壓縮檔名] [要壓縮的檔案]
使用引數時,可以不使用 - - 引數:
-c create,建立檔案
-x extract,提取解壓還原檔案
-v 顯示執行顯示過程
-f 指定備份檔案
-t 列出備份檔案內容,不解包檢視包中的內容
-C 指定解包位置
-z --gzip,以gzip方式壓縮 副檔名:tar.gz
-j 以bz2方式壓縮 副檔名:tar.bz2
-J 以xz方式壓縮 副檔名:tar.xz
1.2歸檔例子
打包/etc/hosts檔案
[[email protected] ~]# tar cvf hosts.tar /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
[ [email protected] ~]# ll hosts.tar
-rw-r--r--. 1 root root 10240 Sep 14 20:16 hosts.tar
在使用絕對路徑進行壓縮時,將預設從檔名中刪除該路徑中前面的/符號,這樣揭陽時,會直接解壓到當前目錄,不然會覆蓋掉原系統中的路徑檔案。
指定路徑解包
[[email protected] ~]# tar xvf hosts.tar -C /opt/
etc/hosts
[[email protected] ~]# ll /opt/etc/
total 4
-rw-r--r--. 1 root root 158 Jun 7 2013 hosts
打包多個檔案
[ [email protected] ~]# tar cvf all.tar /etc/hosts /opt/etc/ /etc/passwd
tar: Removing leading `/' from member names
/etc/hosts
/opt/etc/
/opt/etc/hosts
/etc/passwd
[[email protected] ~]# ll all.tar
-rw-r--r--. 1 root root 10240 Sep 14 20:25 all.tar
不解包檔案的情況下,檢視包有什麼檔案
[[email protected] ~]# tar -tvf all.tar
-rw-r--r-- root/root 158 2013-06-07 10:31 etc/hosts
drwxr-xr-x root/root 0 2018-09-14 20:23 opt/etc/
-rw-r--r-- root/root 158 2013-06-07 10:31 opt/etc/hosts
-rw-r--r-- root/root 1040 2018-08-15 13:36 etc/passwd
打包多目錄
[ [email protected] ~]# tar cvf dir.tar /etc/ /var/
[[email protected] ~]# ll dir.tar
-rw-r--r--. 1 root root 149657600 Sep 14 20:29 dir.tar
1.3打包加壓縮
例1:以gzip進行壓縮
[[email protected] ~]# tar zcvf hosts.tar.gz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
對比不壓縮的包大小
[[email protected] ~]# du -h hosts.*
12K hosts.tar
4.0K hosts.tar.gz
解壓
[[email protected] ~]# tar zxvf hosts.tar.gz
etc/hosts
例2:以bz2方式壓縮
[[email protected] ~]# tar jcvf hosts.tar.bz2 /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
報錯:是因為沒喲bzip2的解壓工具,需要安裝
解決 yum install bzip2 -y
[[email protected] ~]# tar jcvf hosts.tar.bz2 /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解壓
[[email protected] ~]# tar jxvf hosts.tar.bz2
etc/hosts
例3:以xz方式壓縮
[[email protected] ~]# tar Jcvf hosts.tar.xz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解壓
[[email protected] ~]# tar Jxvf hosts.tar.xz
etc/hosts
1.4對比三種打包方式的大小與速度
對比速度
[[email protected] ~]# time tar zcvf etc.tar.gz /etc/
real 0m0.868s
user 0m0.740s
sys 0m0.099s
[[email protected] ~]# time tar jcvf etc.tar.bz2 /etc/
real 0m2.037s
user 0m1.933s
sys 0m0.078s
[[email protected] ~]# time tar Jcvf etc.tar.xz /etc/
real 0m9.828s
user 0m9.577s
sys 0m0.193s
time命令輸入解釋
real: 表示程式整個的執行耗時。可以理解為命令執行開始時刻你看了一下手錶,命令執行結束時,你又看了一下手錶,兩次時間的差值就是本次real 代表的值
user:這個時間代表的是命令執行在使用者態的cpu時間
sys: 這個時間代表的命令是執行在核心態的cpu時間
%cpu_usage = (user_time + sys_time)/real_time * 100%
我們這裡只看速度的話,tar.gz最快,bz2次之。
對比大小
[[email protected] ~]# du -sh /etc/
22M /etc/
[[email protected] ~]# du -h etc*
6.0M etc.tar.bz2
6.9M etc.tar.gz
5.0M etc.tar.xz
壓縮時間越久,效率就越高。
2.zip
1.1命令引數
- 需要安裝
[[email protected] ~]# yum install zip unzip -y
zip 壓縮命令
unzip 解壓命令 - 引數:
-r 遞迴壓縮,壓縮目錄
-d 指定加壓位置
1.2例子
壓縮hosts
[[email protected] ~]# zip hosts.zip /etc/hosts
adding: etc/hosts (deflated 65%)
[[email protected] ~]# du -h hosts.zip
4.0K hosts.zip
解壓
[[email protected] ~]# unzip hosts.zip
Archive: hosts.zip
inflating: etc/hosts
3.gzip、bzip2、xz壓縮工具
3.1gzip、bzip2、xz的使用
[[email protected] test]# touch test01
[[email protected] test]# gzip test01
[[email protected] test]# ls
test01.gz
解壓
[[email protected] test]# gzip -d test01.gz
[[email protected] test]# ls
test01
只能對檔案進行壓縮,且壓縮後原始檔會消失,一般不適用
bzip2,xz這兩個工具可以通過新增引數-k來保留原始檔
bzip2
[[email protected] test]# touch test02
[[email protected] test]# bzip2 -k test02
test01.gz test02 test02.bz2
解壓
[[email protected] test]# rm -f test02
[[email protected] test]# ls
test01 test02.bz2
[[email protected] test]# bzip2 -d test02.bz2 -k
[[email protected] test]# ls
test01 test02 test02.bz2
xz
[[email protected] test]# xz -k test03
[[email protected] test]# ls
test01 test02 test02.bz2 test03 test03.xz
[[email protected] test]# rm -f test03
[[email protected] test]# xz -d test03.xz -k
[[email protected] test]# ls
test01 test02 test02.bz2 test03 test03.xz