压缩七天内文件Linux,Linux文件压缩和打包

6.1、压缩打包介绍

Windows下:rar、zip、7z

Linux下:zip、gz、bz2、xz、tar.gz、tar.bz2、tar.xz

压缩可以节省磁盘空间、节省网络传输带宽资源及传输速率;

6.2、gzip压缩工具

压缩工具:gzip、bzip2、xz、zip、tar(打包和压缩)

gzip工具使用

准备一个大的文件,进入/tmp下,创建d6z目录

find /etc -type f -name “*conf” -exec cat {} >> 1.txt ;查找/etc目录下所有.conf文件查看并追加到当前目录下的1.txt文档内;

文档不够大,多追加几次,查看大小:du -sh 1.txt

1. [root@centos7-01 d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;

2. [root@centos7-01 d6z]# du -sh 1.txt

3. 4.0M 1.txt

压缩1.txt

1. [root@centos7-01 d6z]# du -sh 1.txt

2. 2.0M 1.txt

3. [root@centos7-01 d6z]# wc -l 1.txt

4. 51616 1.txt

5. [root@centos7-01 d6z]# gzip 1.txt

6. [root@centos7-01 d6z]# ls

7. 1.txt.gz

8. [root@centos7-01 d6z]# du -sh 1.txt.gz

9. 528K 1.txt.gz

10. [root@centos7-01 d6z]#

解压缩1.txt.gz

1. [root@centos7-01 d6z]# gzip -d 1.txt.gz

2. [root@centos7-01 d6z]# ls

3. 1.txt

4. [root@centos7-01 d6z]# du -sh 1.txt

5. 2.0M 1.txt

6. [root@centos7-01 d6z]#

压缩前为4M,压缩后为528K,解压缩后为2M,是因为追加之后的文件不是真实的空间,压缩、解压缩之后为真实大小;

gzip -1(1-9为压缩级别,默认为6,1级别为最不严谨的压缩,9为最严谨的压缩耗费CPU的资源最大)

1. [root@centos7-01 d6z]# gzip -9 1.txt

2. [root@centos7-01 d6z]# du -sh 1.txt.gz

3. 528K 1.txt.gz

4. [root@centos7-01 d6z]#

解压gzip加参数-d;

查看压缩文件命令file 1.txt.gz

1. [root@centos7-01 d6z]# gzip -9 1.txt

2. [root@centos7-01 d6z]# du -sh 1.txt.gz

3. 528K 1.txt.gz

4. [root@centos7-01 d6z]#

查看压缩文件内容zcat 1.txt.gz(先解压再查看)

压缩后保存源文件命令:gzip -c 1.txt > /tmp/1.txt.gz

1. [root@centos7-01 d6z]# gzip -d 1.txt.gz

2. [root@centos7-01 d6z]# ls

3. 1.txt

4. [root@centos7-01 d6z]# gzip -c 1.txt > /tmp/1.txt.gz

5. [root@centos7-01 d6z]# ls

6. 1.txt

7. [root@centos7-01 d6z]# tree /tmp/

8. /tmp/

9. ├── 111.txt

10. ├── 11.txt

11. ├── 1.txt.gz

12. ├── d6z

13. │ └── 1.txt

14. ├── passwd

15. └── systemd-private-b4b18621de22479e8755e1fe7ccdb34a-vmtoolsd.service-fMHxo9

16. └── tmp

17. └── vmware-root

18. 4 directories, 5 files

19. [root@centos7-01 d6z]#

解压缩之后把原压缩文件保留命令: gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt

1. [root@centos7-01 d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt

2. [root@centos7-01 d6z]# ls

3. 1.txt 2.txt

4. [root@centos7-01 d6z]# tree /tmp/

5. /tmp/

6. ├── 111.txt

7. ├── 11.txt

8. ├── 1.txt.gz

9. ├── d6z

10. │ ├── 1.txt

11. │ └── 2.txt

12. ├── passwd

13. └── systemd-private-b4b18621de22479e8755e1fe7ccdb34a-vmtoolsd.service-fMHxo9

14. └── tmp

15. └── vmware-root

16. 4 directories, 6 files

17. [root@centos7-01 d6z]# du -sh *.txt

18. 2.0M 1.txt

19. 2.0M 2.txt

20. [root@centos7-01 d6z]# wc -l 1.txt 2.txt

21. 51616 1.txt

22. 51616 2.txt

23. 103232 总用量

24. [root@centos7-01 d6z]#

需要注意的是gzip不能压缩目录

gzip -d和gunzip命令相同

6.3、bzip2压缩工具

相比gzip,bzip2压缩度更高,耗费cup资源更多

yum install -y bzip2命令安装bzip2工具

1. [root@centos7-01 d6z]# bzip2 1.txt

2. [root@centos7-01 d6z]# gzip 2.txt

3. [root@centos7-01 d6z]# du -sh 1.txt.bz2

4. 224K 1.txt.bz2

5. [root@centos7-01 d6z]# du -sh 2.txt.gz

6. 528K 2.txt.gz

解压缩bzip2命令:bzip2 -d或者bunzip2

同样支持-c指定目录

1. [root@centos7-01 d6z]# bzip2 -d 1.txt.bz2

2. [root@centos7-01 d6z]# bzip2 1.txt

3. [root@centos7-01 d6z]# bunzip2 1.txt.bz2

4. [root@centos7-01 d6z]# ls

5. 1.txt 2.txt.gz

6. [root@centos7-01 d6z]# bzip2 -c 1.txt > /tmp/1.txt.bz2

7. [root@centos7-01 d6z]# tree /tmp/

8. /tmp/

9. ├── 111.txt

10. ├── 11.txt

11. ├── 1.txt.bz2

12. ├── 1.txt.gz

13. ├── d6z

14. │ ├── 1.txt

15. │ └── 2.txt.gz

16. ├── passwd

17. └── systemd-private-b4b18621de22479e8755e1fe7ccdb34a-vmtoolsd.service-fMHxo9

18. └── tmp

19. └── vmware-root

20. 4 directories, 7 files

21. [root@centos7-01 d6z]# du -sh /tmp/1.txt.bz2

22. 224K /tmp/1.txt.bz2

23. [root@centos7-01 d6z]# bzip2 -d -c /tmp/1.txt.bz2 > 3.txt

24. [root@centos7-01 d6z]# ls

25. 1.txt 2.txt.gz 3.txt

26. [root@centos7-01 d6z]# gunzip 2.txt.gz

27. [root@centos7-01 d6z]# ls

28. 1.txt 2.txt 3.txt

29. [root@centos7-01 d6z]# du -sh *.txt

30. 2.0M 1.txt

31. 2.0M 2.txt

32. 2.0M 3.txt

33. [root@centos7-01 d6z]#

bzip2默认压缩级别为9

file 1.txt.bz2

1. [root@centos7-01 d6z]# bzip2 1.txt

2. [root@centos7-01 d6z]# file 1.txt.bz2

3. 1.txt.bz2: bzip2 compressed data, block size = 900k

4. [root@centos7-01 d6z]# mv 1.txt.bz2 1.txt

5. [root@centos7-01 d6z]# less 1.txt

6. "1.txt" may be a binary file. See it anyway?

7. [root@centos7-01 d6z]# file 1.txt

8. 1.txt: bzip2 compressed data, block size = 900k

9. [root@centos7-01 d6z]# file 2.txt

10. 2.txt: C source, UTF-8 Unicode text, with very long lines

11. [root@centos7-01 d6z]# mv 1.txt 1.txt.bz2

查看bz2压缩文件内容命令:bzcat 1.txt.bz2

6.4、xz压缩工具

和gzip、bzip2用法基本相同,不常用,主要用于打包压缩

1. [root@centos7-01 d6z]# xz 1.txt

2. [root@centos7-01 d6z]# ls

3. 1.txt.xz 2.txt 3.txt

4. [root@centos7-01 d6z]# du -sh 1.txt.xz

5. 60K 1.txt.xz

6. [root@centos7-01 d6z]#

压缩度更高,同样有9个压缩级别

1. [root@centos7-01 d6z]# ls

2. 1.txt 2.txt 3.txt 4.txt

3. [root@centos7-01 d6z]# xz 1.txt

4. [root@centos7-01 d6z]# xz 2.txt

5. [root@centos7-01 d6z]# unxz 1.txt.xz

6. [root@centos7-01 d6z]# xz -d 2.txt.xz

7. [root@centos7-01 d6z]# ls

8. 1.txt 2.txt 3.txt 4.txt

9. [root@centos7-01 d6z]# xz -c 1.txt > /tmp/1.txt.xz

10. [root@centos7-01 d6z]# ls /tmp/

11. 111.txt 11.txt 1.txt.bz2 1.txt.gz 1.txt.xz 2.txt.xz d6z passwd systemd-private-b4b18621de22479e8755e1fe7ccdb34a-vmtoolsd.service-fMHxo9

12. [root@centos7-01 d6z]# du -sh *txt

13. 2.0M 1.txt

14. 2.0M 2.txt

15. 2.0M 3.txt

16. 2.0M 4.txt

17. 2.0M 6.txt

查看要是内容:xzcat 1.txt.xz

xz同样不支持压缩目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值