Linux基础篇学习——压缩、解压缩,归档、备份(zip/unzip,gzip/gunzip,bzip2/bunzip2,xz/unxz,tar,cpio)

压缩、解压缩原理

计算机系统以bytes为单位
实际上,计算机中最小的计量单位是bits 1 byte = 8 bits
压缩就是将没有使用到的空间丢出来,让文件的占用空间变小
解压缩就是将压缩完的数据还原成未压缩的状态
压缩是指压缩后与压缩前的文件所占用磁盘空间的大小比值

当存放数字1的时候,根据二级制计数,存放的是00000001,实际上前面7个0都是空的,但是由于要满足我们操作系统的存取方式,必须以8位为单位存储,所会造成有一些空间并没有填满。

应用

常见的网站数据传输一般都是使用的压缩技术,数据在网络传输过程中是使用的压缩数据,当压缩数据达到用户主机时,通过解压缩,再展示出来

zip,unzip

既归档又压缩的工具,可以压缩目录

zip

OPTION

选项 含义
-r 递归处理
-m 向压缩文件中添加某个文件
-x 压缩文件时排除某个文件
-d 删除压缩文件中的某个文件

zip -r 递归处理,将指定目录下的所有文件和子目录一并处理
将当前目录下的所有文件和文件夹全部压缩成test.zip文件

[root@localhost ~]# zip -r test.zip ./*

打包目录

[root@localhost ~]# zip test2.zip /tmp/*

zip -m 向压缩文件中添加某个文件
向压缩文件test.zip中添加file文件

[root@localhost ~]# zip -m test.zip file
  adding: file (stored 0%)

zip -x 压缩文件时排除某个文件
压缩文件时排除facebook.txt

[root@localhost tmp]# zip test3.zip ./* -x facebook.txt
  adding: passwd (deflated 58%)

zip -d 删除压缩文件中的某个文件
范例5 删除压缩文件test.zip中的vimrc_beifen文件

[root@localhost ~]# zip -d test.zip vimrc_beifen
deleting: vimrc_beifen

范例1 压缩install.log文件

[root@localhost ~]# zip anaconda-ks.cfg.zip anaconda-ks.cfg
  adding: anaconda-ks.cfg (deflated 45%)
[root@localhost ~]# ls -l anaconda-ks.cfg.zip anaconda-ks.cfg
-rw-------. 1 root root 1241 Sep  7 16:17 anaconda-ks.cfg
-rw-r--r--. 1 root root  859 Oct 27 08:47 anaconda-ks.cfg.zip

范例2 压缩效率分别为3和9

[root@localhost ~]# zip -3 anaconda-ks.cfg3.zip anaconda-ks.cfg
  adding: anaconda-ks.cfg (deflated 44%)
[root@localhost ~]# zip -9 anaconda-ks.cfg9.zip anaconda-ks.cfg
  adding: anaconda-ks.cfg (deflated 45%)
[root@localhost ~]# ls -l anaconda-ks.cfg*.zip anaconda-ks.cfg
-rw-------. 1 root root 1241 Sep  7 16:17 anaconda-ks.cfg
-rw-r--r--. 1 root root  870 Oct 27 08:49 anaconda-ks.cfg3.zip
-rw-r--r--. 1 root root  859 Oct 27 08:49 anaconda-ks.cfg9.zip
-rw-r--r--. 1 root root  859 Oct 27 08:47 anaconda-ks.cfg.zip

1.压缩率(Compression ratio)是文件压缩后的大小与压缩前的大小之比(压缩率一般是越小越好,但是压得越小,解压时间越长)
2.压缩效率(compression efficiency)越大压缩后的文件越小,解压时间越长

unzip

OPTION

unzip 询问用户是否覆盖已有文件
unzip -n 解压缩时不覆盖已有文件
unzip -o 不询问用户,覆盖已有文件
unzip -v 执行时显示详细信息或查看压缩文件不解压


gzip,gunzip

gzip不归档压缩(压缩后原始文件就不存在了)

gzip

OPTION

gzip -r dir/ 该目录下的文件逐个压缩
gzip * 当前目录下的文件逐个压缩

[root@localhost test]# gzip -r dir1/
[root@localhost test]# ls dir1
fstab.gz test3.txt.gz test4.txt.gz test5.txt.gz

gzip -c 压缩但保留原始文件

[root@localhost test]# gzip -c test2.txt > test2.txt.gz
[root@localhost test]# ls test2*
test2.txt test2.txt.gz test2.zip

gzip -d 解压 等价于gunzip
gzip -v 压缩时显示指令执行过程
gzip -t 测试压缩文件是否正确无误
gzip -l 列出压缩文件的相关信息
范例1 使用gzip压缩文件

[root@localhost test]# gzip test1.txt
[root@localhost test]# ls test1*
test1.txt.gz test1.zip

范例2 解压文件至原路径

[root@localhost test]# gunzip test1.txt.gz
[root@localhost test]# ls test1*
test1.txt test1.zip

gunzip

gunzip -c 解压至指定路径
gunzip -r 解压目录下的压缩文件

范例1 解压至指定路径

[root@localhost test]# gunzip -c test2.txt.gz > /tmp/test.txt
[root@localhost test]# cat /tmp/test.txt
test2

范例2 解压目录下的压缩文件

[root@localhost test]# gunzip -r dir1
[root@localhost test]# ls dir1
fstab test3.txt test4.txt test5.txt

bzip2,bunzip2

bzip2、bunzip2是更新的Linux压缩工具,比gzip有着更高的压缩率(bzip2压缩文件将删除原文件)

bzip2

选项 含义
-c 压缩但保留原文件
-k 压缩但保留原文件
-d 解压缩
-v 压缩时显示详细信息
-f 强制压缩替换已存在的同名的.bz2压缩文件
-t 模拟解压

bzip2 -c 压缩但保留原文件 可以重定向到指定目录

[root@localhost tmp]# bzip2 -c test2 > test2.bz2
[root@localhost tmp]# ll test2*
-rw-r--r--. 1 root root  0 Oct 27 10:08 test2
-rw-r--r--. 1 root root 14 Oct 27 10:09 test2.bz2

bzip2 -k 压缩但保留原文件

[root@localhost tmp]# bzip2 -k test3
[root@localhost tmp]# ll test3*
-rw-r--r--. 1 root root 24 Oct 27 09:16 test3
-rw-r--r--. 1 root root 61 Oct 27 09:16 test3.bz2

bzip2 -d 解压缩 等价于bunzip2
bzip2 -v 压缩时显示详细信息
bzip2 -f 强制压缩替换已存在的同名的.bz2压缩文件

[root@localhost tmp]# ll test1*
-rw-r--r--. 1 root root   4 Oct 27 10:02 test1
-rw-r--r--. 1 root root  61 Oct 27 09:16 test1.bz2
[root@localhost tmp]# bzip2 test1
bzip2: Output file test1.bz2 already exists.
[root@localhost tmp]# bzip2 -f test1
[root@localhost tmp]# ll test1*
-rw-r--r--. 1 root root 42 Oct 27 10:02 test1.bz2

bzip2 -t 模拟解压 查看文件能否解压

[root@localhost tmp]# bzip2 -tv test2.bz2
  test2.bz2: ok

xz,unxz

练习1 压缩文件

[root@localhost test]# xz test1.txt
[root@localhost test]# ls test1.txt.xz
test1.txt.xz

练习2 压缩dir1目录下文件

[root@localhost test]# xz dir1/*
[root@localhost test]# ls dir1
fstab.xz test3.txt.xz test4.txt.xz test5.txt.xz

练习3 查看压缩文件内容

[root@localhost test]# xzcat test1.txt.xz
test1

练习4 解压缩

[root@localhost test]# unxz test1.txt.xz

练习5 解压缩目录dir1下文件

[root@localhost test]# xz -d dir1/*
[root@localhost test]# ls dir1
fstab test3.txt test4.txt test5.txt

tar 打包归档

tar命令可以把一大堆的文件和目录全部打包成一个文件
打包:打包是指将一大堆文件或目录变成一个总的文件
压缩:压缩是将一个大的文件通过一些压缩算法变成一个小文件

Linux 中很多压缩程序只能针对一个文件进行压缩,当压缩一大堆文件时,先tar打包,再gzip等压缩。

-z 有gzip属性的
-j 有bz2属性的
-J 有xz属性的
-Z 有compress属性的

-c 建立压缩档案 必须有此参数
-f 使用档案名字 必须有此参数且放在最后,后面只能接档案名
-v 显示所有过程
–remove-files 打包后删除源文件
-r 向归档文件末尾追加文件
-u 更新原压缩包中的文件
-t 查看内容
-x 解压 默认覆盖已有文件
-C 解压到指定目录
-N <日期格式> --newer=<日期时间>,只将较指定日期更新的文件保存到备份文件里

准备测试文件

[root@localhost ~]# mkdir /tmp/dir
[root@localhost ~]# cd /tmp/dir/
[root@localhost dir]# for i in {1..5};do echo "this is a test of test$i">test$i;done
[root@localhost dir]# ll
total 20
-rw-r--r--. 1 root root    24 Oct 29 20:25 test1
-rw-r--r--. 1 root root    24 Oct 29 20:25 test2
-rw-r--r--. 1 root root    24 Oct 29 20:25 test3
-rw-r--r--. 1 root root    24 Oct 29 20:25 test4
-rw-r--r--. 1 root root    24 Oct 29 20:25 test5

练习1 把所有test*文件打包成一个test.tar文件(不压缩)

[root@localhost tmp]# tar -cvf test.tar test*
test2
test3
test4
test5

练习2 打包压缩后删除源文件

[root@localhost tmp]# tar -czf test1.tar test* --remove-files

练习3 向归档文件末尾追加文件file

[root@localhost tmp]# tar -rvf test.tar file
file

练习4 更新归档中的文件file

[root@localhost tmp]# echo 123 >> file
[root@localhost tmp]# tar -uvf test.tar file
file

练习5 查看test.tar的内容

[root@localhost tmp]# tar -tf test.tar
test2
test3
test4
test5
file

练习6 解压test.tar里的test*文件并删除归档

[root@localhost dir]# tar -xvf test.tar test*;rm -rf test.tar

练习7 打包/home/wang为wang.tat.gz到指定目录/var/tmp

[root@zycentos7 ~]# tar -czvf /var/tmp/wang.tar.gz /home/wang
[root@zycentos7 ~]# ls /var/tmp
wang.tar.gz

在不知道使用什么压缩工具的时候可以使用 tar -xf 进行解压


cpio 归档工具

cpio用于创建、解压归档文件,也可以对归档文件执行拷入拷出的动作,即向归档文件中追加文件,或从归档文件中提取文件。它也支持tar格式的归档文件,但是不支持对压缩后的tar(如.tar.gz格式)。

cpio一般从标准输入获取数据,写入到标准输出,所以一般会结合管道、输入重定向、输出重定向使用。

cpio三种运行模式

模式 Value
Copy-out模式 进行归档操作(若未指定归档的目标,将归档到标准输出中)
copy-pass模式 目录拷贝,使用find -depth来指定待归档文件
Copy-in模式 提取,cpio将从归档文件中提取文件,或者列出归档文件中的文件列表(将从标准输入中读取归档文件)

OPTION

选项 含义
-o 指定运行为copy-out模式,即归档模式
-i 指定运行为copy-in模式,即提取模式
-p 指定运行为copy-pass模式,即目录拷贝模式
-t 列出归档文件中的文件列表
-F 使用指定归档文件名替代标准输入或输出
-A 向已存在的归档文件中追加文件(只能使用-F或-O指定归档文件,只能用在copy-out模式下)
-u 当目标中有同名文件时,强制替换冲突文件
-d 当需要的时候自动创建目录
-0/--null 解析空字符串\0
-v 给出详细信息

copy-out归档模式下,其默认行为等价于重定向符号">",所以内容会完全覆盖,但归档文件(inode)不变

练习

练习1 将家目录下的所有文件都归档到tree.cpio中

[root@localhost ~]# find ~ -depth -print0|cpio --null -ov -F /tmp/tree.cpio
……
214 blocks

出于准确性考虑,会在find上使用 -print0 ,然后在cpio上使用 --null 解析空字符
如果使用find搜索,且归档文件和搜索目录是同一路径时,它会将归档文件本身也归档到归档文件中,即进行了迭代归档

解决迭代归档问题:让归档文件不被find搜索到即可
1.在find中排除
2.在cpio中排除
3.把归档文件放到其他目录下去

练习2 列出归档文件中的文件列表

[root@localhost tmp]# cpio -tv < tree.cpio
……
dr-xr-x---   6 root     root            0 Oct 29 21:11 /root
214 blocks

练习3 列出归档文件中指定目录/root下的所有文件

[root@localhost tmp]# cpio -tF tree.cpio /root/*	;不会显示出隐藏文件
/root/vimrc_beifen
……
/root/learngit
214 blocks
[root@localhost tmp]# cpio -tF tree.cpio /root/.*	;只会列出隐藏文件
/root/.bash_logout
……
/root/.bash_history.swp
214 blocks
[root@localhost tmp]# cpio -tF tree.cpio /root/{.*,*}	;既列出隐藏文件,也列出普通文件
/root/vimrc_beifen
……
/root/.bash_history.swp
214 blocks

练习4 向归档文件中追加文件

[root@localhost ~]# ls /etc/passwd|cpio -oA -F tree.cpio

练习5 提取文件

[root@localhost home]# cpio -idv -F tree.cpio /root/test1

强制提取(目标目录中存在同名文件)

[root@localhost home]# cpio -idvu -F tree.cpio /root/test1

练习6 目录备份(目录文件复制,即copy-pass模式)

[root@localhost tmp]# find ~ -depth -print0|cpio --null -pvd /tmp/

该模式下复制的目录在目标位置上是以子目录形式存在的

发布了43 篇原创文章 · 获赞 30 · 访问量 5938

猜你喜欢

转载自blog.csdn.net/qq_42049496/article/details/102809763