Linux 解压缩 gz bz2 xz 归档tar 命令

Linux的压缩格式有xz,bz2,gz等,不同的压缩格式有着不同的压缩算法,所以压缩的比例也不相同。

gzip工具压缩出来后.gz,bzip2工具压缩后为.bz2,xz工具压缩后.xz。这些工具只能压缩文件不能压缩目录,如果传递文件参数,会将目录里面的文件逐个进行压缩。默认这些命令再压缩之后会删除原文件,只保留压缩后的文件

 

(1).gz

gzip工具压缩

[root@localhost ~]# ls -lh messages  --可以看到该文件195K

-rw-------. 1 root root 195K Feb 26 16:43 messages

[root@localhost ~]# gzip messages

[root@localhost ~]# ls -lh messages.gz   --压缩完之后23K

-rw-------. 1 root root 23K Feb 26 16:43 messages.gz

[root@localhost ~]# ls  --可以看到原文件没有了

anaconda-ks.cfg  messages.gz

可以看到对文本文件压缩压缩比还是非常大的,但是压缩完之后会删除原文件,而且不能压缩目录,如果压缩目录会将目录下面的每一个文件进行压缩。

gzip /path/to/file_name1  file_name2   后面可以指定多个文件,那么这样会将多个文件逐个进行压缩

gunzip解压缩

[root@localhost ~]# gunzip messages.gz --gunzip后面跟上.gz文件

[root@localhost ~]# ls  --可以看到解压缩之后.gz压缩文件也被删除了

anaconda-ks.cfg messages

当然了,解压缩也可以使用gzip -d来解压缩,相当于gunzip

[root@localhost ~]# gzip  messages

[root@localhost ~]# gzip  -d messages.gz

[root@localhost ~]# ls

anaconda-ks.cfg   messages

如果想要看压缩文件里面的内容,但是不解压可以使用zcat,zcat messages.gz

 

(2)bzip2(和gizp类似)

比gizp有着更大的压缩比的压缩工具,及压缩后的文件更小。和gzip一样只能压缩文件,不能压缩目录。

[root@localhost ~]# bzip2 ansible.repo  --bizp2使用格式就是后面加上文件名

[root@localhost ~]# ls  --压缩后的文件是以.bz2结尾的

anaconda-ks.cfg  ansible.repo.bz2  

[root@localhost ~]# bzip2 -d ansible.repo.bz2  --也支持-d解压,解压也会删除压缩文件

[root@localhost ~]# ls

anaconda-ks.cfg  ansible.repo  



[root@localhost ~]# bzip2 -k ansible.repo  --使用-k选项可以压缩时候保存原文件不被删除

[root@localhost ~]# ls

anaconda-ks.cfg  ansible.repo  ansible.repo.bz2



[root@localhost ~]# bunzip2 ansible.repo.bz2  --也可以使用bunzip进行解压

[root@localhost ~]# ls

anaconda-ks.cfg  ansible.repo

最后和gizp一样也可以使用bzcat对压缩文件的内容进行查看

 

(3)xz

[root@localhost ~]# man xz  --可以看到xz的使用方法和gzip,bzip2类似

DESCRIPTION

xz is a general-purpose data compression tool with command line syntax similar to gzip(1) and bzip2(1).

[root@localhost ~]# xz messages

[root@localhost ~]# ls

anaconda-ks.cfg   messages.xz



[root@localhost ~]# xz -d messages.xz

[root@localhost ~]# ls

anaconda-ks.cfg   messages



[root@localhost ~]# xz -k messages

[root@localhost ~]# ls

anaconda-ks.cfg   messages  messages.xz

uxz解压和xzcat来查看

xz,bzip2,gzip压缩比不一样,xz压缩后更小,其次bzip2,最后gzip。

 

(4)zip

这个和上面不一样,这个可以用来压缩目录,zip后面跟上压缩后保留的文件名,参数可以是多个文件中间空格隔开,或者目录(即归档又压缩的工具)

[root@localhost test]# zip test.zip /test/*

  adding: test/a.txt (stored 0%)

  adding: test/b.txt (stored 0%)

  adding: test/c.txt (stored 0%)

[root@localhost test]# ls --可以看到zip压缩后不删除源文件

a.txt  b.txt  c.txt   test.zip

[root@localhost test]# zip new.zip a.txt b.txt  c.txt  

  adding: a.txt (stored 0%)

  adding: b.txt (stored 0%)

  adding: c.txt (stored 0%)

[root@localhost test]# ls

a.txt  b.txt  c.txt   new.zip  test.zip

解压的命令是unzip

 

tar命令

 

tar 由 'Tape archiver(磁带归档器)' 衍生而来,最初被用来在磁带上归档和存储文件。Tar 是一个 GNU 软件,它可以压缩一组文件(归档),或提取它们以及对已有的归档文件进行相关操作。在存储、备份以及传输文件方面,它是很有用的。在创建归档文件时,Tar 可以保持原有文件和目录结构不变。通过 Tar 归档的文件的后缀名为 ‘.tar’。

 

-c表示将多个文件归档到一起

-f 表示归档后要指定归档的名字,如.tar,f后面必须跟上xxx.tar

-x 将归档进行展开

--xattrs  归档时,保留文件扩展属性信息,这个在备份时候有用

[root@localhost test]# ls

libusal-1.1.11-23.el7.x86_64.rpm  libusb-0.1.4-3.el7.i686.rpm  libusb-0.1.4-3.el7.x86_64.rpm

[root@localhost test]# tar -cf test.tar  *.rpm

[root@localhost test]# ls

libusal-1.1.11-23.el7.x86_64.rpm  libusb-0.1.4-3.el7.i686.rpm  libusb-0.1.4-3.el7.x86_64.rpm  test.tar



[root@localhost test]# tar -xf test.tar   --可以看到.tar文件还是存在的

[root@localhost test]# ls

libusal-1.1.11-23.el7.x86_64.rpm  libusb-0.1.4-3.el7.i686.rpm  libusb-0.1.4-3.el7.x86_64.rpm  test.tar



[root@localhost ~]# tar -tf test.tar  --查看归档里文件

httpd-2.4.6-90.el7.centos.x86_64.rpm

httpd-tools-2.4.6-90.el7.centos.x86_64.rpm

mailcap-2.1.41-2.el7.noarch.rpm

可以看到创建归档使用cf ,解压归档使用xf

 

有些时候文件归档了还需要压缩

归档压缩的两种方式

(1)先归档,然后根据使用哪种压缩方式gzip,bizp2,xz 自己选择压缩方式

[root@localhost ~]# tar -cf rpm.tar  *.rpm

[root@localhost ~]# ls

httpd-2.4.6-90.el7.centos.x86_64.rpm  httpd-tools-2.4.6-90.el7.centos.x86_64.rpm  mailcap-2.1.41-2.el7.noarch.rpm  rpm.tar

[root@localhost ~]# xz rpm.tar   --对于归档的.tar包还可以使用gz,bzip2,xz再次压缩

[root@localhost ~]# ls

  httpd-2.4.6-90.el7.centos.x86_64.rpm  httpd-tools-2.4.6-90.el7.centos.x86_64.rpm  mailcap-2.1.41-2.el7.noarch.rpm  rpm.tar.xz

(2)归档压缩一步到位,tar命令调用压缩命令。

使用-zcf,z表示直接调用gzip,即归档又压缩。使用-zxf先解压,后展开归档,filename.tar.gz

使用-jcf 调用bizp2 -jxf同理 filename.tar.bz2

使用-Jcf 调用xz -Jcf同理   filename.tar.xz

[root@localhost ~]# tar -Jcf rpm.tar.xz *.rpm  --归档压缩

[root@localhost ~]# ls

httpd-2.4.6-90.el7.centos.x86_64.rpm  httpd-tools-2.4.6-90.el7.centos.x86_64.rpm  mailcap-2.1.41-2.el7.noarch.rpm  rpm.tar.xz



[root@localhost ~]# rm -rf *.rpm  --解压缩,展开归档

[root@localhost ~]# tar -Jxf rpm.tar.xz

[root@localhost ~]# ls

anaconda-ks.cfg  httpd-2.4.6-90.el7.centos.x86_64.rpm  httpd-tools-2.4.6-90.el7.centos.x86_64.rpm  mailcap-2.1.41-2.el7.noarch.rpm  rpm.tar.xz

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值