⽂件归档: 把⽂件通过特定形式保存/存储或者进⾏传递/数据备份。
⽂件压缩包
1、⽂件打包 : 将多个⽂件进⾏存储/存储为⼀个⽂件
⽂件打包格式: tar格式
2、⽂件压缩 :将⽂件通过压缩算法进⾏压缩为⼀个精简⽂件--可以解压缩-数据还原|占⽤空间⼩
⽂件压缩格式:Linux : bz2/gz
3、⽂件打包带压缩
⽂件打包带压缩格式 windows常⽤ zip/7z | linux: tar.bzip2/tar.gz
windows 打包/压缩软件 :winrar/好压
在windows中打包/压缩软件软件 通常直接⼀步到位 : 打包带压缩
在linux中⽂件打包/⽂件压缩 :可以分别单独管
1、文件压缩
在linux中,常⻅⽂件压缩格式 bz2 / gz
在linux中,常⻅⽂件压缩⼯具 bzip2 / gzip
# 使⽤bzip2/bunzip2⽅式⽅式进⾏⽂件压缩/解压
[root@localhost test01]# bzip2 a01.txt
[root@localhost test01]# ls
a01.txt.bz2
[root@localhost test01]# bunzip2 a01.txt
bunzip2: Can't open input file a01.txt: No such file or directory.
[root@localhost test01]# bunzip2 a01.txt.bz2
# 使⽤gzip/gunzip⽅式⽅式进⾏⽂件压缩/解压
[root@localhost test01]# gzip a01.txt
[root@localhost test01]# ls
a01.txt.gz
[root@localhost test01]# gunzip a01.txt.gz
[root@localhost test01]# ls
a01.txt
#===============================
dd ----- ⽂件创建|数据传输
if = input file
of = output file
bs = block
size 块⼤⼩
count = 数量
[root@localhost test01]# dd if=/dev/zero of=testzip bs=1M count=100
记录了100+0 的读入
记录了100+0 的写出
104857600字节(105 MB)已复制,0.379093 秒,277 MB/秒
[root@localhost test01]# ls
a01.txt testzip
[root@localhost test01]# ls -lh
总用量 100M
-rw-r--r--. 1 root root 0 7月 30 19:28 a01.txt
-rw-r--r--. 1 root root 100M 7月 30 19:34 testzip
[root@localhost test01]# gzip testzip #用gzip压缩
[root@localhost test01]# ls -lh
总用量 100K
-rw-r--r--. 1 root root 0 7月 30 19:28 a01.txt
-rw-r--r--. 1 root root 100K 7月 30 19:34 testzip.gz #gzip压缩压缩后从100M 压缩到了 100k
[root@localhost test01]# gunzip testzip.gz
[root@localhost test01]# ls
a01.txt testzip
[root@localhost test01]# bzip2 testzip
ls[root@localhost test01]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root 0 7月 30 19:28 a01.txt
-rw-r--r--. 1 root root 113 7月 30 19:34 testzip.bz2 # #bzip2压缩压缩后从100M 压缩到了 100k
[root@localhost test01]# bzip2 -d testzip.bz2 #用bzip2 -d 解压缩。也可以用bunzip2
[root@localhost test01]# ls
a01.txt testzip
bzip2 压缩/解压缩过程中保留原⽂件
[root@localhost test01]# bzip2 -k a01.txt #压缩过程中保留原文件
[root@localhost test01]# ls
a01.txt a01.txt.bz2 testzip #原文件还在
[root@localhost test01]# rm -rf a01.txt #先删除a01.txt
[root@localhost test01]# ls
a01.txt.bz2 testzip
[root@localhost test01]# bzip2 -d -k a01.txt.bz2 #解压缩过程中保留原文件
[root@localhost test01]# ls
a01.txt a01.txt.bz2 testzip # 解压后原文件a01.txt.bz2 还在
gzip压缩/解压缩过程中保留原⽂件
[root@localhost test01]# rm -rf a01.txt.bz2
[root@localhost test01]# ls
a01.txt testzip
[root@localhost test01]# gzip -c a01.txt > a01.txt.gz #gzip压缩过程中保留原文件需要输出重定向
[root@localhost test01]# ls
a01.txt a01.txt.gz testzip
[root@localhost test01]# rm -rf a01.txt
[root@localhost test01]# ls
a01.txt.gz testzip
[root@localhost test01]# gzip -d -c a01.txt.gz > a01.txt #gzip解压缩过程中保留原文件也需要输出
[root@localhost test01]# ls
a01.txt a01.txt.gz testzip
2、文件打包
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# bzip2 -k a01 a02 a03 #压缩a01-a03,生成了多个压缩文件
[root@localhost test02]# ls
a01 a01.bz2 a02 a02.bz2 a03 a03.bz2 a04 #生成了多个压缩文件
[root@localhost test02]# rm -rf *.bz2
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# bzip2 -k a0{1..3} #使用通配符压缩
[root@localhost test02]# ls
a01 a01.bz2 a02 a02.bz2 a03 a03.bz2 a04 #同样还是 生成了多个压缩文件
# bzip2/gzip 无法压缩多个文件,无法压缩目录 ,该种情况需要先将多个文件/目录进行tar打包,打包之后变 成一个文件再压缩
tar ⼯具进⾏打包
tar [参数] [⽬标⽂件命名] [源文件/目录名称]
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# tar -cvf test02.tar a0{1..3} #将多个文件打包,生成test02.tar
a01
a02
a03
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar
[root@localhost test02]# rm -rf a01 a02 a03
[root@localhost test02]# ls
a04 test02
[root@localhost test02]# tar -xvf test02 #解压test02
a01
a02
a03
[root@localhost test02]# ls
a01 a02 a03 a04 test02
[root@localhost test02]# ls ../test01 #查看/test01文件夹
a01.txt a01.txt.gz testzip
[root@localhost test02]# tar -xvf test02 -C ../test01 #解压test02到/test01目录下,-C(大写)
a01
a02
a03
[root@localhost test02]# ls
a01 a02 a03 a04 test02
[root@localhost test02]# ls ../test01 #再次查看/test01文件夹
a01 a01.txt a01.txt.gz a02 a03 testzip #多了a01 a02 a03
[root@localhost test02]# tar -cvf ../test01/test02.tar a0{1..4} #打包a4个文件到/test01目录下
a01
a02
a03
a04
[root@localhost test02]# ls ../test01
a01 a01.txt a01.txt.gz a02 a03 test02.tar testzip
[root@localhost test02]# rm -rf test02
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# cd ..
[root@localhost clh]# tar -cvf test02.tar test02 #对test02文件目录进行打包
test02/
test02/a04
test02/a01
test02/a02
test02/a03
#在打包目录时,不建议打包目标文件 存储在需要打包的目录
3 、文件打包+压缩
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# tar -cvf test02.tar a0{1..4}
a01
a02
a03
a04
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar
[root@localhost test02]# bzip2 -k test02.tar
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar test02.tar.bz2
[root@localhost test02]# gzip -c test02.tar > test02.tar.gz
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar test02.tar.bz2 test02.tar.gz
tar
#bzip2 连打包带压缩/解包带解压缩
[root@localhost test02]# rm -rf *tar
[root@localhost test02]# rm -rf *.tar
[root@localhost test02]# rm -rf *.bz2
[root@localhost test02]# rm -rf *.gz
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# tar -jcf test02.tar.bz2 a0{1..4} #连打包带压缩
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar.bz2
[root@localhost test02]# rm -rf a*
[root@localhost test02]# ls
test02.tar.bz2
[root@localhost test02]# tar -jxvf test02.tar.bz2 #bzip2连解包带解压缩
a01
a02
a03
a04
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar.bz2
#gzip 连打包带压缩/解包带解压缩
[root@localhost test02]# rm -rf test02.tar.bz2
[root@localhost test02]# ls
a01 a02 a03 a04
[root@localhost test02]# tar -zcvf test02.tar.gz a0{1..4} #连打包带压缩
a01
a02
a03
a04
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar.gz
[root@localhost test02]# rm -rf a*
[root@localhost test02]# ls
test02.tar.gz
[root@localhost test02]# tar -zxvf test02.tar.gz #连解包带解压缩
a01
a02
a03
a04
[root@localhost test02]# ls
a01 a02 a03 a04 test02.tar.gz