Linux压缩和打包

一、压缩

1、什么是压缩?

  压缩是利用算法将文件有损或无损地处理,以达到保留最多文件信息,而令文件体积变小
  那么是如何实现压缩的呢?计算机处理的信息是以二进制的形式表示的,且使用bytes单位计量,但实际上计算机中最小的单位是bits,大家都知道1byte=8bits,那么在记录一个数字1的时候是如何存储的呢?" 00000001 "实际上前面7位都是空的,但要满足我们操作系统的存取方式,必须以8为为单位存储,所以造成有一些空间并未填满。压缩就是将这些没有使用到的空间丢出来,让文件的占用空间变小。

  常见压缩解压缩命令:

压 缩 解压缩 查看压缩文件内容 压缩文件后缀名
zip uzip zcat / zless .zip
gzip gunzip zcat / zless .gz
bzip2 bunzip2 bzcat / bzless .bz2
xz unxz xzcat / xzless .xz

2、常见压缩命令

① zip命令

  • zip简介
      zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有".zip"扩展名的压缩文件。
  • 命令语法
      zip [选项]… 压缩包名 源文件或源目录
  • 命令常用选项
选 项 解 释
-r 递归处理,将指定目录下的所有文件和子目录一并压缩
-m 将文件压缩之后,删除原始文件,即把文件移到压缩文件中
-d 从压缩文件内删除指定文件
-v 显示压缩过程或版本信息
-q 不显示压缩过程
-u 更新较新的文件到压缩文件中
-压缩级别 压缩级别是从 1~9 的数值,数值越小压缩速度更快,数值越大压缩效果更好
  • 实例

  Ⅰ、同时压缩多个文件;

[root@localhost tmp]# zip test.zip boot.log maillog
  adding: boot.log (deflated 64%)
  adding: maillog (deflated 77%)
[root@localhost tmp]# ls -lh    //可以看出zip在压缩多个文件的同时会将它们一并打包
total 12K
-rw-r--r--. 1 root root 1.3K Mar 30 19:46 boot.log
-rw-------. 1 root root  788 Mar 30 19:46 maillog
`-rw-r--r--. 1 root root  953 Mar 30 20:03 test.zip`

  Ⅱ、向压缩文件test.zip中添加/var/log/secure文件;

[root@localhost tmp]# zip test.zip /var/log/secure
  adding: var/log/secure (deflated 86%)
[root@localhost tmp]# ls -lh
total 12K
-rw-r--r--. 1 root root 1.3K Mar 30 19:46 boot.log
-rw-------. 1 root root  788 Mar 30 19:46 maillog
`-rw-r--r--. 1 root root 1.6K Mar 30 20:14 test.zip`

  Ⅲ、从压缩文件test.zip中添删除/var/log/secure文件;

[root@localhost tmp]# zip -d test.zip /var/log/secure
deleting: var/log/secure
[root@localhost tmp]# ls -lh
total 12K
-rw-r--r--. 1 root root 1.3K Mar 30 19:46 boot.log
-rw-------. 1 root root  788 Mar 30 19:46 maillog
`-rw-r--r--. 1 root root  953 Mar 30 20:17 test.zip`

② unzip命令

  • unzip简介
      unzip命令可以查看和解压缩".zip"文件。
  • 命令语法
      unzip [选项]… 压缩文件
  • 命令常用选项
选 项 解 释
-d 目录名 将压缩文件解压到指定目录下
-n 解压时并不覆盖已经存在的文件
-o 解压时覆盖已经存在的文件,无需确认。
-l 列出压缩文件内所包含的文件
-v 列出压缩文件的详细信息,包括压缩文件中包含的文件大小、文件名以及压缩比等
-t 检测压缩文件完整性
  • 实例

  Ⅰ、查看压缩文件内所包含的文件;

[root@localhost tmp]# unzip -l test.zip    //-l选项
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     1283  03-30-2020 19:46   boot.log
      788  03-30-2020 19:46   maillog
---------                     -------
     2071                     2 files

[root@localhost tmp]# unzip -v test.zip    //-v选项
Archive:  test.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
    1283  Defl:N      462  64% 03-30-2020 19:46 4738bcbd  boot.log
     788  Defl:N      183  77% 03-30-2020 19:46 56a6c26f  maillog
--------          -------  ---                            -------
    2071              645  69%                            2 files
    

  Ⅱ、解压压缩文件至指定目录;

[root@localhost tmp]# unzip -d testdir/ test.zip
Archive:  test.zip
  inflating: testdir/boot.log
  inflating: testdir/maillog
[root@localhost tmp]# cd testdir/
[root@localhost testdir]# ls
boot.log  maillog

③ gzip命令

  • gzip简介
      gzip是Linux系统中经常使用的一个对文件进行压缩和解压的命令,只能用来压缩文件(如果指定目录,则会压缩该目录下的所有文件),文件经它压缩后会产生具有".gz"扩展名的压缩文件。
  • 命令语法
      gzip [选项]… 源文件
  • 命令常用选项
选 项 解 释
-r 递归处理,指定目录下的所有文件和子目录分别压缩
-c 将压缩数据输出到标准输出,并保留源文件
-l 列出压缩文件的相关信息(压缩文件大小、未压缩文件大小、压缩比率、未压缩文件名称)
-v 显示指令执行过程
-d 对".gz"文件进行解压缩
-压缩级别 压缩级别是从 1~9 的数值,数值越小压缩速度更快,数值越大压缩效果更好,默认为 6
  • 实例

  Ⅰ、压缩文件;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# ls
testfile
[root@localhost tmp]# gzip testfile    //使用gzip压缩
[root@localhost tmp]# ls    //可以看出gzip在生成压缩文件的同时会删除源文件
testfile.gz

  Ⅱ、将压缩数据输出到标准输出;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# echo "123456" > testfile    //向文件中输入数据
[root@localhost tmp]# gzip -c testfile    //将压缩数据输出到标准输出
▒h▒^testfile342615▒▒%

  Ⅲ、压缩并保留源文件;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# gzip -c testfile > testfile.gz    //将压缩数据重定向到压缩文件中,这样就可以实现在压缩的同时不删除源文件
[root@localhost tmp]# ls
testfile  testfile.gz

  Ⅳ、压缩目录;

[root@localhost tmp]# mkdir testdir    //创建测试目录
[root@localhost tmp]# touch testdir/file1 testdir/file2 testdir/file3 testdir/file4    //创建测试文件
[root@localhost tmp]# ls
`testdir`
[root@localhost tmp]# ls testdir/
`file1  file2  file3  file4`
[root@localhost tmp]# gzip -r testdir/    //递归压缩目录
[root@localhost tmp]# ls    //可以看出,目录依然存在且并未变成压缩文件
`testdir`
[root@localhost tmp]# ls testdir/    //将目录下的所有文件分别压缩,且并未作打包处理
`file1.gz  file2.gz  file3.gz  file4.gz`

④ gunzip命令

  • gunzip简介
      gunzip命令用来解压缩".gz"文件。
  • 命令语法
      gunzip [选项]… 文件
  • 命令常用选项
选 项 解 释
-r 递归处理,将指定目录下的所有文件和子目录分别解压缩
-c 将解压缩数据输出到标准输出,并保留源压缩文件
-f 强制解压缩文件
-l 列出压缩文件内容
-v 显示指令执行过程
-t 检测压缩文件完整性
  • 实例

  解压缩文件;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# gzip testfile    //压缩
[root@localhost tmp]# ls
testfile.gz
[root@localhost tmp]# gunzip testfile    //解压缩
[root@localhost tmp]# ls
testfile

⑤ bzip2命令

  • bzip2简介
      bzip2与gzip类似,都只能用来压缩文件,且压缩任务完成后不会保留源文件,但".bz2"格式的压缩比率相对较高,压缩速度相对较慢,文件经它压缩后会另外具有".bz2"扩展名的压缩文件。
  • 命令语法
      bzip2 [选项]… 源文件
  • 命令常用选项
选 项 解 释
-c 将压缩数据输出到标准输出,并保留源文件
-f 执行压缩或解压缩时,若输出文件与现有文件同名时,默认不会覆盖现有文件,使用 -f 则会强制覆盖现有文件
-k 执行压缩或解压缩任务后,保留源文件
-v 显示指令执行过程
-d 对".bz2"文件进行解压缩
-压缩级别 压缩级别是从 1~9 的数值,数值越小压缩速度更快,数值越大压缩效果更好 ,默认为 6
-t 检测压缩文件完整性
  • 实例

  Ⅰ、压缩并保留源文件;

`//方法一`
[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# bzip2 -k testfile    //压缩
[root@localhost tmp]# ls
testfile  testfile.bz2
`//方法二`
[root@localhost tmp]# bzip2 -c testfile > testfile1.bz2    //将压缩数据重定向到压缩文件中,这样就可以实现在压缩的同时不删除源文件
[root@localhost tmp]# ls
testfile  testfile1.bz2  testfile.bz2

  Ⅱ、将压缩数据输出到标准输出;

[root@localhost tmp]# bzip2 -c testfile
bzip2: I won't write compressed data to a terminal.    //什么意思呢?我不会将压缩数据写入终端。
bzip2: For help, type: `bzip2 --help'.    //也许选项-c的意义就是使用重定向执行压缩并保留源文件

⑥ bunzip2命令

  • bunzip2简介
      bunzip2命令用来解压缩".bz2"文件。
  • 命令语法
      bunzip2 [选项]… 压缩文件
  • 命令常用选项
选 项 解 释
-c 将解压缩数据输出到标准输出,并保留源压缩文件
-f 执行解压缩时,若输出文件与现有文件同名时,默认不会覆盖现有文件,使用 -f 则会强制覆盖现有文件
-k 执行解压缩任务后,保留源压缩文件
-v 显示指令执行过程
-L 显示版本信息
  • 实例

  Ⅰ、解压缩文件;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# bzip2 testfile    //压缩
[root@localhost tmp]# ls
testfile.bz2
[root@localhost tmp]# bunzip2 testfile.bz2    //解压缩
[root@localhost tmp]# ls
testfile

  Ⅱ、将解压缩数据输出到标准输出;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# echo "123456" > testfile    //向文件中输入数据
[root@localhost tmp]# bzip2 testfile    //压缩
[root@localhost tmp]# bunzip2 -c testfile.bz2
123456

⑦ xz命令

  • xz简介
      xz是一个很少见到的,但绝大多数Linux默认自带的一个压缩工具,与gzip和bzip2同样,都支持多文件压缩,但三者而言xz的压缩比率是最高的。
  • 命令语法
      xz [选项]… 源文件
  • 命令常用选项
选 项 解 释
-c 将压缩数据输出到标准输出,并保留源文件
-f 执行压缩或解压缩时,若输出文件与现有文件同名时,默认不会覆盖现有文件,使用 -f 则会强制覆盖现有文件
-k 执行压缩或解压缩任务后,保留源文件
-v 显示指令执行过程
-d 对".xz"文件进行解压缩
-压缩级别 压缩级别是从 1~9 的数值,数值越小压缩速度更快,数值越大压缩效果更好,默认为 6
-t 检测压缩文件完整性
  • 实例

  压缩文件;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# xz testfile    //压缩
[root@localhost tmp]# ls
testfile.xz

⑧ unxz命令

  • unxz简介
      unxz命令用来解压缩".xz"文件。
  • 命令语法
      unxz [选项]… 压缩文件
  • 命令常用选项
选 项 解 释
-c 将解压缩数据输出到标准输出,并保留源压缩文件
-k 执行解压缩任务后,保留源压缩文件
-v 显示指令执行过程
  • 实例

  解压缩文件;

[root@localhost tmp]# touch testfile    //创建测试文件
[root@localhost tmp]# xz testfile    //压缩
[root@localhost tmp]# ls
testfile.xz
[root@localhost tmp]# unxz testfile.xz    //解压缩
[root@localhost tmp]# ls
testfile

二、打包(归档)

1、什么是打包?

  打包(归档)是将多个文件和目录集中存储在一个文件中。归档文件没有经过压缩,因此,它占用的空间是其中所有文件和目录的总和。
  压缩是为了节省存储空间,那么打包的目的是什么呢?那是因为多数压缩程序只能针对一个文件进行压缩,如果需要压缩一大堆文件时,就得先将这一大堆文件打成一个包,然后再进行压缩。

2、tar命令

  • 命令解释
      tar 命令可以把一大堆的文件和目录打包成一个文件,同时也可以进行解打包。使用 tar 命令归档的包通常称为 tar 包(tar 包文件都是以“.tar”结尾的)。
  • 命令语法
      tar [选项…] [文件]…
  • 命令常用选项

  ① 打包解包选项

选 项 解 释
-c 将多个文件或目录进行打包操作
-x 对 tar 包做解打包操作
-A 追加 tar 文件到 tar 包
-r 追加文件至 tar 包结尾
-u 更新原 tar 包中的文件
-t 查看 tar 包中有哪些文件或目录
-v 显示打包文件过程
-o 文件解打包后到标准输出
-N [date / file] 目录 打包指定目录中比date时间或者比file时间更新的文件
-f 包名 指定包的文件名,这是选项必须使用的并且位置必须在最后一个,后跟包名

  ② 压缩 / 解压缩选项

选 项 解 释
-z 压缩或解压缩 “.tar.gz” 格式
-j 压缩或解压缩 ".tar.bz2"格式
-J 压缩或解压缩 ".tar.xz"格式
  • 实例

  Ⅰ、打包文件和目录;

[root@localhost tmp]# touch testfile{1,2,3,4}    //创建测试文件
[root@localhost tmp]# mkdir testdir    //创建测试目录
[root@localhost tmp]# tar -cvf test.tar testfile{1,2,3,4} testdir/    //打包为test.tar文件
testfile1
testfile2
testfile3
testfile4
testdir/
[root@localhost tmp]# ls
testdir  testfile1  testfile2  testfile3  testfile4  test.tar

  Ⅱ、打包并使用gzip命令压缩文件和目录;

[root@localhost tmp]# touch testfile{1,2,3,4}    //创建测试文件
[root@localhost tmp]# mkdir testdir    //创建测试目录
[root@localhost tmp]# tar -zcvf test.tar.gz testfile{1,2,3,4} testdir/    //打包并压缩
testfile1
testfile2
testfile3
testfile4
testdir/
[root@localhost tmp]# ls -lh    //压缩后的包只有177byte大小
total 20K
drwxr-xr-x. 2 root root 4.0K Apr  1 07:03 testdir
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile1
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile2
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile3
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile4
-rw-r--r--. 1 root root  10K Apr  1 07:12 test.tar
-rw-r--r--. 1 root root  177 Apr  1 07:11 test.tar.gz

  Ⅲ、解压缩解打包test.tar.gz文件;

[root@localhost tmp]# rm -rf testfile* testdir    //删除测试文件
[root@localhost tmp]# ls
test.tar  test.tar.gz
[root@localhost tmp]# tar -zxvf test.tar.gz    //解压缩解打包,只需将选项-c换成-x即可
testfile1
testfile2
testfile3
testfile4
testdir/
[root@localhost tmp]# ls
testdir  testfile1  testfile2  testfile3  testfile4  test.tar  test.tar.gz

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章