1.文件壓縮的含義
將多個文件或目錄合并成為一個特殊的文件。
2.對文件進行壓縮的好處
減小文件的體積
加快資源的傳輸
節(jié)省網(wǎng)絡(luò)的帶寬
3.Windows的壓縮包與Linux的壓縮包能否互通
windows壓縮包類型: rar zip 其實支持很多類型的壓縮
linux壓縮包類型: zip tar.gz ....
windows與linux互通 建議使用: zip
4.Linux下壓縮包有哪些常見的類型
格式? ? ? ? ? ? 壓縮工具
.zip? ? ? ? ? ? ? zip壓縮工具 (必須要會使用的)
.gz? ? ? ? ? ? ? gzip壓縮工具,只能壓縮文件,會刪除原文件(通常配合tar使用)
.bz2? ? ? ? ? ? bzip2壓縮工具,只能壓縮文件,會刪除原文件(通常配合tar使用)
.tar.gz? ? ? ? 先使用tar命令歸檔打包,然后使用gzip壓縮 (必須會的)
.tar.bz2? ? ? 先使用tar命令歸檔打包,然后使用bzip壓縮 (順帶就會)
5.linux gzip工具使用
# 1. gzip 打包與壓縮 ,僅對文件有效.
# gzip filename? ? ? ? ? ? ? 打包
# gzip -d filename.gz? ? ? 解包
# zcat filename.gz? ? ? ? ? 查看包內(nèi)文件的內(nèi)容
[root@xuliangwei ~]# yum install gzip -y
[root@xuliangwei ~]# gzip file? ? ? ? ? #對文件進行壓縮
[root@xuliangwei ~]# zcat file.gz? ? ? #查看gz壓縮后的文件
[root@xuliangwei ~]# gzip -d file.gz? #解壓gzip的壓縮包
#使用場景:當(dāng)需要讓某個文件快速關(guān)閉和快速啟用.
[root@xuliangwei ~]# gzip CentOS-Vault.repo -->CentOS-Vault.repo.gz
[root@xuliangwei ~]# zcat CentOS-Vault.repo.gz --> 查看不想解壓的壓縮包文件內(nèi)容
6.linux zip工具使用
# 默認情況下沒有 zip 和 unzip 工具,需要進行安裝
[root@xuliangwei ~]# yum install zip unzip -y
#1. 壓縮文件為zip包
[root@xuliangwei ~]# zip filename.zip filename
[root@xuliangwei ~]# unzip -l filename.zip? #查看包內(nèi)容
#2. 壓縮目錄為zip包
[root@xuliangwei ~]# zip -r dir.zip dir/
#3. 查看zip壓縮包是否是完整的
[root@xuliangwei ~]# zip -T filename.zip
test of filename.zip OK
#4. 不解壓壓縮包查看壓縮包中的內(nèi)容
[root@xuliangwei ~]# unzip -l filename.zip
[root@xuliangwei ~]# unzip -t filename.zip? #檢測文件是否都ok
#5. 解壓zip文件包, 默認解壓至當(dāng)前目錄
[root@xuliangwei ~]# unzip filename.zip
#6. 解壓zip內(nèi)容至/opt目錄
[root@xuliangwei ~]# unzip filename.zip -d /opt/
#打包? ? ? ? ? zip -r /tmp/test.zip file dir/
#解包? ? ? ? ? unzip tt.zip
? ? ? ? ? ? ? ? ? ? unzip tt.zip -d /opt
7.linux tar工具使用
tar 是linux下最常用的壓縮與解壓縮, 支持文件和目錄的壓縮歸檔
#語法:tar [-zjxcvfpP] filename
c? ? #創(chuàng)建新的歸檔文件
x? ? #對歸檔文件解包
t? ? #列出歸檔文件里的文件列表
f? ? #指定包文件名,多參數(shù)f寫最后
z? ? #使用gzip壓縮歸檔后的文件(.tar.gz)
j? ? #使用bzip2壓縮歸檔后的文件(.tar.bz2)
J? ? #使用xz壓縮歸檔后的文件(tar.xz)
C? #指定解壓目錄位置
X? ? #排除多個文件(寫入需要排除的文件名稱)
h? ? #打包軟鏈接
--exclude? ? #在打包的時候?qū)懭胄枰懦募蚰夸?/p>
#常用打包與壓縮組合
cjf? ? #打包tar.bz格式
cJf? #打包tar.xz格式 使用田少,不考慮
zxf? #解壓tar.gz格式
jxf? ? #解壓tar.bz格式
---------------------------------------
czf? ? #打包tar.gz格式 (*)
tf? ? ? #查看壓縮包內(nèi)容
xf? ? #自動選擇解壓模式 (*)
1.將文件或目錄進行打包壓縮
#打包
tar czf test.tar.gz test/ test2/? ?#以gzip方式壓縮
tar cjf test.tar.bz2 dir.txt dir/? ? #以bz2方式壓縮
#查看包內(nèi)容
tar tf test.tar.gz
tar tf test.tar.bz2
tar tf test.tar.xz
#解壓
tar xf test.tar.gz
tar xf test.tar.bz2
tar xf test.tar.xz
tar xf root.tar.gz -C /tmp/? #解壓至指定目錄
#打包/tmp下所有文件
find tmp/ -type f | xargs tar czf tmp.tar.gz
tar czf tmp.tar.gz $(find /tmp/ -type f)
#3.打包鏈接文件,打包鏈接文件的真實文件
[root@xuliangwei /]# tar czhf local.tar.gz etc/rc.local
#4.排除操作
tar czf etc.tar.gz --exclude=etc/services? /etc/
tar czf etc.tar.gz? --exclude=etc/passwd --exclude=etc/shadow /etc/
#5.將需要排除的文件寫入文件中
[root@oldboyedu opt]# cat pc.txt
etc/gshadow
etc/gshadowetc/
passwd
etc/passwdetc/
shadowetc/
shadow
etc/security/opasswd
etc/pam.d/passwd
[root@oldboyedu opt]# tar czXf pc.txt etc.tar.gz /etc/
#1.環(huán)境準備
[root@xuliangwei ~]# yum install mariadb-server
[root@xuliangwei ~]# systemctl start mariadb
[root@xuliangwei ~]# mkdir /backup
#案例1.mysql備份及恢復(fù)
[root@xuliangwei ~]# tar cJf /backup/mysql.tar.xz /var/lib/mysql
[root@xuliangwei ~]# tar xf /backup/mysql.tar.xz -C /
#案例2 mysql備份及恢復(fù)
[root@xuliangwei ~]# cd /var/lib/mysql
[root@xuliangwei mysql]# tar cJf /backup/mysql.tar.xz *
[root@xuliangwei mysql]# tar tf /backup/mysql.tar.xz
[root@xuliangwei mysql]# tar xf /backup/mysql.tar.xz -C /var/lib/mysql