Linux centosVMware 压缩打包介绍、gzip压缩工具、bzip2压缩工具、xz压缩工具。

一、压缩打包介绍

Lnux下常见的压缩文件通常是.tar.gz模式,还有.tar.gz.bz2.zip、.tar.bz2、.tar.xz

.gz:表示由gzip压缩工具压缩的文件

.bz2:表示由gzip2压缩工具压缩的文件

.tar:表示由tar打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件)

.tar.gz:表示先由tar打包,然后再由gzip压缩

.tar.bz2:表示由tar打包,再由bzip2压缩

.tar.xz:表示由tar先打包,后再xz压缩

二、gzip压缩工具,不能压缩目录

gzip格式:gzip [-d#] filename,其中#为1-9数字

-d:解压时使用

-#:表示压缩等级,1为最差,9为最好,6为默认

gzip后边直接跟文件名,表示在当前目录下压缩该文件,而原文件也会消失。

[root@davery ~]# cd /tmp/

[root@davery tmp]# ls
0.txt

[root@davery tmp]# mkdir yasuo
[root@davery tmp]# ls yasuo

[root@davery tmp]# ls
0.txt

[root@davery tmp]# du -sh 0.txt
212K 0.txt
[root@davery tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 0.txt \;
[root@davery tmp]# du -sh 0.txt
448K 0.txt
[root@davery tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 0.txt \;
[root@davery tmp]# du -sh 0.txt
704K 0.txt
[root@davery tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 0.txt \;
[root@davery tmp]# du -sh 0.txt
1.2M 0.txt
[root@davery tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 0.txt \;
[root@davery tmp]# du -sh 0.txt
1.2M 0.txt
[root@davery tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 0.txt \;
[root@davery tmp]# du -sh 0.txt
2.2M 0.txt
[root@davery tmp]# wc -l 0.txt
32388 0.txt
[root@davery tmp]# gzip 0.txt 压缩
[root@davery tmp]# ls
0.txt.gz

[root@davery tmp]# du -sh 0.txt.gz
320K 0.txt.gz
[root@davery tmp]# gzip -d 0.txt.gz        解压
[root@davery tmp]# ls
0.txt

[root@davery tmp]# gzip -1 0.txt       压缩为最低级别
[root@davery tmp]# du -sh 0.txt.gz   查看大小

376K 0.txt.gz

[root@davery tmp]# gzip -d 0.txt.gz
[root@davery tmp]# ls
0.txt

[root@davery tmp]# gzip -9 0.txt   压缩为最高级别
[root@davery tmp]# du -sh 0.txt.gz 查看大小
320K 0.txt.gz
[root@davery tmp]#

[root@davery tmp]# zcat 0.txt.gz 查看压缩内容

扩展

[root@davery tmp]# gzip -d 0.txt.gz
[root@davery tmp]# gzip -c 0.txt > /tmp/0.txt.gz    解压后不会消失
[root@davery tmp]# ls
0.txt.gz

[root@davery tmp]# ls /tmp/0.txt.gz
/tmp/0.txt.gz
[root@davery tmp]# file !$
file /tmp/0.txt.gz
/tmp/0.txt.gz: gzip compressed data, was "0.txt", from Unix, last modified: Tue Apr 3 19:53:32 2018
[root@davery tmp]#

[root@davery tmp]# gzip -d -c /tmp/0.txt.gz > /tmp/yasuo/1.txt
[root@davery tmp]# ls
0.txt
1.txt

[root@davery tmp]# wc -l 0.txt 1.txt
32388 0.txt
20 1.txt
32408 总用量
[root@davery tmp]#

root@davery tmp]# du -sh *.txt
1.3M 0.txt
4.0K 1.txt
[root@davery tmp]#

三、bzip2压缩工具

格式:bzip2 [-dz] filename ,

-z:压缩

-d:解压缩

[root@davery tmp]# yum install -y bzip2   安装

[root@davery tmp]# bzip2 0.txt
[root@davery tmp]# ls
0.txt.bz2

[root@davery tmp]# du -sh 0.txt.bz2
132K 0.txt.bz2

[root@davery tmp]# bzip2 -d 0.txt.bz2

[root@davery tmp]# bzip2 -c 0.txt > /tmp/0.txt.bz2

[root@davery tmp]# du -sh /tmp/0.txt.bz2
132K /tmp/0.txt.bz2

[root@davery tmp]# 

[root@davery tmp]# bzip2 -d -c 0.txt.bz2 > 2.txt

[root@davery tmp]# du -sh 2.txt
1.3M 2.txt

[root@davery tmp]# file 0.txt 2.txt     看文件类型
0.txt: C source, UTF-8 Unicode text, with very long lines
2.txt: C source, UTF-8 Unicode text, with very long lines

四、xz压缩工具

格式:xz [-dz] filename 

-z 压缩

-d解压

[root@davery ~]# cd /tmp
[root@davery tmp]#
[root@davery tmp]# xz 0.txt
[root@davery tmp]# du -sh 0.txt.xz
48K 0.txt.xz

[root@davery tmp]# xz -d -c 0.txt.xz > 3.txt.xz
[root@davery tmp]# du -sh 3.txt.xz
1.3M 3.txt.xz
[root@davery tmp]#

原文地址:https://www.cnblogs.com/davery/p/8710854.html