压缩打包介绍,gzip压缩工具,bzip2压缩工具,xz压缩工具

file命令

概念:file命令可以查看一个文件或者目录属于什么的。
[root@localhost tmp]# file 1.txt.gz  #查看这个文件是什么类别的
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Apr 14 14:43:49 2018

压缩包介绍

概念:在Windows下我们接触最多的压缩文件 .rar 格式;
但在Linux下,不能识别这种格式,他有自己独特的压缩工具;
但.zip格式的文件在Windows和Linux下都可以使用;
Windows常见的压缩文件:.rar 、.zip 、.7Z
Linux常用的压缩文件:.zip 、.gz 、.bz2 、.xz 、.tar 、.gz 、
.tar 、.bz2 、.tar.xz
压缩文件,不仅能节省磁盘空间,而且在传输能节省网络带宽。

gzip压缩工具(不能压缩目录)

命令格式:
gzip [-d#] filename,其中#为1-9的数字
-d 该参数在解压缩时使用
-# 表示压缩等级,1为最差,9为最好,6为默认

准备工作

创建一个目录

[root@localhost tmp]# mkdir d6z
[root@localhost tmp]# cd d6z/

find搜索一个大的文件出来

[root@localhost d6z]# find /etc/ -type f -name "*conf"

find 搜索,/etc/目录下,-type f 指定搜索类型为f普通文档的文件,-name "*conf"查找名字以为conf结尾的所有文件。

/etc/resolv.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/yum/pluginconf.d/fastestmirror.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/version-groups.conf
/etc/lvm/lvm.conf
/etc/lvm/lvmlocal.conf

把搜索的这些文件的内容,输出到一个文件里

[root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;

find 搜索,/etc/目录下,-type f 指定搜索类型为f普通文档的文件,-name "*conf"查找名字以为conf结尾的所有文件,-exec推动后面的命令,cat {} 针对所有文件挨个查看 {}表示列出来的文件,>> 追加到 1.txt文件里 \;

[root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost d6z]# !du
du -sh 1.txt #查看这个文件的大小
2.2M 1.txt
[root@localhost d6z]# wc -l 1.txt #查看这个文件的行数总共有多少
37765 1.txt

接下来用gzip压缩它
[root@localhost d6z]# gzip 1.txt 
[root@localhost d6z]# ls
1.txt.gz
2018.4.16 四周第四次课
[root@localhost d6z]# du -sh 1.txt.gz #再来查看这个文件的大小
372K 1.txt.gz

解压gzip包

[root@localhost d6z]# gzip -d 1.txt.gz #选项-d在解压缩时使用
[root@localhost d6z]# ls #查看文件
1.txt
[root@localhost d6z]# du -sh 1.txt #查看大小,这里跟之前的不一样,是因为这个文件里本来就有虚空间。
1.5M 1.txt

扫描二维码关注公众号,回复: 88708 查看本文章

查看压缩文件的信息,用file

[root@localhost d6z]# file 1.txt.gz 
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Apr 14 14:14:22 2018

查看压缩文件的内容,用zcat

[root@localhost d6z]# zcat 1.txt.gz

压缩时候,指定到一个目录,并且不让这个原文件消失

[root@localhost d6z]# gzip -d 1.txt.gz #先给之前压缩的文件解压
gzip: 1.txt.gz: No such file or directory
[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz

gzip解压缩 -c指定 1.txt文件,>重镜像到,/tmp/1.txt.gz

[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# ls /tmp/1.txt.gz 
/tmp/1.txt.gz
2018.4.16 四周第四次课

解压时,不删除,指定一个地方

[root@localhost d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt
2018.4.16 四周第四次课

bzip2压缩工具 (不支持目录压缩)这个比gzip压缩更狠一点

概念:操作与gzip一样,只是比gzip压缩的更狠一点,占用的cpu越多。
bzip2命令格式为:
bzip2 [-dz] filename
参数选项只有 -d(解压缩)  -z(压缩)
压缩级别分别是1~9,默认是9级。

yum安装源码包bzip2

[root@localhost d6z]# yum install -y bzip2

xz压缩工具(不能压缩目录)

概念:操作跟gzip和bizp2是一样的。 这个比gzip和dzip2都狠。
参数-d (解压缩) -z(压缩)

具体示例如下:

[root@localhost d6z]# xz 1.txt #压缩1.txt文件
[root@localhost d6z]# ls #查看
1.txt.xz 2.txt
[root@localhost d6z]# xz -d 1.txt.xz #解压缩1.txt.xz文件
[root@localhost d6z]# ls #查看
1.txt 2.txt
[root@localhost d6z]# xz -c 1.txt > /tmp/3.txt.xz #重镜像1.txt文件到/tmp目录下新生成3.txt.xz,保留原文件
[root@localhost d6z]# ls /tmp/
1.txt.gz systemd-private-d4565e90fd384c749a36d51b19e3e377-chronyd.service-91Lo0Y
2.txt systemd-private-d4565e90fd384c749a36d51b19e3e377-vgauthd.service-XKotz4
3.txt.xz systemd-private-d4565e90fd384c749a36d51b19e3e377-vmtoolsd.service-NZwSqa
d6z yum.log
ks-script-5ToQJK
[root@localhost d6z]# xz -d -c /tmp/3.txt.xz >3.txt
[root@localhost d6z]# ls 
1.txt 2.txt 3.txt
2018.4.16 四周第四次课

猜你喜欢

转载自my.oschina.net/u/3830571/blog/1798898