Linux打包与压缩

Linux下常见的压缩文件类型:
.gz .zip .bz2 .xz
结合压缩文件类型,则打包文件:
.tar .tar.gz .tar.bz2 .tar.xz

下面是对这些类型进行详细介绍:

[root@weijie test]# gzip 1.txt 
[root@weijie test]# ls
1.txt.gz
[root@weijie test]# gzip -d 1.txt.gz 
[root@weijie test]# ls
1.txt
[root@weijie test]# gzip -8 1.txt 
[root@weijie test]# ls
1.txt.gz
[root@weijie test]# zcat 1.txt.gz 
root:x:0:0:root,lala,111,11:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@weijie test]# touch 2.txt
[root@weijie test]# echo '111' > 2.txt 
[root@weijie test]# cat 2.txt 
111
[root@weijie test]# gzip -c 2.txt > 1.txt.gz 
[root@weijie test]# zcat 1.txt.gz 
111
[root@weijie test]# gunzip -c 1.txt.gz  >> 2.txt 
[root@weijie test]# ls
1.txt.gz  2.txt
[root@weijie test]# cat 2.txt 
111
111

gzip + 文件名 压缩文件并把源文件删除
解压:gzip -d + 文件名.gz
gzip -n 指定压缩级别 1-9 级 默认是6级
zcat + 文件.gz 不解压并查看文件
gzip -c + 文件 > 文件.gz 将文件压缩并覆盖原来的压缩文件
gzip -c + 文件 >> 文件.gz 将文件压缩并追加到原来的压缩文件后
gunzip -c +文件.gz >> 文件 将文件解压并直接追加到指定文件,但原来的压缩文件仍然存在
gunzip -c +文件.gz > 文件 将文件解压并直接覆盖到指定文件,但原来的压缩文件仍然存在
gzip 不能压缩目录

[root@weijie test]# bzip2 1.txt 
[root@weijie test]# ls
1.txt.bz2  2.txt.bz2
[root@weijie test]# bzip2 -d 2.txt.bz2 
[root@weijie test]# bzip2 -c 2.txt >> 1.txt.bz2 
[root@weijie test]# bzcat 1.txt.bz2 
111
111
111
[root@weijie test]# ls
1.txt.bz2  2.txt
[root@weijie test]# bzip2 2.txt 
[root@weijie test]# ls
1.txt.bz2  2.txt.bz2
[root@weijie test]# bzip2 -d 2.txt.bz2 
[root@weijie test]# ls
1.txt.bz2  2.txt
[root@weijie test]# bzcat 1.txt.bz2 
111
111
111
[root@weijie test]# cat 2.txt 
111
111
[root@weijie test]# bzip2 -c 2.txt >> 1.txt.bz2 
[root@weijie test]# bzcat 1.txt.bz2 
111
111
111
111
111

默认解压级别9,查看压缩文件命令为bzcat,其他的用法和zip,没有很大区别,同样的bzip工具也不能压缩目录。

[root@weijie test]# xz 1.txt 
[root@weijie test]# xz 2.txt 
[root@weijie test]# ls
1.txt.xz  2.txt.xz
[root@weijie test]# xz -d 2.txt.xz 
[root@weijie test]# xz -c 2.txt >> 1.txt.xz 
[root@weijie test]# ls
1.txt.xz  2.txt
[root@weijie test]# xzcat 1.txt.xz 
111
111
111
111
111
111
111

xz的用法也没有多大变化,不解压查看文件命令为xzcat,其他的命令与另外两个工具类似,同样不能压缩目录。

[root@weijie /]# zip test.zip /test/*
  adding: test/1.txt.xz (deflated 24%)
  adding: test/2.txt (stored 0%)
  adding: test/2.txt.zip (stored 0%)
[root@weijie /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  test   test.zip  usr
boot  etc  lib   media  opt  root  sbin  sys  test1  tmp       var
[root@weijie /]# unzip -l test.zip 
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      132  03-01-2020 18:40   test/1.txt.xz
        8  03-01-2020 17:21   test/2.txt
      168  03-01-2020 18:51   test/2.txt.zip
---------                     -------
      308                     3 files

zip工具与其他三种压缩工具有很大区别。
zip + 被压缩文件后的文件名 需要压缩的文件或目录,解压后源文件还是存在
unzip + 压缩文件 解压文件命令(原来的压缩文件还是会存在)
-d 指定路径 解压到指定目录下
unzip -l 不解压查看压缩文件里的文件或目录
这个工具可以压缩目录

[root@weijie test]# ls
1.txt.xz  2.txt  2.txt.zip  abc
[root@weijie test]# tar -cvf abc.tar abc
abc/
abc/2.txt
[root@weijie test]# ls
1.txt.xz  2.txt  2.txt.zip  abc  abc.tar
[root@weijie test]# tar -cvf 1.tar 2.txt abc
2.txt
abc/
abc/2.txt
[root@weijie test]# ls
1.tar  1.txt.xz  2.txt  2.txt.zip  abc  abc.tar
[root@weijie test]# tar -xvf 1.tar 
2.txt
abc/
abc/2.txt
[root@weijie test]# ls
1.tar  1.txt.xz  2.txt  2.txt.zip  abc  abc.tar
[root@weijie test]# tar -tf abc.tar 
abc/
abc/2.txt
[root@weijie /]# tar -cvf test.tar test --exclude 2.txt
test/
test/1.txt.xz
test/2.txt.zip
test/abc/
test/abc.tar
test/1.tar
[root@weijie /]# tar -tf test.tar  | grep '2.txt'
test/2.txt.zip

tar打包工具:
tar -cvf + 指定包名 + 需要打包的文件或者是目录 打包
–exclude 排除指定的文件或者是目录
tar -xvf + 包名 解开包
tar -tf + 包名 不解包但查看包内的目录或文件
c 表示创建包
v 表示可视过程
f 表示强制打包
x 表示解开包

[root@weijie /]# tar -zcvf test.tar.gz test
test/
test/abc/
test/abc/2.txt
test/2.txt
test/1.txt.xz
test/2.txt.zip
test/abc.tar
test/1.tar
[root@weijie /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  test         tmp  var
boot  etc  lib   media  opt  root  sbin  sys  test.tar.gz  usr
[root@weijie /]# tar -zxvf test.tar.gz 
test/
test/abc/
test/abc/2.txt
test/2.txt
test/1.txt.xz
test/2.txt.zip
test/abc.tar
test/1.tar
[root@weijie /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  test         tmp  var
boot  etc  lib   media  opt  root  sbin  sys  test.tar.gz  usr

打包并压缩:
z 用gzip工具进行压缩或解压
j 用bzip2工具进行压缩或解压
J 用xz工具进行压缩或解压

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值