【2018.04.16学习笔记】【linux基础知识6.1-6.4】

Linux压缩工具详解
本文详细介绍了Linux系统中常用的三种压缩工具:gzip、bzip2和xz。这些工具不仅能够有效节省磁盘空间,还能减少文件传输所占用的网络带宽。文章通过实际案例展示了如何使用这些工具进行压缩和解压缩操作。

6.1 压缩打包介绍

Windows系统里常见的压缩文件格式有:rar、zip、7z等

linux系统压缩文件类型有:zip、gz、bz2、xz、tar.gz、tar.bz2、tar.xz等

文件压缩能够节省磁盘空间,更重要的是能够节省文件传输占用的网络带宽,由于企业网络带宽极其昂贵,对于企业来说能够节省其运营成本。

6.2 gzip压缩工具

gzip命令:不能压缩目录,压缩完后,原文件会删除。

[root@lgs-01 ~]# du -sh abc
3.0M	abc
[root@lgs-01 ~]# gzip /root/abc
[root@lgs-01 ~]# ls -l abc.gz
-rwxr-xr-x. 1 root root 1090830 4月  16 18:00 abc.gz
[root@lgs-01 ~]# du -sh abc.gz 
1.1M	abc.gz
[root@lgs-01 ~]# ls -l abc
ls: 无法访问abc: 没有那个文件或目录

可以指定压缩级别:-(1-9),默认的压缩级别是6,级别越高消耗越大的cpu资源。

[root@lgs-01 ~]# gzip -9 abc
[root@lgs-01 ~]# du -sh abc.gz 
1.1M	abc.gz

解压缩:gzip -d或者 gunzip命令,解压缩后原来的压缩文件会删除。

[root@lgs-01 ~]# gzip -d abc.gz 
[root@lgs-01 ~]# du -sh abc
3.0M	abc

查看压缩文件内容:ccat,先解压,在cat查看内容

[root@lgs-01 ~]# gzip xxx.conf
[root@lgs-01 ~]# zcat xxx.conf.gz 
# 
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
# --------------------------------------------------------
# MANDATORY_MANPATH			manpath_element
# MANPATH_MAP		path_element	manpath_element
# MANDB_MAP		global_manpath	[relative_catpath]

gzip -c:指定压缩文件保存目录,原文件不删除。

[root@lgs-01 ~]# gzip -c zb.txt > /root/c/666.gz
[root@lgs-01 ~]# ls -l zb.txt 
-rw-r--r--. 1 root root 4350 1月  24 2017 zb.txt
[root@lgs-01 ~]# ls -l c/666.gz
-rw-r--r--. 1 root root 1656 4月  16 18:15 c/666.gz

6.3 bzip2压缩工具

bzip2命令:压缩成bz2文件,不能压缩目录,压缩算法与gzip不同,压缩率更高

[root@lgs-01 ~]# bzip2 abc
[root@lgs-01 ~]# du -sh abc.bz2
1.1M	abc.bz2

解压命令:bzip2 -d 或者 bunzip2

[root@lgs-01 ~]# bzip2 -d abc.bz2 
[root@lgs-01 ~]# du -sh abc 
3.0M	abc

指定压缩文件保存的目录:bzip2 -c

[root@lgs-01 ~]# bzip2 -c abc > c/abc1.bz2
[root@lgs-01 ~]# ls -l c/abc1.bz2 
-rw-r--r--. 1 root root 1078029 4月  16 18:27 c/abc1.bz2

查看压缩文件内容:bzcat

[root@lgs-01 ~]# bzip2 xxx.conf 
[root@lgs-01 ~]# bzcat xxx.conf.bz2 
# 
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
# --------------------------------------------------------
# MANDATORY_MANPATH			manpath_element
# MANPATH_MAP		path_element	manpath_element
# MANDB_MAP		global_manpath	[relative_catpath]

可以指定压缩级别:-(1-9),默认的压缩级别是6,级别越高消耗越大的cpu资源。

[root@lgs-01 ~]# bzip2 -1 xxx.conf 
[root@lgs-01 ~]# du -sh xxx.conf.bz2 
4.0K	xxx.conf.bz2

6.4 xz压缩工具

压缩命令:xz 。不支持压缩目录,压缩率比gzip和bzip2更高

[root@lgs-01 ~]# xz abc
[root@lgs-01 ~]# du -sh abc.xz 
816K	abc.xz

解压命令:xz -d或者unxz

[root@lgs-01 ~]# unxz abc.xz 
[root@lgs-01 ~]# ls -l abc
-rwxr-xr-x. 1 root root 3073568 4月  16 18:00 abc

指定压缩文件存放的目录:-c

[root@lgs-01 ~]# xz -c abc > c/abc1.xz
[root@lgs-01 ~]# ls -l c/abc1.xz
-rw-r--r--. 1 root root 831676 4月  16 18:35 c/abc1.xz

压缩级别: -(1-9),默认为6

[root@lgs-01 ~]# xz -9 abc
[root@lgs-01 ~]# du -sh abc.xz 
816K	abc.xz

查看压缩文件内容:xzcat

[root@lgs-01 ~]# xz xxx.conf 
[root@lgs-01 ~]# xzcat xxx.conf.xz 
# 
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
# --------------------------------------------------------
# MANDATORY_MANPATH			manpath_element
# MANPATH_MAP		path_element	manpath_element
# MANDB_MAP		global_manpath	[relative_catpath]

转载于:https://my.oschina.net/u/3804114/blog/1796341

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值