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删除压缩文件中的某个文件

将当前目录下的所有文件和文件夹全部压缩成test.zip文件

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

打包目录

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

范例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 执行时显示详细信息或查看压缩文件不解压

综合实例

文件准备

mkdir /test
cd /test
for i in {1..5};do echo "test$i" > test$i.txt;done
mkdir dir1
cp /etc/fstab dir1/

实例1 将当前目录dir1连同目录下文件一起压缩

[root@localhost test]# zip -r dir1.zip dir1/
  adding: dir1/ (stored 0%)
  adding: dir1/fstab (deflated 44%)
[root@localhost test]# ls dir1*
dir1.zip

dir1:
fstab

实例2 向压缩文件中test1.zip中添加test2.txt文件

[root@localhost test]# zip -m test1.zip test2.txt
  adding: test2.txt (stored 0%)

实例3 删除压缩文件中的文件

[root@localhost test]# zip -d test1.zip test2.txt
deleting: test2.txt

实例4 压缩文件时排除某个文件

[root@localhost test]# zip test.zip *.txt -x test1.txt
  adding: test3.txt (stored 0%)
  adding: test4.txt (stored 0%)
  adding: test5.txt (stored 0%)

实例5 解压文件test2.zip

[root@localhost test]# unzip test2.zip
Archive:  test2.zip
extracting: test2.txt

实例6 将压缩文件text.zip在指定目录dir1下解压缩

[root@localhost test]# unzip test.zip -d dir1
Archive:  test.zip
 extracting: dir1/test3.txt
 extracting: dir1/test4.txt
 extracting: dir1/test5.txt

实例7 查看压缩文件目录,但不解压

[root@localhost test]# unzip -v test.zip
Archive:  test.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       6  Stored        6   0% 01-21-2020 18:33 4e6f5599  test3.txt
       6  Stored        6   0% 01-21-2020 18:33 012ec35e  test4.txt
       6  Stored        6   0% 01-21-2020 18:33 1835f21f  test5.txt
--------          -------  ---                            -------
      18               18   0%                            3 files

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模拟解压
-z强制执行压缩(或--compress)

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

zcat、zless、bzcat、bzless 显示压缩文件的内容

1.zcat、zless

[root@localhost test]# zcat test2.txt.gz
test2

2.bzcat、bzless
在屏幕上显示man.config.bz2解压缩之后的内容

[root@localhost test]# bzcat man.config.bz2
i love ybc
because

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

练习8 解压wang.tat.gz到指定目录/home/zhao

[root@zycentos7 ~]# tar -xf wang.tar.gz -C /home/zhao

在不知道使用什么压缩工具的时候可以使用 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/

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值