day15-Linux壓縮、打包

文件打包與壓縮

1.什么是文件壓縮
將多個(gè)文件或目錄合并成為一個(gè)特殊的文件
2.為什么要對(duì)文件進(jìn)行壓縮
當(dāng)我們?cè)趥鬏敶罅康奈募r(shí),通常都會(huì)選擇該文件進(jìn)行壓縮
首先:壓縮后的文件會(huì)比壓縮前的文件小
其次:多個(gè)文件傳輸很慢,但單個(gè)文件傳輸會(huì)很快,同時(shí)還能節(jié)省網(wǎng)絡(luò)的消耗
3.windows的壓縮包與linux的壓縮包能否互通
在windows系統(tǒng)下,我們接觸最多的壓縮格式是rar或zip,但在linux上使用最多的壓縮格式是zip和tar.gz,當(dāng)然不用擔(dān)心,linux上的壓縮格式放在windows系統(tǒng)下都是可以正常打開的。
PS:linux不支持windows下的RAR格式的壓縮文件。windows和linux互通通常選擇zip

linux下壓縮包有哪些常見的類型

image.png

1.gzip打包與壓縮
gzip打包與壓縮,僅對(duì)文件有效

yum install gzip -y   #需要安裝gzip工具
[root@oldboy ~]# gzip filename       
[root@oldboy ~]# zcat filename.gz
[root@oldboy ~]# gzip -d filename.gz     
使用場(chǎng)景:當(dāng)需要某個(gè)文件快速關(guān)閉和快速啟用

2.zip打包與壓縮
使用zip命令可以對(duì)文件進(jìn)行壓縮打包,解壓則需要使用unzip命令
-l : 查看包內(nèi)容
-T: 查看zip壓縮包 是否完整
-r: 遞歸打包目錄
-t: 檢測(cè)文件是否都o(jì)k

默認(rèn)情況下沒有zip 和unzip工具,需要進(jìn)行安裝
[root@oldboy ~]# yum install zip unzip -y
壓縮文件為zip包
[root@oldboy ~]# zip filename.zip filename 
[root@oldboy ~]# unzip -l filename.zip 
壓縮目錄為zip包
[root@oldboy ~]# zip -r dir.zip dir/
[root@oldboy ~]# zip -T filename.zip 
不解壓查看壓縮包的內(nèi)容
[root@oldboy ~]# unzip -l filename.zip
[root@oldboy ~]# unzip -t filename.zip 
解壓到當(dāng)期目錄
[root@oldboy ~]# unzip filename.zip 
解壓zip包內(nèi)容至/opt目錄
[root@oldboy ~]# unzip filename.zip -d /opt/   #注意-d的使用位置

3.tar壓縮打包
tar是linux下最常用的壓縮與解壓縮,支持文件和目錄的壓縮歸檔
語法
c:創(chuàng)建新的歸檔文件
x:對(duì)歸檔文件解包
t: 列出歸檔文件里的文件列表
f: 指定包文件名,多參數(shù)f寫最后
z: 使用gzip壓縮歸檔后的文件(.tar.gz)
j : 使用bzip2壓縮歸檔后的文件(.tar.bz2)
J: 使用xz壓縮歸檔后的文件 (.tar.xz)
C:指定解壓目錄位置
X: 排除多個(gè)文件(寫入需要排除的文件名稱)
h: 打包軟鏈接
--exclude 在打包的時(shí)候?qū)懭胄枰懦募蚰夸?br> 常用打包與壓縮組合
cjf:打包tar.bz格式
cJf:打包tar.xz格式 (使用較少)
zxf:解壓tar.gz格式
jxf: 解壓tar.bz格式
czf:打包tar.gz格式
tf: 查看壓縮包內(nèi)容
xf: 自動(dòng)選擇解壓模式

打包
[root@oldboy ~]# tar czf test.tar.gz test/ test2/
[root@oldboy ~]# tar cjf test.tar.bz2 dir/
[root@oldboy ~]# tar cJf test.tar.xz ops/ ops1/
查看包內(nèi)容
[root@oldboy ~]# tar tf test.tar.gz 
[root@oldboy ~]# tar tf test.tar.bz2 
[root@oldboy ~]# tar tf test.tar.xz
解壓包
[root@oldboy ~]# tar xf test.tar.gz
[root@oldboy ~]# tar xf test.tar.bz2
[root@oldboy ~]# tar xf test.tar.xz
[root@oldboy ~]# tar xf test.tar.gz -C /tmp/    #解壓到指定目錄
打包/tmp下的所有文件
[root@oldboy ~]# find /tmp/ -type f | xargs tar czf tmp.tar.gz
[root@oldboy ~]# tar czf tmp.tar.gz  $(find /tmp/ -type f)
打包鏈接文件,打包鏈接文件的真實(shí)文件
[root@oldboy ~]# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 Jul 24 15:37 /etc/rc.local -> rc.d/rc.local
[root@oldboy ~]# tar czfh local.tar.gz /etc/rc.local 
排除操作
[root@oldboy ~]# tar czf etc.tar.gz /etc/ --exclude=etc/services
[root@oldboy ~]# tar czf etc.tar.gz /etc/ --exclude=etc/passwd --exclude=etc/shadow
將需要排除的文件寫入文件中
[root@oldboy ~]# cat >>pc.txt<<EOF
> etc/gshadow
> etc/passwd
> etc/gshadow-
> etc/passwd-
> etc/shadow
> etc/shadow-
> EOF
[root@oldboy opt]# tar czxf pc.txt etc.tar.gz /etc/
打包目錄,命令以ip地址+當(dāng)前時(shí)間方式的壓縮包
[root@oldboy ~]# tar zcf $(ifconfig ens32|awk 'NR==2{print $2}')_$(date +%F)_etc.tar.gz /etc
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容