Debian8系統文件如何壓縮與歸檔
Debian8系統文件如何壓縮與歸檔
debian8系(xi)統文(wen)件如何壓縮(suo)與歸檔?本教程(cheng)以debian8系(xi)統為例
本(ben)配置適用于debian8,9版本(ben)
1.tar
1.1命令與參數
用法:tar [參(can)數] [壓(ya)縮文(wen)件名] [要壓(ya)縮的(de)文(wen)件]
使用(yong)參數時(shi),可以不使用(yong)
-c create,創建文件
-x extract,提取(qu)解壓還原(yuan)文件
-v 顯示(shi)執行顯示(shi)過程
-f 指定備份文件
-t 列(lie)出備份文件(jian)內(nei)容,不解包查看包中的內(nei)容
-C 指定解包位置
-z --gzip,以gzip方式壓(ya)縮(suo) 擴展名(ming):tar.gz
-j 以(yi)bz2方式壓縮 擴(kuo)展名(ming):tar.bz2
-J 以(yi)xz方(fang)式壓縮 擴展名(ming):tar.xz
1.2歸檔例子
打(da)包/etc/hosts文件
[root@debian ~]# tar cvf hosts.tar /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
[root@debian ~]# ll hosts.tar
-rw-r--r--. 1 root root 10240 Sep 14 20:16 hosts.tar
在使用絕對路(lu)徑進行壓縮(suo)時(shi),將默認從文件(jian)名(ming)中刪除該路(lu)徑中前(qian)面(mian)的/符號,這樣解壓時(shi),會直接解壓到當(dang)前(qian)目錄,不然會覆蓋掉原(yuan)系統中的路(lu)徑文件(jian)。
指定路徑解包
[root@debian ~]# tar xvf hosts.tar -C /opt/
etc/hosts
[root@debian ~]# ll /opt/etc/
total 4
-rw-r--r--. 1 root root 158 Jun 7 2013 hosts
打包多個文件
[root@debian ~]# 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
[root@debian ~]# ll all.tar
-rw-r--r--. 1 root root 10240 Sep 14 20:25 all.tar
不(bu)解包文件的情況下,查看(kan)包有什么文件
[root@debian ~]# 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
打包多目錄
[root@debian ~]# tar cvf dir.tar /etc/ /var/
[root@debian ~]# ll dir.tar
-rw-r--r--. 1 root root 149657600 Sep 14 20:29 dir.tar
1.3打包加壓縮
例(li)1:以gzip進行壓縮
[root@debian ~]# tar zcvf hosts.tar.gz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
對比不壓縮的包大小
[root@debian ~]# du -h hosts.*
12K hosts.tar
4.0K hosts.tar.gz
解壓
[root@debian ~]# tar zxvf hosts.tar.gz
etc/hosts
例2:以(yi)xz方式(shi)壓縮
[root@debian ~]# tar Jcvf hosts.tar.xz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解壓
[root@debian ~]# tar Jxvf hosts.tar.xz
etc/hosts
1.4對比三(san)種打包方式的(de)大小與速度
對比速度
[root@debian ~]# time tar zcvf etc.tar.gz /etc/
real 0m0.868s
user 0m0.740s
sys 0m0.099s
[root@debian ~]# time tar jcvf etc.tar.bz2 /etc/
real 0m2.037s
user 0m1.933s
sys 0m0.078s
[root@debian ~]# time tar Jcvf etc.tar.xz /etc/
real 0m9.828s
user 0m9.577s
sys 0m0.193s
time命令輸入(ru)解(jie)釋(shi)
real: 表(biao)示程序整(zheng)個的(de)運(yun)行(xing)耗時(shi)。可(ke)以(yi)理解為(wei)命令運(yun)行(xing)開始時(shi)刻你看了一下手(shou)表(biao),命令運(yun)行(xing)結束時(shi),你又看了一下手(shou)表(biao),兩(liang)次時(shi)間的(de)差值就是本次real 代(dai)表(biao)的(de)值
user:這個時(shi)間(jian)(jian)代(dai)表的(de)是命令運(yun)行在用戶態的(de)cpu時(shi)間(jian)(jian)
sys: 這(zhe)個時間(jian)代表的(de)命(ming)令是(shi)運行在核心態(tai)的(de)cpu時間(jian)
%cpu_usage = (user_time sys_time)/real_time * 100%
我們這里只(zhi)看速度的話,tar.gz最快,bz2次之(zhi)。
對比大小
[root@debian ~]# du -sh /etc/
22M /etc/
[root@debian ~]# du -h etc*
6.0M etc.tar.bz2
6.9M etc.tar.gz
5.0M etc.tar.xz
壓縮時間越久(jiu),效率就越高。
2.zip
2.1命令參數
需要安裝
[root@debian ~]# yum install zip unzip -y
zip 壓縮命令
unzip 解壓命(ming)令
參數:
-r 遞歸壓(ya)縮(suo)(suo),壓(ya)縮(suo)(suo)目(mu)錄
-d 指定加壓位置
2.1例子
壓縮hosts
[root@debian ~]# zip hosts.zip /etc/hosts
adding: etc/hosts (deflated 65%)
[root@debian ~]# du -h hosts.zip
4.0K hosts.zip
解壓
[root@debian ~]# unzip hosts.zip
Archive: hosts.zip
inflating: etc/hosts
3.gzip、bzip2、xz壓(ya)縮工具
3.1gzip、bzip2、xz的使用
[root@debian test]# touch test01
[root@debian test]# gzip test01
[root@debian test]# ls
test01.gz
解壓
[root@debian test]# gzip -d test01.gz
[root@debian test]# ls
test01
只能對文件進行(xing)壓縮(suo),且壓縮(suo)后源(yuan)文件會消(xiao)失,一般(ban)不適用
bzip2,xz這兩個(ge)工具(ju)可(ke)以通過添加參數-k來保(bao)留源文件(jian)
bzip2
[root@debian test]# touch test02
[root@debian test]# bzip2 -k test02
test01.gz test02 test02.bz2
解壓
[root@debian test]# rm -f test02
[root@debian test]# ls
test01 test02.bz2
[root@debian test]# bzip2 -d test02.bz2 -k
[root@debian test]# ls
test01 test02 test02.bz2
xz
[root@debian test]# xz -k test03
[root@debian test]# ls
test01 test02 test02.bz2 test03 test03.xz
[root@debian test]# rm -f test03
[root@debian test]# xz -d test03.xz -k
[root@debian test]# ls
test01 test02 test02.bz2 test03 test03.xz

