6.文件压缩和打包

Linux文件压缩和打包

gzip压缩工具

格式: gzip [-d # -c] filename 其中#为1~9的数字.

  • -d 改参数是解压缩时使用
  • -# 表示压缩级别,1为最差, 9为最好; 默认是6
  • -c 压缩的文件不消失; 指定压缩到哪个目录

不可以压缩目录

	[root@lz-01 tmp]# mkdir d6z
	[root@lz-01 tmp]# cd d6z/
	[root@lz-01 d6z]# ls
	[root@lz-01 d6z]# find /etc/ -type f -name "*conf"  //在etc中找到全部结尾conf文件并列出
	...
	[root@lz-01 d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;  //追加到1.txt
	[root@lz-01 d6z]# ls
	1.txt
	...
	[root@lz-01 d6z]# du -sh 1.txt 
	3.1M	1.txt
	[root@lz-01 d6z]# wc -l 1.txt 
	31510 1.txt
	[root@lz-01 d6z]# gzip 1.txt
	[root@lz-01 d6z]# ls
	1.txt.gz
	[root@lz-01 d6z]# du -sh 1.txt.gz 
	284K	1.txt.gz
	[root@lz-01 d6z]# gzip -d 1.txt.gz 
	[root@lz-01 d6z]# ls
	1.txt
	[root@lz-01 d6z]# du -sh 1.txt 
	1.2M	1.txt
	[root@lz-01 d6z]# wc -l 1.txt 
	31510 1.txt
	[root@lz-01 d6z]# gzip -1 1.txt 
	[root@lz-01 d6z]# du -sh 1.txt.gz 
	328K	1.txt.gz
	[root@lz-01 d6z]# 默认6级别^C
	[root@lz-01 d6z]# gunzip 1.txt.gz    //解压缩
	[root@lz-01 d6z]# ls
	1.txt
	[root@lz-01 d6z]# gzip -9 1.txt 
	[root@lz-01 d6z]# file 1.txt.gz 
	1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Mar 29 07:15:33 2019, max compression
	[root@lz-01 d6z]# zcat 1.txt.gz //查看压缩文件
	[root@lz-01 d6z]# ls
	1.txt.gz
	[root@lz-01 d6z]# gzip -d 1.txt.gz 
	[root@lz-01 d6z]# gzip -c 1.txt > /tmp/1.txt.gz
	[root@lz-01 d6z]# ls
	1.txt
	[root@lz-01 d6z]# ls /tmp/1.txt.gz 
	/tmp/1.txt.gz
	[root@lz-01 d6z]# file !$
	file /tmp/1.txt.gz
	/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Mar 29 07:15:33 2019
	[root@lz-01 d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt
	[root@lz-01 d6z]# ls
	1.txt  2.txt
	[root@lz-01 d6z]# wc -l 1.txt 2.txt 
	  31510 1.txt
	  31510 2.txt
	  63020 总用量
	[root@lz-01 d6z]# du -sh *.txt
	1.2M	1.txt
	1.2M	2.txt
	[root@lz-01 d6z]# 
bzip2压缩工具

格式: bzip2 [-d # c] filename

  • -d 解压缩
  • -# 压缩级别1~9,默认为9.
  • -c 压缩的文件不消失; 指定压缩到哪个目录

不可以压缩目录

	[root@lz-01 d6z]# ls
	1.txt  2.txt  3.txt
	[root@lz-01 d6z]# bzip2 1.txt 
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  3.txt
	[root@lz-01 d6z]# bzip2 -d 1.txt.bz2 
	[root@lz-01 d6z]# ls
	1.txt  2.txt  3.txt
	[root@lz-01 d6z]# bzip2 -c 1.txt > /tmp/1.txt.bz2
	[root@lz-01 d6z]# ls
	1.txt  2.txt  3.txt
	[root@lz-01 d6z]# du -sh /tmp/1.txt.bz2
	80K	/tmp/1.txt.bz2
	[root@lz-01 d6z]# bzip2 -d -c /tmp/1.txt.bz2 > 4.txt
	[root@lz-01 d6z]# ls
	1.txt  2.txt  3.txt  4.txt
	[root@lz-01 d6z]# bunzip2  1.txt.bz2// 解压缩   ^C 
	[root@lz-01 d6z]# bzip2 1.txt 
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  3.txt  4.txt
	[root@lz-01 d6z]# file 1.txt.bz2 
	1.txt.bz2: bzip2 compressed data, block size = 900k
	[root@lz-01 d6z]# file 2.txt 
	2.txt: UTF-8 Unicode text
	[root@lz-01 d6z]# bzcat 1.txt.bz2   //查看压缩文件内容
xz压缩工具

格式: xz [-d # c] filename 和bizp2, gzip 类似

不可以压缩目录

	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  3.txt  4.txt
	[root@lz-01 d6z]# xz 2.txt 
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt.xz  3.txt  4.txt
	[root@lz-01 d6z]# du -sh 2.txt.xz 
	28K	2.txt.xz
	[root@lz-01 d6z]# xz -d 2.txt.xz 
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  3.txt  4.txt
	[root@lz-01 d6z]# 

	[root@lz-01 d6z]# xz -c 2.txt > /tmp/2.txt.xz
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  3.txt  4.txt
	[root@lz-01 d6z]# ls /tmp/2.txt.xz 
	/tmp/2.txt.xz
	[root@lz-01 d6z]# xz -d -c /tmp/2.txt.xz > ./5.txt
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  3.txt  4.txt  5.txt
	[root@lz-01 d6z]# xzcat /tmp/2.txt.xz  // 查看压缩文件
zip压缩工具

格式: zip filename.zip filename

可以压缩目录; 压缩文件,原文件不消失.

	[root@lz-01 d6z]# zip 2.txt.zip 2.txt 
	  adding: 2.txt (deflated 75%)
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  2.txt.zip  3.txt  4.txt  5.txt  test1
	[root@lz-01 d6z]# du -sh 2.txt.zip
	284K	2.txt.zip
	[root@lz-01 d6z]# unzip
	
	[root@lz-01 d6z]# unzip 2.txt.zip
	Archive:  2.txt.zip
	replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  2.txt.zip  3.txt  4.txt  5.txt  test1
	[root@lz-01 d6z]# unzip 2.txt.zip
	Archive:  2.txt.zip
	replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
	  inflating: 2.txt                   
	[root@lz-01 d6z]# ls
	1.txt.bz2  2.txt  2.txt.zip  3.txt  4.txt  5.txt  test1
	[root@lz-01 d6z]# mkdir test2
	[root@lz-01 d6z]# unzip 2.txt.zip -d test2/
	Archive:  2.txt.zip
	  inflating: test2/2.txt             
	[root@lz-01 d6z]# ls /tmp/d6z/test2/
	2.txt
	[root@lz-01 d6z]# unzip -l 2.txt.zip //查看文件列表, 不能查看文件内容
tar打包工具

转载于:https://my.oschina.net/u/3851442/blog/3029503

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值