Linux基础-压缩与解压缩

1.压缩包格式

  1. Windows
    rar
    zip
  2. Linux
    zip
    tar.gz
    tar.bz2
    tar.xz

  3. 压缩的好处主要有:
  4. 节省磁盘空间占用率
  5. 节省网络传输带宽消耗
  6. 网络传输更加快捷

  7. Linux系统常见的后缀名所对应的压缩工具
    .gz -- gzip压缩工具压缩的文件
    .bz2 -- bzip2压缩工具压缩的文件
    .tar -- tar没有压缩功能,只是把一个目录合并成一个文件
    .tar.gz -- 先使用tar打包,然后再使用gzip压缩归档
    .tar.bz2 -- 先使用tar打包,然后再使用bzip压缩归档
    .tar.xz -- 先使用tar打包,然后使用xz压缩归档

注意:
1. Linux下常用压缩文件以tar.gz结尾

  1. Linux下压缩文件必须带后缀

  2. TAR归档工具:
    tar是linux下最常用的压缩和解压缩,支持文件和目录的压缩
    原始含义是归档,不带压缩功能
    语法:
    tar [-zjxcvfpP] filename
    c -- 创建新的归档文件
    x -- 对归档文件解包
    t -- 列出归档文件里的文件列表
    v -- 输出命令的归档或解包的过程
    f -- 指定包文件名,多参数f写在最后
    C -- 指定解压目录位置
    z -- 使用gzip压缩归档后的文件(tar.gz)
    j -- 使用bzip2压缩归档后的文件(tar.bz2)
    J -- 使用xz压缩归档后的文件(tar.xz)
    X -- 排除多个文件(写入需要排除的文件名称)
    p -- 创建压缩归档文件时,保留源文件的权限
    h -- 打包软链接
    --hard-dereference -- 打包硬链接
    --exclude -- 在打包的时候写入需要排除文件或目录

按照选项的行为分为四类:
.tar .tar.gz .tar.bz2 .tar.xz
cf czf cjf cJf
tf tf tf tf
xf xf xf xf
--常用打包和压缩解压组合

安装gzip,bzip2,xz软件包
yum install -y gzip bzip2 xz

[root@localhost test]# touch oldboy{1..10}.txt
[root@localhost test]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy10.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
[root@localhost test]# tar -cf oldboy.tar oldboy?.txt
[root@localhost test]# tar -czf oldboy.tar.gz oldboy?.txt
[root@localhost test]# tar -cjf oldboy.tar.bz2 oldboy?.txt
[root@localhost test]# tar -cJf oldboy.tar.xz oldboy?.txt
[root@localhost test]# ll -h
total 24K
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy10.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
-rw-r--r-- 1 root root 10K Mar 12 07:44 oldboy.tar
-rw-r--r-- 1 root root 196 Mar 12 07:46 oldboy.tar.bz2
-rw-r--r-- 1 root root 183 Mar 12 07:46 oldboy.tar.gz
-rw-r--r-- 1 root root 216 Mar 12 07:46 oldboy.tar.xz
[root@localhost test]#

[root@localhost test]# file oldboy.tar
oldboy.tar: POSIX tar archive (GNU)
[root@localhost test]# file oldboy.tar.gz
oldboy.tar.gz: gzip compressed data, from Unix, last modified: Tue Mar 12 07:46:07 2019
[root@localhost test]# file oldboy.tar.bz2
oldboy.tar.bz2: bzip2 compressed data, block size = 900k
[root@localhost test]# file oldboy.tar.xz
oldboy.tar.xz: XZ compressed data
[root@localhost test]#

-- 查看压缩内容
[root@localhost test]# tar -tf oldboy.tar.gz
oldboy1.txt
oldboy2.txt
oldboy3.txt
oldboy4.txt
oldboy5.txt
oldboy6.txt
oldboy7.txt
oldboy8.txt
oldboy9.txt
[root@localhost test]# tar -tf oldboy.tar.bz2
oldboy1.txt
oldboy2.txt
oldboy3.txt
oldboy4.txt
oldboy5.txt
oldboy6.txt
oldboy7.txt
oldboy8.txt
oldboy9.txt
[root@localhost test]# tar -tf oldboy.tar.xz
oldboy1.txt
oldboy2.txt
oldboy3.txt
oldboy4.txt
oldboy5.txt
oldboy6.txt
oldboy7.txt
oldboy8.txt
oldboy9.txt
[root@localhost test]#

--解压压缩包
[root@localhost test]# ll
total 24
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy10.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
-rw-r--r-- 1 root root 10240 Mar 12 07:44 oldboy.tar
-rw-r--r-- 1 root root 196 Mar 12 07:46 oldboy.tar.bz2
-rw-r--r-- 1 root root 183 Mar 12 07:46 oldboy.tar.gz
-rw-r--r-- 1 root root 216 Mar 12 07:46 oldboy.tar.xz
[root@localhost test]# rm -f *.txt
[root@localhost test]# ll
total 24
-rw-r--r-- 1 root root 10240 Mar 12 07:44 oldboy.tar
-rw-r--r-- 1 root root 196 Mar 12 07:46 oldboy.tar.bz2
-rw-r--r-- 1 root root 183 Mar 12 07:46 oldboy.tar.gz
-rw-r--r-- 1 root root 216 Mar 12 07:46 oldboy.tar.xz
[root@localhost test]# tar -xf oldboy.tar.gz
[root@localhost test]# ll
total 24
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
-rw-r--r-- 1 root root 10240 Mar 12 07:44 oldboy.tar
-rw-r--r-- 1 root root 196 Mar 12 07:46 oldboy.tar.bz2
-rw-r--r-- 1 root root 183 Mar 12 07:46 oldboy.tar.gz
-rw-r--r-- 1 root root 216 Mar 12 07:46 oldboy.tar.xz
[root@localhost test]#

-- 参数大C,将压缩包中的内容解压到指定目录下
[root@localhost test]# ll ../temp
total 0
[root@localhost test]# tar -xf oldboy.tar.xz -C ../temp
[root@localhost test]# ll ../temp
total 0
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
[root@localhost test]#

-- 创建压缩文件,排除单个文件
[root@localhost test]# tar -zcf oldboy1.tar.gz --exclude=oldboy5.txt ./*
[root@localhost test]# tar -tf oldboy1.tar.gz
./oldboy1.txt
./oldboy2.txt
./oldboy3.txt
./oldboy4.txt
./oldboy6.txt
./oldboy7.txt
./oldboy8.txt
./oldboy9.txt
./oldboy.tar.gz
[root@localhost test]#

-- 创建压缩文件,排除多个文件,需要建立一个文件列表,将需要排除的所有文件的路径写入该
文件,创建压缩归档时使用'X'选项
[root@localhost test]# ll
total 12
-rw-r--r-- 1 root root 36 Mar 12 08:12 exclude.list
-rw-r--r-- 1 root root 451 Mar 12 08:08 oldboy1.tar.gz
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
-rw-r--r-- 1 root root 203 Mar 12 08:06 oldboy.tar.gz
[root@localhost test]# cat exclude.list
oldboy2.txt
oldboy3.txt
oldboy4.txt
[root@localhost test]# tar -czfX oldboy2.tar.gz exclude.list oldboy*.txt
[root@localhost test]# tar -tf oldboy2.tar.gz
oldboy1.txt
oldboy5.txt
oldboy6.txt
oldboy7.txt
oldboy8.txt
oldboy9.txt
[root@localhost test]# ll
total 16
-rw-r--r-- 1 root root 36 Mar 12 08:12 exclude.list
-rw-r--r-- 1 root root 451 Mar 12 08:08 oldboy1.tar.gz
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy1.txt
-rw-r--r-- 1 root root 161 Mar 12 08:19 oldboy2.tar.gz
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy2.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy3.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy4.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy5.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy6.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy7.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy8.txt
-rw-r--r-- 1 root root 0 Mar 12 07:43 oldboy9.txt
-rw-r--r-- 1 root root 203 Mar 12 08:06 oldboy.tar.gz
[root@localhost test]#

//压缩后删除源文件
[root@wll ~]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy1.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy2.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy3.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy4.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy5.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy6.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy7.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy8.txt
-rw-r--r-- 1 root root 0 Nov 13 06:40 oldboy9.txt
[root@wll ~]# tar -zcvf oldboy.tgz oldboy* --remove-files
oldboy1.txt
oldboy2.txt
oldboy3.txt
oldboy4.txt
oldboy5.txt
oldboy6.txt
oldboy7.txt
oldboy8.txt
oldboy9.txt
[root@wll ~]# ll
total 4
-rw-r--r-- 1 root root 182 Nov 13 06:41 oldboy.tgz
[root@wll ~]# tar -tf oldboy.tgz
oldboy1.txt
oldboy2.txt
oldboy3.txt
oldboy4.txt
oldboy5.txt
oldboy6.txt
oldboy7.txt
oldboy8.txt
oldboy9.txt
[root@wll ~]#

上一篇:矩阵优化DP类问题应用向小结


下一篇:POJ-2065-SETI(高斯消元)