【linux相识相知】压缩与打包
我们日常使用window的时候,经常会用到压缩与解压缩,如果要压缩一个文件,右击选择【添加到压缩文件】,解压缩则右击选择【解压到当前文件夹】,“点点点”就能完成。但是在一个没有装图形化界面的linux操作系统又不能使用“点点点”,那该怎么操作呢?本文就linux中如何使用压缩和打包工具做出解释。
为什么要压缩文件
压缩的目的是为了就是将文件通过压缩算法转变成一个体积更小格式的文件,减小了文件在硬盘上的占用空间,压缩文件的时候,特别的消耗CPU的时钟周期,因为CPU要进行大量的计算,所有压缩也是一种拿时间换空间的操作,同时也能使文件能够通过较慢的网络连接来实现更快的传输。
常用的压缩工具
在linux操作系统中提供了很多的压缩和解压缩工具,每个压缩工具在执行压缩的时候所用的算法是不一样的,设计越优良的算法,压缩的程度就越高。比较老的压缩工具有compress(现在已经不常用了),常用的压缩工具有:gzip、bzip、xz和zip。我们可以通过后缀名来区分压缩文件是被什么工具压缩的,例如如果使用compress压缩文件,得到的文件的后缀名是.z,其他的压缩的后缀名如下:

gzip、gunzip和zcat
gzip是最常用的压缩工具了,gunzip是对应的解压缩工具。zcat可以在不解压.gz格式的压缩文件的情况下查看文件的内容。
语法:gzip [OPTION]... FILE...
常用选项: -d:解压缩,相当于gunzip;
-#:指定压缩比,默认是6,数字越大压缩比越大(-),压缩比=压缩前文件大小/压缩后文件的大小;
-c:将压缩结果输出至标准输出。 gzip -c file > /path/to/somefile.gz
举例:
将/etc/init.d/functions复制到tmp目录下执行gzip压缩:
[root@localhost tmp]# cp /etc/init.d/functions /tmp/
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
[root@localhost tmp]# gzip functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.gz
也可以使用gunzip,为了方便记忆,建议大家直接使用-d选项就好了:
[root@localhost tmp]# gzip functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.gz
[root@localhost tmp]# gunzip functions.gz
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
指定压缩比:
[root@localhost tmp]# gzip - functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.gz
将压缩比设置为9之后,相对于压缩比6,仅仅只减少了8个字节,一般情况下都不需要去动压缩比,因为6已经是一个最好的选择。
使用-c将输出结果至标准输出,我们将看到一堆乱码,那-c选项到底有什么用呢?

我们在压缩文件的时候原文件会被删除,如果想保留原文件就可以通过-c选项来实现啦!
[root@localhost tmp]# gzip -c functions > functions.gz
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
-rw-r--r--. root root Sep : functions.gz
可以使用zcat在不解压缩的情况下查看文件的内容:
[root@localhost tmp]# zcat functions.gz
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
# TEXTDOMAIN=initscripts # Make sure umask is sane
umask # Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
......(略)
bzip2、bunzip2和bzcat
和gzip类似,bzip2为压缩工具,bunzip2为解压缩工具,同样bzcat的作用了在不解压文件的情况下,查看文件内容。
语法:bzip2 [OPTION]... FILE...
常用选项: -d:解压缩,相当于bunzip2
-#:指定压缩比,默认是6,数字越大压缩比越大(-)
-k:keep,压缩并保留原文件,bzip2不需要像gzip那样使用输出重定向至指定的文件,这样就方便多啦
我们来举例看一下:
将/etc/init.d/functions复制到tmp目录下,使用bzip2压缩:
[root@localhost tmp]# bzip2 functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.bz2
说明bzip2在默认压缩的情况下也会删除原文件,节约了磁盘的空间。
再来看一下解压缩的方法:
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.bz2
[root@localhost tmp]#
[root@localhost tmp]# bunzip2 functions.bz2
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
[root@localhost tmp]# bzip2 functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.bz2
[root@localhost tmp]# bzip2 -d functions.bz2
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
[root@localhost tmp]# bzip2 -k functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
-rw-r--r--. root root Sep : functions.bz2
使用bzcat在不打开压缩文件的情况下查看文件的内容:
[root@localhost tmp]# bzcat functions.bz2
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
# TEXTDOMAIN=initscripts # Make sure umask is sane
umask # Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH
......(略)
xz、unxz和xzcat
压缩工具的新秀,xz为压缩工具,unxz为解压缩工具,xzcat也是在不打开压缩文件的情况下查看文件内容。
语法:xz [OPTION]... FILE...
常用选项: -d:解压缩
-#:指定压缩比,默认为6
-k:压缩并保留原文件
[root@localhost tmp]# xz functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.xz
解压缩:
[root@localhost tmp]# unxz functions.xz #使用unxz解压缩
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
[root@localhost tmp]# xz functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions.xz
[root@localhost tmp]# xz -d functions.xz #使用-d选项解压缩
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
使用-k选项压缩并保留原文件:
[root@localhost tmp]# xz -k functions
[root@localhost tmp]# ll
total
-rw-r--r--. root root Sep : functions
-rw-r--r--. root root Sep : functions.xz
试试xzcat:
[root@localhost tmp]# xzcat functions.xz
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
# TEXTDOMAIN=initscripts # Make sure umask is sane
umask # Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH
......(略)
扩展,是用man手册的时候,我们会发现另外一个工具lzma、unlzma和lzcat,其后缀名为.lzma,记住xz就好啦,它和lzma是有一定的关系的,详细可见man手册。
lzma is equivalent to xz --format=lzma
unlzma is equivalent to xz --format=lzma --decompress
lzcat is equivalent to xz --format=lzma --decompress --stdout
我们linux内核官网查找内核文件的时候,文件被压缩使用的工具是gzip和xz,也可以看到xz的压缩率更大。
https://www.kernel.org/pub/linux/kernel/v4.x/

现在存在的一个问题是,仅仅只是对单个文件进行压缩,那么这些工具能够对目录进行压缩吗?
我们在tmp文件下创建test目录,拷贝几个文件到里面:
[root@localhost tmp]# ll /tmp/test/
total
-rw-r--r--. root root Sep : functions
-rw-------. root root Sep : messages
-rw-r--r--. root root Sep : passwd
现在来试试压缩目录:
[root@localhost tmp]# gzip /tmp/test/
gzip: /tmp/test/ is a directory -- ignored
[root@localhost tmp]# bzip2 /tmp/test/
bzip2: Input file /tmp/test/ is a directory.
[root@localhost tmp]# xz /tmp/test/
xz: /tmp/test/: Is a directory, skipping
都不行,那该怎么办呢?接下来我们讲讲tar吧!
打包工具tar
tar命令是用来归档文件的,可以将多个文件或者一个目录归一成一个后缀名为.tar的文件,归档文件并不会压缩文件,反而可能使文件的大小稍微大一点,所以一般在归档之后再执行压缩!。下面我们就来看一下tar的使用方法吧!
语法:tar [OPTION]... FILE...
方法:
()创建归档
-c -f /path/to/somefile.tar file...
-cf /path/to/somefile.tar file...
()展开归档
-xf /path/from/somefile.tar
-xf /path/from/somefile.tar -C /path/to/somedir
()查看归档文件的文件列表
-tf /path/to/somefile.tar
归档之后然后进行压缩,结合之前的压缩工具,就能实现压缩多个文件。
()归档压缩
-z:gzip2
-zcf /path/to/somefile.tar.gz file...
-zxf /path/to/somefile.tar.gz -C /path/to/somedir #z可以去掉
-j:bzip2
-jcf /path/to/somefile.tar.bz2 file...
-jxf /path/to/somefile.tar.bz2 -C /path/to/somedir #j可以去掉
-J:xz
-Jcf /path/to/somefile.tar.xz file...
-Jxf /path/to/somefile.tar.xz -C /path/to/somedir #J可以去掉
下面我们就将/tmp/test先使用tar打包成tar文件,再将tar压缩成.xz的压缩文件:
[root@localhost tmp]# tar -cf test.tar test/
[root@localhost tmp]# ll
total
drwxr-xr-x. root root Sep : test
-rw-r--r--. root root Sep : test.tar
[root@localhost tmp]# xz test.tar
[root@localhost tmp]# ll
total
drwxr-xr-x. root root Sep : test
-rw-r--r--. root root Sep : test.tar.xz
使用unxz解压缩,再展开归档至/root下:
[root@localhost tmp]# unxz test.tar.xz #解压缩
[root@localhost tmp]# ll
total
drwxr-xr-x. root root Sep : test
-rw-r--r--. root root Sep : test.tar
[root@localhost tmp]# tar -xf test.tar -C /root/ #展开归档至指定的目录
[root@localhost tmp]# ll /root/
total
-rw-------. root root Aug : anaconda-ks.cfg
drwxr-xr-x. root root Sep : test
这样显得有点麻烦,所有在生产环境中,我们一般直接使用选项-z,-j,-J来实现压缩归档。
[root@localhost tmp]# tar -zcf test.tar.gz test/
[root@localhost tmp]# ll
total
drwxr-xr-x. root root Sep : test
-rw-r--r--. root root Sep : test.tar.gz
[root@localhost tmp]# tar -zxf test.tar.gz -C /root/
[root@localhost tmp]# ll /root/test/
total
-rw-r--r--. root root Sep : functions
-rw-------. root root Sep : messages
-rw-r--r--. root root Sep : passwd
所有两组命令 tar -zcf,tar -zxf 或者 tar -Jcf,tar -Jxf 的是非常好用的,也是最常用的组合。
zip和unzip
一个可以在windows和Linux共用的压缩工具,方便在这两种操作系统之间压缩和解压缩文件,这里就简单的看一下:
[root@localhost tmp]# ll /tmp/test
total 360
-rw-r--r--. 1 root root 15131 Sep 5 21:41 functions
-rw-------. 1 root root 345807 Sep 5 21:41 messages
-rw-r--r--. 1 root root 1117 Sep 5 21:41 passwd
[root@localhost tmp]# zip -r test.zip test #选项-r实现递归压缩
adding: test/ (stored 0%)
adding: test/functions (deflated 69%)
adding: test/passwd (deflated 57%)
adding: test/messages (deflated 90%)
[root@localhost tmp]# unzip test.zip -d /root/ #选项-d可以指定解压缩的路径
Archive: test.zip
creating: /root/test/
inflating: /root/test/functions
inflating: /root/test/passwd
inflating: /root/test/messages
[root@localhost tmp]# ll /root/test
total 360
-rw-r--r--. 1 root root 15131 Sep 5 21:41 functions
-rw-------. 1 root root 345807 Sep 5 21:41 messages
-rw-r--r--. 1 root root 1117 Sep 5 21:41 passwd
【linux相识相知】压缩与打包的更多相关文章
- linux tar命令 压缩、打包、解压 详解
linux tar命令 压缩.打包.解压 详解 1.常用压缩命令 tar –czvf 压缩后的文件.tar.gz 要压缩的文件 2.常用解压命令 tar –xzvf 解压后的文件.tar.gz [要解 ...
- Linux下 目录 压缩 解压缩 打包
http://blog.sina.com.cn/s/blog_7479f7990100zwkp.html tar -zcvf /home/xahot.tar.gz /xahot tar -zcv ...
- Linux基础命令---压缩与打包
GZIP: 普通文件打包成gzip文件:gzip filename(问题:如何测试一个文件是否是gzip文件?) gzip文件解压成普通文件:gzip -d filename(副作用:原始gz文件会被 ...
- Linux下文件压缩与打包
Linux常用压缩命令compresscompress压缩出来的文件的后缀是.Z,解压命令是ucompresscompress -c 文件 > 压缩后的文件名 ,选项-v显示压缩过程,选项-c的 ...
- linux中文件压缩与打包
一.常见的压缩命令 在linux环境中,压缩文件的扩展名大多是*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2,首先我们来介绍以下这些压缩文案的扩展名:. *.Z:compres ...
- 3.Linux 文件的压缩与打包
1.常用压缩打包命令 常用的压缩打包扩展名为如下: *.Z compress 程序压缩的文件,非常老旧了,不再细说 *.gz gzip 程序压缩的文件: *.bz2 bzip2 程序压缩的文件: *. ...
- 【linux相识相知】用户及权限管理
linux系统是多用户(Multi-users)和多任务(Multi-tasks)的,这样的目的是为了一台linux主机可以给很多用户提供服务同时运行多种服务,但是我们是怎么区分每个用户呢?作为一个管 ...
- 【linux相识相知】磁盘分区及文件系统管理详解
磁盘,提供持久的数据存储,它不像我们的内存,如果突然断电了,在内存中的数据一般都会被丢掉了,内存中的数据在保存的时候,会被写到硬盘里面,磁盘也是一种I/O设备. 我们都知道磁盘分区完成之后,还要进行格 ...
- 【linux相识相知】网络属性配置
当我们拥有一个崭新的计算机的时候,第一步恐怕都是迫不及待的下载各种软件,看视频,听音乐等,这里的关键的一点是要有网络.现在的个人计算机大部分都是windows操作系统的,接入网络网络很简单,插上网线也 ...
随机推荐
- 使用jmeter做简单的场景设计
使用jmeter做简单的场景设计 Jmeter: Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试.我之所以选择它,最重要的一点就是----开源 个人 ...
- 【spring boot】FilterRegistrationBean介绍
前言 以往的javaee配置过滤器是在web.xml中配置的,如下代码 <filter> <filter-name>TestFilter</filter-name> ...
- 八大排序算法的python实现(七)基数排序
代码: #coding:utf-8 #author:徐卜灵 import math #print math.ceil(3.2) 向上取整4.0 #print math.floor(3.2) 向下取整3 ...
- Python之路Python文件操作
Python之路Python文件操作 一.文件的操作 文件句柄 = open('文件路径+文件名', '模式') 例子 f = open("test.txt","r&qu ...
- 关于VisualStudio性能分析数据中的独占样本数和非独占样本数的意义
VisualStudio中自带有Profile工具进行性能性能分析,其中用得比较多的数据是函数调用时间,它主要有独占样本数和非独占样本数两个指标,关于这两个指标代表的意义,MSDN的解释比较文艺: 非 ...
- JDK 5 ~ 10 新特性倾情整理!
JDK 5 ~ 10 新特性倾情整理! 最近连 JDK11都在准备发布的路上了,大家都整明白了吗?也许现在大部分人还在用6-8,8的新特性都没用熟,9刚出不久,10-11就不用说了. 为了大家对JDK ...
- 01、前端需要注意哪些SEO?
1.前端需要注意哪些SEO? 1)设置网站TDK标签的设置 2)图片img标签必须加上alt属性 3)h1~h6标签合理使用 4)a标签增加rel="nofollow" 5) 安装 ...
- Android 日历视图(Calendarview)
1.介绍 2.常用属性 3.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...
- sharepoint_study_1
描述:机器上进行SharePoint开发,需要SQL Server提供最基本的服务 解决: SQL Server 的数据库引擎: SQL Server 代理: SQL Server 浏览器组件:
- python-使用字典使Fibonacci更有效率
原代码: def fib(n): if n == 1: return 1 elif n == 2: return 2 else: return fib(n-1)+fib(n-2) 改进后: def f ...