文件的压缩打包

目录

  • 文件的压缩打包
  • 压缩包格式
  • 为什么使用压缩
  • Linux常用压缩格式及命令
  • 压缩命令-gzip
  • 压缩命令-zip
  • 压缩命令-tar
  • tar企业案例
  • 思维导图

压缩包格式

windows:
.zip
.tar
.tar.gz
.gz
.rar
.7z
.bz
.bz2
.xz

为什么使用压缩

  • 1.文件或目录太大,需要压缩传输
  • 2.服务安装包都需要解压

Linux常用压缩格式及命令

格式

Linux命令

.zip

zip

.gz

gzip

.tar

tar

.tar.gz

tar、gzip

压缩命令-gzip

# gzip命令的安装:(centOS7自带命令)
[root@localhost ~]# yum install -y gzip

# gzip命令用法:
gzip 普通文件名

# 选项
-r:递归压缩
(gzip递归压缩会自动对目录下个每个普通文件分别压缩,不可对上级目录进行压缩)

# 查看压缩包中文件的内容命令
zcat

# 解压命令
gzip -d 压缩包名



## 特性:
1.压缩文件后,源文件不存在
2.只能压缩文件,不能压缩目录
3.压缩后,压缩包的位置在源文件的目录下
4.压缩后可以直接查看文件内容zcat
5.一个压缩包中,只会有一个文件
6.解压后,压缩包没了,只剩源文件


# 示例:
[root@localhost~]# ll /cheshi003/toni/
total 20
-rw-r--r-- 1 root root 29 May  1 00:29 aaa1.txt.gz
-rw-r--r-- 1 root root 29 May  1 00:29 aaa2.txt.gz
-rw-r--r-- 1 root root 29 May  1 00:29 aaa3.txt.gz
-rw-r--r-- 1 root root 29 May  1 00:29 aaa4.txt.gz
-rw-r--r-- 1 root root 29 May  1 00:29 aaa5.txt.gz

[root@localhost~]# gzip -r /cheshi003/toni/
[root@localhost~]# ll /cheshi003/toni/
total 24
-rw-r--r-- 1 root root 33 May  1 00:57 aaa1.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:57 aaa2.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:58 aaa3.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:58 aaa4.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:59 aaa5.txt.gz


[root@localhost~]# file /cheshi003/toni/aaa1.txt.gz
/cheshi003/toni/aaa1.txt.gz: gzip compressed data, was "aaa1.txt", from Unix, last modified: Sun May  1 00:57:44 2022

[root@localhost~]# zcat /cheshi003/toni/aaa1.txt.gz 
bbb

# 解压/递归解压:
1)
[root@localhost~]# gzip -d /cheshi003/toni/aaa1.txt.gz 
[root@localhost~]# ll /cheshi003/toni/
total 24
-rw-r--r-- 1 root root  4 May  1 00:57 aaa1.txt
-rw-r--r-- 1 root root 33 May  1 00:57 aaa2.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:58 aaa3.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:58 aaa4.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:59 aaa5.txt.gz
-rw-r--r-- 1 root root 33 May  1 00:56 aaaa.txt.gz

2)
[root@localhost~]# gzip -dr /cheshi003/toni/
[root@localhost~]# ll /cheshi003/toni/
total 24
-rw-r--r-- 1 root root 4 May  1 00:57 aaa1.txt
-rw-r--r-- 1 root root 4 May  1 00:57 aaa2.txt
-rw-r--r-- 1 root root 4 May  1 00:58 aaa3.txt
-rw-r--r-- 1 root root 4 May  1 00:58 aaa4.txt
-rw-r--r-- 1 root root 4 May  1 00:59 aaa5.txt
-rw-r--r-- 1 root root 4 May  1 00:56 aaaa.txt

压缩命令-zip

# zip和unzip命令:
[root@localhost ~]# yum install -y zip
[root@localhost ~]# yum install -y unzip

# zip压缩命令:
zip 压缩包名 文件或目录
压缩目录需要加选项 -r 如果不加,压缩后,只有一个空目录,没有里面的文件

                    命令:      压缩包名           需要放进压缩包的文件
[root@localhost ~]# zip        txt.zip          1.txt 2.txt 3.txt

# 选项
-r:递归压缩,包括目录下的所有文件
[root@localhost~]# zip -r /ceshi002/ys.zip /ceshi001/ys

## 压缩并指定压缩包的存放位置:
[root@localhost ~]# zip /opt/zls.zip 1.txt 2.txt 3.txt 4.txt

## 特性:
1.压缩文件后,源文件存在
2.可以指定压缩后保存的路径
3.可以压缩目录,也可以压缩文件,也可以指定多个文件一起压缩
4.压缩目录需要加选项,如果不加,压缩后,只有一个空目录,没有里面的文件
5.解压后,压缩包不会消失,如果同一目录下出现同名文件则会询问是否要覆盖

### 示例:
[root@localhost~]# ll /ceshi001/ys
total 0
-rw-r--r-- 1 root root 0 Apr 30 21:50 file1
-rw-r--r-- 1 root root 0 Apr 30 21:50 file2
-rw-r--r-- 1 root root 0 Apr 30 21:50 file3
-rw-r--r-- 1 root root 0 Apr 30 21:50 file4
-rw-r--r-- 1 root root 0 Apr 30 21:50 file5

[root@localhost~]# zip -r /ceshi002/zzz.zip /ceshi001/ys
  adding: ceshi001/ys/ (stored 0%)
  adding: ceshi001/ys/file1 (stored 0%)
  adding: ceshi001/ys/file2 (stored 0%)
  adding: ceshi001/ys/file3 (stored 0%)
  adding: ceshi001/ys/file4 (stored 0%)
  adding: ceshi001/ys/file5 (stored 0%)

[root@localhost~]# ll /ceshi002
total 4
-rw-r--r-- 1 root root 984 May  1 02:36 zzz.zip

[root@localhost~]# file /ceshi002/zzz.zip 
/ceshi002/zzz.zip: Zip archive data, at least v1.0 to extract



# unzip解压缩命令:
unzip 压缩包名
unzip在默认情况下,是解压缩文件到当前工作目录,如果当前目录中存在和压缩文件中同名的文件,将提示用户

[root@localhost~]# unzip /ceshi002/zzz.zip 
Archive:  /ceshi002/zzz.zip
replace ceshi001/ys/file1? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
 extracting: ceshi001/ys/file1       
 extracting: ceshi001/ys/file2       
 extracting: ceshi001/ys/file3       
 extracting: ceshi001/ys/file4       
 extracting: ceshi001/ys/file5       

## unzip -l:查看压缩包里面都有哪些文件
   (无法看见压缩包里文件里的内容)

###示例:
[root@localhost~]# unzip -l /ceshi002/ys.zip 
Archive:  /ceshi002/ys.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-30-2022 21:53   ceshi001/ys/
        0  04-30-2022 21:50   ceshi001/ys/file1
        0  04-30-2022 21:50   ceshi001/ys/file2
        0  04-30-2022 21:50   ceshi001/ys/file3
        0  04-30-2022 21:50   ceshi001/ys/file4
        0  04-30-2022 21:50   ceshi001/ys/file5
---------                     -------
        0                     6 files


## unzip -d:指定解压路径
[root@localhost~]# unzip /ceshi002/zzz.zip  -d /ceshi002

### 示例:
[root@localhost~]# unzip /ceshi002/zzz.zip  -d /ceshi002
Archive:  /ceshi002/zzz.zip
   creating: /ceshi002/ceshi001/ys/
 extracting: /ceshi002/ceshi001/ys/file1  
 extracting: /ceshi002/ceshi001/ys/file2  
 extracting: /ceshi002/ceshi001/ys/file3  
 extracting: /ceshi002/ceshi001/ys/file4  
 extracting: /ceshi002/ceshi001/ys/file5  

[root@localhost~]# ll /ceshi002/ceshi001/ys
total 0
-rw-r--r-- 1 root root 0 Apr 30 21:50 file1
-rw-r--r-- 1 root root 0 Apr 30 21:50 file2
-rw-r--r-- 1 root root 0 Apr 30 21:50 file3
-rw-r--r-- 1 root root 0 Apr 30 21:50 file4
-rw-r--r-- 1 root root 0 Apr 30 21:50 file5





### 过程演示:
[root@localhost~]# tree /ceshi001
/ceshi001
├── ys
│   ├── file1
│   ├── file2
│   ├── file3
│   ├── file4
│   └── file5
└── ys02
    ├── aaa.txt
    └── abc.txt

2 directories, 7 files
[root@localhost~]# mkdir /ceshi002

[root@localhost~]# zip -r /ceshi002/zzz.zip /ceshi001/ys
  adding: ceshi001/ys/ (stored 0%)
  adding: ceshi001/ys/file1 (stored 0%)
  adding: ceshi001/ys/file2 (stored 0%)
  adding: ceshi001/ys/file3 (stored 0%)
  adding: ceshi001/ys/file4 (stored 0%)
  adding: ceshi001/ys/file5 (stored 0%)

[root@localhost~]# ll /ceshi002
total 4
-rw-r--r-- 1 root root 984 May  1 02:36 zzz.zip

[root@localhost~]# file /ceshi002/zzz.zip 
/ceshi002/zzz.zip: Zip archive data, at least v1.0 to extract

[root@localhost~]# unzip -l /ceshi002/zzz.zip
Archive:  /ceshi002/zzz.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-30-2022 21:53   ceshi001/ys/
        0  04-30-2022 21:50   ceshi001/ys/file1
        0  04-30-2022 21:50   ceshi001/ys/file2
        0  04-30-2022 21:50   ceshi001/ys/file3
        0  04-30-2022 21:50   ceshi001/ys/file4
        0  04-30-2022 21:50   ceshi001/ys/file5
---------                     -------
        0                     6 files


[root@localhost~]# unzip /ceshi002/zzz.zip  -d /ceshi002
Archive:  /ceshi002/zzz.zip
   creating: /ceshi002/ceshi001/ys/
 extracting: /ceshi002/ceshi001/ys/file1  
 extracting: /ceshi002/ceshi001/ys/file2  
 extracting: /ceshi002/ceshi001/ys/file3  
 extracting: /ceshi002/ceshi001/ys/file4  
 extracting: /ceshi002/ceshi001/ys/file5  


[root@localhost~]# ll /ceshi002/ceshi001/ys
total 0
-rw-r--r-- 1 root root 0 Apr 30 21:50 file1
-rw-r--r-- 1 root root 0 Apr 30 21:50 file2
-rw-r--r-- 1 root root 0 Apr 30 21:50 file3
-rw-r--r-- 1 root root 0 Apr 30 21:50 file4
-rw-r--r-- 1 root root 0 Apr 30 21:50 file5

压缩命令-tar

# tar命令本身是归档

## 选项:(正常使用不需要-、 特殊情况比如-C就会需要)
c:归档
f:指定包名
z:使用gzip把归档文件压缩(.tar.gz)
v:显示压缩/解压的过程
x:解压归档文件(默认解压到当前所在目录)
C(大写):指定解压的位置(路径)
t:查看压缩包里的文件都有哪些(要配合f来指定查看的压缩包)
j:使用bzip2压缩文件
J:压缩成.xz包
X:排除指定的文件
h:打包软链接(如果软链接文件是相对路径,那么不加h打包出来的文件会失效)
P(大写):压缩时带绝对路径,解压时按绝对路径解压(这个选项很危险,要注意解压时绝对路径同名文件不询问直接覆盖的问题)
--exclude:排除指定文件
--hard-dereference:打包硬链接文件



## 特性
1.压缩文件后,源文件存在
2.目录和文件都可以压缩
3.压缩后,压缩包的位置可以指定任意目录
[root@localhost ~]# tar zcf /usr/local/lx.tar.gz /etc /opt /tmp
4.可以查看压缩包里有哪些文件,但是查看不了文件内容
[root@localhost ~]# tar tf /usr/local/lx.tar.gz
5.一个压缩包中,可以有多个文件或目录
6.解压后,压缩包还在,源文件也可以随意指定路径 -C
7.使用zcf压缩,zxf解压
使用jcf压缩,jxf解压
使用Jcf压缩,Jxf解压
万能解压命令:xf


## 注意:
1.tar命令在解压开文件时,如果有文件名冲突,则不会询问,直接覆盖
2.tar命令,在打包时,会自动删除绝对路径的"/"
3.以后打包,尽量使用相对路径,cd到需要打包目录或文件的上级目录
[root@localhost ~]# cd /
[root@localhost /]# tar zcf /usr/local/src/opt.tgz opt/


## zcf举例:
[root@localhost~]# tree /ceshi003
/ceshi003
├── AA1
│   ├── a1.txt
│   ├── a2.txt
│   ├── a3.txt
│   ├── a4.txt
│   └── a5.txt
└── AA2

[root@localhost~]# tar zcf /ceshi004/ttt.tar.gz /ceshi003/AA1
tar: Removing leading `/' from member names
# 这里是警告在打包的过程中 tar命令自动删除绝对路径的"/"
所以:
[root@localhost~]# cd /
[root@localhost/]# tar zcf /ceshi004/ttt.tar.gz ceshi003/AA1

[root@localhost/]# tar tf /ceshi004/ttt.tar.gz 
ceshi003/AA1/
ceshi003/AA1/a1.txt
ceshi003/AA1/a2.txt
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt

相当于是两个步骤:
1)先归档
[root@localhost/]# tar cf /ceshi004/ttt.tar ceshi003/AA1
2)再使用gzip压缩
[root@localhost/]# gzip /ceshi004/ttt.tar


## zxf举例:
## C举例:这个时候C需要加上-即:-C
[root@localhost/]# tar zxf /ceshi004/ttt.tar.gz -C /ceshi004/B1/
[root@localhost/]# ll /ceshi004/B1/ceshi003/AA1
total 0
-rw-r--r-- 1 root root 0 May  1 03:22 a1.txt
-rw-r--r-- 1 root root 0 May  1 03:22 a2.txt
-rw-r--r-- 1 root root 0 May  1 03:22 a3.txt
-rw-r--r-- 1 root root 0 May  1 03:22 a4.txt
-rw-r--r-- 1 root root 0 May  1 03:22 a5.txt


## 万能解压命令:xf
[root@localhost/]# tar xf /ceshi004/ttt333.tar.gz -C /ceshi004/B2
[root@localhost/]# ll /ceshi004/B2/ceshi003/AA1
total 0
-rw-r--r-- 1 root root 0 May  1 03:22 a3.txt
-rw-r--r-- 1 root root 0 May  1 03:22 a4.txt
-rw-r--r-- 1 root root 0 May  1 03:22 a5.txt



## X举例:
[root@localhost~]# tree /ceshi003
/ceshi003
├── AA1
│   ├── 1.log
│   ├── 2.log
│   ├── a1.txt
│   ├── a2.txt
│   ├── a3.txt
│   ├── a4.txt
│   └── a5.txt
└── AA2


[root@localhost/]# cat /ceshi001/paichu.txt 
a1.txt
a2.txt


[root@localhost~]# vim /ceshi001/paichu2
[root@localhost~]# cat /ceshi001/paichu2
*.log

#### 1)
[root@localhost/]# tar zcf /ceshi004/ttt222.tar.gz -X /ceshi001/paichu.txt ceshi003/AA1 
[root@localhost/]# tar tf /ceshi004/ttt222.tar.gz 
ceshi003/AA1/
ceshi003/AA1/1.log
ceshi003/AA1/2.log
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt

#### 2)通配符排除选项
[root@localhost~]# cd /
[root@localhost/]# tar zcf /ceshi004/ttt444.tar.gz -X /ceshi001/paichu2 ceshi003/AA1 
[root@localhost/]# tar tf ceshi004/ttt444.tar.gz 
ceshi003/AA1/
ceshi003/AA1/a1.txt
ceshi003/AA1/a2.txt
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt


## --exclude举例:
[root@localhost/]# tar zcf /ceshi004/ttt333.tar.gz --exclude=a1.txt --exclude=a2.txt ceshi003/AA1 
[root@localhost/]# tar tf /ceshi004/ttt333.tar.gz 
ceshi003/AA1/
ceshi003/AA1/1.log
ceshi003/AA1/2.log
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt

## zhcf 举例:
[root@localhost/]# ll /work
lrwxrwxrwx. 1 root root 14 Mar 29 21:23 kobe.jpg -> /root/kobe.jpg

[root@localhost/work]# tar zchf /ceshi003/AA2/photo.tar.gz kobe.jpg 
[root@localhost/work]# tar tf /ceshi003/AA2/photo.tar.gz 
kobe.jpg
[root@localhost/work]# tar xf /ceshi003/AA2/photo.tar.gz -C /ceshi001/ys02/


[root@localhost/work]# ll -i /ceshi001/ys02/kobe.jpg 
16777657 -rw-r--r-- 1 root root 106060 Jan 23  2021 /ceshi001/ys02/kobe.jpg

[root@localhost/work]# ll -i /root/kobe.jpg 
33578027 -rw-r--r--. 1 root root 106060 Jan 23  2021 /root/kobe.jpg


## zcPf和xPf举例:
1)压缩的时候不会提示:tar: Removing leading `/' from member names的警告

[root@localhost/work]# tar zcPf /ceshi002/photo2.tai.gz /work/passwd.txt 
[root@localhost/work]# tar tf /ceshi002/photo2.tai.gz 
tar: Removing leading `/' from member names
/work/passwd.txt

2)解压缩的时候也要是加上P的选项才可以解压,
但是要注意按照绝对路径解压,会覆盖掉源文件修改后的内容
[root@localhost/work]# tar xPf /ceshi002/photo2.tai.gz


## --hard-dereference:打包硬链接文件举例:

[root@localhost~]# cat /work2/ylj.txt 
B:A:123
C:F:223
D:n:421

[root@localhost~]# cat /root/paixu1.txt 
B:A:123
C:F:223
D:n:421

[root@localhost/work2]# tar zcf /ceshi004/YLJ.tai.gz --hard-dereference ylj.txt
[root@localhost/ceshi004]# tar tf /ceshi004/YLJ.tai.gz
ylj.txt
[root@localhost/ceshi004]#  tar xf /ceshi004/YLJ.tai.gz 
[root@localhost/ceshi004]# cat ylj.txt 
B:A:123
C:F:223
D:n:421



## 配合find命令使用举例:
1)
[root@localhost/ceshi003/AA1]# find -name '*.log'| xargs tar zcf /ceshi004/alllog.tar.gz
2)
[root@localhost/ceshi003/AA1]# tar zcf /ceshi004/alllog.tar.gz `find -name '*.log'`
3)
[root@localhost/ceshi003/AA1]# tar zcf /ceshi004/alllog2.tar.gz $(find -name '*.log')

tar企业案例

1.数据库物理备份

# 基础环境准备
[root@localhost ~]# yum install mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mkdir /backup


# 案例1 mysql物理备份及恢复
[root@localhost ~]# tar cJf /backup/mysql.tar.xz /var/lib/mysql
[root@localhost ~]# tar xf /backup/mysql.tar.xz -C /


# 案例2 mysql物理备份及恢复
[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

2.传输海量小文件

## 文件传输(如果etc下小文件特别多,很占用磁盘IO)
[root@localhost lib]# cp -a /etc /tmp

## 以下方式减少小文件的传输
[root@localhost lib]# tar czf - /etc | tar xzf - -C /tmp

3.网络传输海量小文件

#常规方法
[root@localhost ~]# scp -r /etc root@10.0.0.200:/tmp


#建议方法:
#接收B主机, 需要监听端口
[root@hostB ~]# systemctl stop firewalld.service
[root@hostB ~]# nc -l 8888 |tar xzf - -C /tmp
#发送方A主机
[root@hostA ~]# tar -czf - /etc | nc 10.0.0.200 8888
tar: Removing leading `/' from member names

思维导图

centos对xz后缀的文件如何解压 centos7解压gz文件_hive