1.什么是文件壓縮
將多個文件或目錄合并成為一個特殊的文件。
2.為什么要對文件進行壓縮
(1)減小文件的體積
(2)加快資源的傳輸
(3)節(jié)省網(wǎng)絡(luò)的帶寬
3.Windows的壓縮包與Linux的壓縮包能否互通?
(1)windows:支持 rar zip 很多類型的壓縮
(2)linux:支持 zip tar.gz 等類型的壓縮
ps: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打包與壓縮 ,僅對文件有效.
(1)gzip filename 打包
(2) gzip -d filename.gz 解包
(3) zcat filename.gz 查看包內(nèi)文件的內(nèi)容
[root@localhost ~]# yum install gzip -y
[root@localhost ~]# gzip file (對文件進行壓縮)
[root@localhost ~]# zcat file.gz ( 查看gz壓縮后的文件)
[root@localhost ~]# gzip -d file.gz(解壓gzip的壓縮包)
使用場景:當需要讓某個文件快速關(guān)閉和快速啟用.
[root@localhost ~]# gzip CentOS-Vault.repo -->CentOS-Vault.repo.gz
[root@localhost~]# zcat CentOS-Vault.repo.gz --> 查看不想解壓的壓
6.Linux zip工具使用
** 默認情況下沒有zip和unzip工具,需要進行安裝**
[root@localhost~]# yum install zip unzip -y
(1).壓縮文件為zip包
[root@localhost~]# zip filename.zip filename
[root@localhost~]# unzip -l filename.zip #查看包內(nèi)
容
(2.)壓縮目錄為zip包
[root@localhost~]# zip -r dir.zip dir/
(3).查看zip壓縮包是否是完整的
[root@localhost~]# zip -T filename.zip
test of filename.zip OK
(4).不解壓壓縮查看壓縮包中的內(nèi)容
[root@localhost ~]# unzip -l filename.zip
[root@localhost ~]# unzip -t filename.zip (檢測文件是
否都ok)
(5).解壓zip文件包, 默認解壓至當前目錄
[root@localhost~]# unzip filename.zip
(6).解壓zip內(nèi)容至/opt目錄
[root@localhost ~]# 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ū)懭胄枰懦募蚰夸?
常用打包與壓縮組合
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/ (解壓至指定目錄)
(1)打包/tmp下所有文件
find tmp/ -type f | xargs tar czf tmp.tar.gz
tar czf tmp.tar.gz $(find /tmp/ -type f)
(2).打包鏈接文件,打包鏈接文件的真實文件
[root@xuliangwei /]# tar czfh local.tar.gz
etc/rc.local
(3).排除操作
tar czf etc.tar.gz /etc/ --exclude=etc/services
tar czf etc.tar.gz /etc/ --exclude=etc/passwd --
exclude=etc/shadow
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@localhost ~]# yum install mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost~]# mkdir /backup
案例1.mysql備份及恢復(fù)
[root@localhost ~]# tar cJf /backup/mysql.tar.xz
/var/lib/mysql
[root@localhost ~]# tar xf /backup/mysql.tar.xz -C /
案例2 mysql備份及恢復(fù)
[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]# tar cJf /backup/mysql.tar.xz
*
[root@localhost mysql]# tar tf /backup/mysql.tar.xz
[root@localhost mysql]# tar xf /backup/mysql.tar.xz -
C /var/lib/mysql