Linux培训
达内IT学院

400-111-8989

Linux运维基础知识之压缩与解压缩


今天小编要跟大家分享的文章是关于Linux运维基础知识之压缩与解压缩。希望本篇文章能够对想要学习Linux运维知识的小伙伴们有所帮助,下面来和小编一起看一看吧!

Linux运维基础知识之压缩与解压缩

1、compress

· compress [-dfvcVr] [-b maxbits] [file ...]

· -d: 解压缩,相当于uncompress

· -c: 结果输出至标准输出,不删除原文件,可以重定向到name.Z,生成压缩文件

· -v: 显示详情

· uncompress 解压缩

[root@CentOS7 data]# compress m ##不保留源文件

[root@CentOS7 data]# ll

total 4840

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 994739 Aug 8 19:11 m.Z

[root@CentOS7 data]# compress -d m.Z

[root@CentOS7 data]# ll

total 7724

-rw-r--r--. 1 root root 3944964 Aug 8 19:11 m

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

[root@CentOS7 data]# compress -c m > m.Z

## 保留源文件

[root@CentOS7 data]# ll

total 8688

-rw-r--r--. 1 root root 3944964 Aug 8 19:11 m

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 994739 Aug 8 19:20 m.Z

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

[root@CentOS7 data]# compress -d m.Z

m already exists.

Do you wish to overwrite m (y or n)? y

2、 gzip/gunzip

gzip [OPTION]... FILE ...

· -d: 解压缩,相当于gunzip

· -c: 将压缩或解压缩的结果输出至标准输出

· -#:1-9,指定压缩比,值越大压缩比越大

· cat /etc/fstab |gzip > fstab.gz 当命令的执行结果太多,希望对命令执行结果进行压缩,如生产中对mysql进行备份mysqldump|gzip > fstab.gz

· zcat:不显式解压缩的前提下查看文本文件内容zcat file.Z >file

· 实例:

[root@CentOS7 data]# gzip m

[root@CentOS7 data]# ll

total 4408

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

[root@CentOS7 data]# gunzip m ##和gzip -d一样

[root@CentOS7 data]# ll

total 7716

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-r--r--. 1 root root 3944964 Aug 8 19:22 m

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

[root@CentOS7 data]# zcat m.gz > m

[root@CentOS7 data]# ll

total 8264

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-r--r--. 1 root root 3944964 Aug 8 19:45 m

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

3、bzip2

bzip2/bunzip2/bzcatbzip2 [OPTION]... FILE ...

· -k: keep, 保留原文件

· -d:解压缩

· -#:1-9,压缩比,默认为9

· bzcat:不显式解压缩的前提下查看文本文件内容

[root@CentOS7 data]# bzip2 m

[root@CentOS7 data]# ll

total 8528

-rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 267452 Aug 8 19:45 m.bz2

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

[root@CentOS7 data]# bzcat m.bz2 > f1

[root@CentOS7 data]# ll

total 12384

-rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-r--r--. 1 root root 3944964 Aug 8 19:45 m

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 267452 Aug 8 19:45 m.bz2

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

[root@CentOS7 data]# bzip2 -d m.bz2

[root@CentOS7 data]# ll

total 12120

-rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-r--r--. 1 root root 3944964 Aug 8 19:45 m

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

[root@CentOS7 data]# bzip2 -k f1 ##保留源文件

[root@CentOS7 data]# ll

total 8792

-rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1

-rw-r--r--. 1 root root 267452 Aug 8 19:49 f1.bz2

-rw-r--r--. 1 root root 0 Aug 8 19:41 file

-rw-------. 1 root root 3944964 Aug 8 19:10 m.bak

-rw-r--r--. 1 root root 267452 Aug 8 19:45 m.bz2

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts

4、xz/unxz/xzcat

· xz [OPTION]... FILE ...

· -k: keep, 保留原文件

· -d:解压缩

· -#:1-9,压缩比,默认为6

· xzcat: 不显式解压缩的前提下查看文本文件内容

5、 zip/unzip:

· zip可以对多个文件压缩归档成单个文件

可以压缩文件夹

打包压缩

· zip –r sysconfig.zip /etc/sysconfig/

解包解压缩

· unzip sysconfig.zip

· cat /var/log/messages | zip messages -

· unzip -p message > message

(1)把文件解压到当前目录下

unzip test.zip

(2)如果要把文件解压到指定的目录下,需要用到-d参数。

unzip -d /temp test.zip

(3)解压的时候,有时候不想覆盖已经存在的文件,那么可以加上-n参数

unzip -n test.zip

unzip -n -d /temp test.zip

(4)只看一下zip压缩包中包含哪些文件,不进行解压缩

unzip -l test.zip

(5)查看显示的文件列表还包含压缩比率

unzip -v test.zip

(6)检查zip文件是否损坏

unzip -t test.zip

(7)将压缩文件test.zip在指定目录tmp下解压缩,如果已有相同的文件存在,要求unzip命令覆盖原先的文件

unzip -o test.zip -d /tmp/

[root@CentOS7 data]# zip -r tertdir.zip testdir/

[root@CentOS7 data]# ll

total 8796

-rw-r--r--. 1 root root 978 Aug 8 19:59 tertdir.zip

drwxr-xr-x. 3 root root 17 Aug 8 19:58 testdir

[root@CentOS7 data]# zip -r sysconfig.zip /etc/sysconfig

-rw-r--r--. 1 root root 95104 Aug 8 20:02 sysconfig.zip

[root@CentOS7 data]# cat /var/log/messages |zip message.zip - ##将cat 命令的执行结果放到文件名为-中,然后压缩成message.zip

adding: - (deflated 98%)

[root@CentOS7 data]# ll

-rw-r--r--. 1 root root 61943 Aug 8 20:13 message.zip

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:56 scripts

drwxr-xr-x. 3 root root 17 Aug 8 19:58 testdir

-rw-r--r--. 1 root root 978 Aug 8 19:59 testdir.zip

[root@CentOS7 data]# unzip message.zip

Archive: message.zip

inflating: -

[root@CentOS7 data]# ll

total 11712

-rw-------. 1 root root 2851599 Aug 8 20:13 - ##解压后文件-

message.zip

-rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz

drwxr-xr-x. 4 root root 4096 Aug 8 19:56 scripts

drwxr-xr-x. 3 root root 17 Aug 8 19:58 testdir

-rw-r--r--. 1 root root 978 Aug 8 19:59 testdir.zip

[root@CentOS7 data]# unzip -p testdir.zip > test ##压缩后更改文件名为test

[root@CentOS7 data]# ll

total 11712

-rw-r--r--. 1 root root 0 Aug 8 20:39 test

-rw-r--r--. 1 root root 978 Aug 8 19:59 testdir.zip

6、 tar工具

tar(Tape ARchive,磁带归档的缩写)

· tar [OPTION]...

§ -c:创建打包文件

§ -p:显示打包过程

§ -v:显示压缩过程

§ -x:解压缩

§ -t:预览

(1) 创建归档

· tar -cpvf /PATH/TO/SOMEFILE.tar FILE...

(2) 追加文件至归档:注:不支持对压缩文件追加

· tar -r -f /PATH/TO/SOMEFILE.tar FILE...

(3) 查看归档文件中的文件列表

· tar -t -f /PATH/TO/SOMEFILE.tar

(4) 展开归档

· tar -x -f /PATH/TO/SOMEFILE.tar

tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/

(5) 结合压缩工具实现:归档并压缩

· -z:gzip

§ 后缀名:.tar.gz

§ 归档并压缩:tar -zvcf

§ 展开归档:tar -zxf

· -j:bzip2

§ 后缀名:.tar.bz2

§ 归档并压缩:tar -jvcf

§ 展开归档:tar -jxf

· -J:xz

§ 后缀名:.tar.xz

§ 归档并压缩:tar -Jvcf

§ 展开归档:tar -Jxf

[root@CentOS7 data]# tar -zcvf testdir.tar.gz testdir

testdir/

testdir/di1/

testdir/di1/dir2/

testdir/di1/dir2/3/

testdir/di1/dir2/4/

testdir/di1/dir2/5/

[root@CentOS7 data]# ll

testdir

testdir.tar.gz

[root@CentOS7 data]# tar -jcvf testdir.tar.bz2 testdir

[root@CentOS7 data]# ll

testdir.tar.bz2

[root@CentOS7 data]# tar -Jcvf testdir.tar.bz2 testdir

testdir/

testdir/di1/

testdir/di1/dir2/

testdir/di1/dir2/3/

testdir/di1/dir2/4/

testdir/di1/dir2/5/

[root@CentOS7 data]# ll

testdir.tar.xz

[root@CentOS7 data]# tar -zvxf testdir.tar.gz

testdir/

testdir/di1/

testdir/di1/dir2/

testdir/di1/dir2/3/

testdir/di1/dir2/4/

testdir/di1/dir2/5/

[root@CentOS7 data]# ll

testdir

-T选项指定输入文件,-X选项指定包含要排除的文件列表

tar zcvf mybackup.tgz -T /root/includefilelist -X/root/excludefilelist

分割大的 tar 文件为多份小文件:

split –b Size –d tar-file-name prefix-name

split -b 1M –d mybackup.tgz mybackup-parts

split -b 1M mybackup.tgz mybackup-parts

合并:

cat mybackup-parts* > mybackup.tar.gz

7、 cpio

功能:复制文件从或到归档

cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以

解压以“.cpio” 或者“.tar” 结尾的文件

cpio [选项] > 文件名或者设备名

cpio [选项] < 文件名或者设备名

选项

· -o 将文件拷贝打包成文件或者将文件输出到设备上

· -i 解包,将打包文件解压或将设备上的备份还原到系统

· -t 预览,查看文件内容或者输出到设备上的文件内容

· -v 显示打包过程中的文件名称

· -d 解包生成目录,在cpio还原时,自动的建立目录

· -c 一种较新的存储方式

以上就是小编今天为大家分享的关于Linux运维基础知识之压缩与解压缩的文章,希望本篇文章能够对正在从事Linux运维相关工作的小伙伴们有所帮助,想要了解更多Linux相关知识记得关注达内Linux培训官网。最后祝愿小伙伴们工作顺利,成为一名优秀的Linux运维工程师。

文章来源:原创朱友生 SRE学习之路

【免责声明:本文图片及文字信息均由小编转载自网络,旨在分享提供阅读,版权归原作者所有,如有侵权请联系我们进行删除。】

预约申请免费试听课

填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:运维人员能够通过HTTPS解决什么问题
下一篇:2020年Linux运维工程师必备的Linux常用命令

云计算就业岗位有哪些?

学物联网工程好就业吗?

图像算法工程师发展前景

大数据运维工资一般多少?

  • 扫码领取资料

    回复关键字:视频资料

    免费领取 达内课程视频学习资料

Copyright © 2023 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省