1.常见压缩文件
- windows系统中,常见的压缩文件有 .rar, .zip, .7z
- linux系统中,常见的压缩文件有 .zip, .gz, .bz2, .xz, .tar.gz, .tar.bz2, .tar.xz
压缩后优势:
- 压缩的文件会变小很多
- 压缩后的文件带宽资源耗费也变小了
- 压缩后的文件会减少传输时间
2. gzip压缩工具
压缩和解压
- 压缩:在多个文件一起压缩的时候,只能压缩第一个文件
- 解压:多个文件可以在一起直接,比如bzip2 -d 1.txt.bz2 2.txt.bz2 这样会将两个文件都解压
gzip目录概要
- gzip 1.txt //压缩1.txt文件
- gzip -d 1.txt 或者 uzip 1.txt.gz 解压1.txt文件的两种方法
- gzip -# 1.txt //#表示范围1-9,默认为6
- 不能压缩目录
- zcat 1.txt.gz
- gzip -c 1.txt > /root/1.txt.gz 压缩文件,并指定目录
- gunzip -c /root/1.txt.gz > /tmp/1.txt.new 解压文件,并指定
gzip压缩文件 gzip不能压缩目录
- gzip 1.txt 压缩1.txt文件
- 首先切换到/tmp目录下,并新建一个目录d6z
[root@localhost ~]# cd /tmp/[root@localhost tmp]# ls11.sh han.11123.sh fstab[root@localhost tmp]# mkdir d6z
- 切换到/d6z目录下,并查找/etc目录下所有以conf结尾的文件
[root@localhost tmp]# cd d6z[root@localhost d6z]# find /etc/ -type f -name "*conf" 查找所有文件中,名字以conf结尾的文件/etc/resolv.conf/etc/pki/ca-trust/ca-legacy.conf/etc/yum/pluginconf.d/fastestmirror.conf/etc/yum/pluginconf.d/langpacks.conf/etc/yum/protected.d/systemd.conf/etc/yum/version-groups.conf/etc/rdma/mlx4.conf/etc/rdma/rdma.conf
- 然后这文件查找出的文件内容追加到1.txt中,这个符号 {} 表示列出来的文件
- 多追加几次文件内容到1.txt中
[root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;[root@localhost d6z]#
- 查看文件和大小
[root@localhost d6z]# ls1.txt[root@localhost d6z]# du -sh 1.txt3.2M 1.txt
这里多次追加会看到文件,du -sh 1.txt查看的文件数值不同,但在多次查看,文件大小会恢复正常。(跳转数值较大比,是因为这个文件本身存在很多空隙,最后在压缩并解压后,会发现大小会有不同)
- 压缩文件1.txt
[root@localhost d6z]# gzip 1.txt[root@localhost d6z]# ls1.txt.gz
会看到源文件消失了,变成了.gz的压缩文件 1.查看压缩文件大小[root@localhost d6z]# du -sh 1.txt.gz332K 1.txt.gz
gzip解压文件
- gzip -d 1.txt.gz 把1.txt文件解压出来
[root[@localhost](https://my.oschina.net/u/570656) d6z]# gzip -d 1.txt.gz[root[@localhost](https://my.oschina.net/u/570656) d6z]# ls1.txt[root[@localhost](https://my.oschina.net/u/570656) d6z]# du -sh 1.txt1.3M 1.txt
- gunzip 1.txt.gz 解压1.txt文件
[root@localhost d6z]# gunzip 1.txt.gz[root@localhost d6z]# ls1.txt[root@localhost d6z]# du -sh 1.txt1.3M 1.txt
gzip指定压缩的级别
. gzip -n(数字) 文件名 //指定压缩级别
- gzip压缩的级别范围有1-9,默认是 6 级别件也是最小的)压缩到一定级别后,到达极限后,会压缩不了
file命令,查看压缩后的文件
- 压缩后的文件变成了二进制文件,不能直接使用cat查看
- file命令,查看压缩的文件
- 格式 file 1.txt.gz
[root@localhost d6z]# file 1.txt.gz1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov 9 14:23:33 2017, max compression
这里会看到这是一个gzip的压缩数据,名称是1.txt,基于Unix平台,最后一次的更改时间,压缩的级别
zcat命令,查看压缩文件的内容
- zcat命令,查看压缩文件的内容
- 格式 zcat 1.txt.gz
- 这是先解压,后cat查看的
- 格式 zcat 1.txt.gz
gzip压缩文件,并指定目录
- gzip -c 1.txt > /tmp/1.txt.gz 压缩文件,并重定向目录和名称;原文件不会消失,在-C指定目录下也会有新的压缩文件生成。
[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz[root@localhost d6z]# ls /tmp/1.txt.gz/tmp/1.txt.gz
gzip解压文件,并指定目录
原压缩文件不会消失,在指定的解压位置会有解压文件生成
- gunzip -c /tmp/1.txt.gz > /tmp/6dz/2.txt
- gzip -d -c /tmp/1.txt.gz > /tmp/6dz/2.txt
[root@localhost d6z]# gzip -c -d /tmp/1.txt.gz >/tmp/d6z/2.txt[root@localhost d6z]# ls1.txt 2.txt
1.txt和2.txt这两个文件大小相同(du -sh 1.txt 2.txt),行数形同(wc -l 1.txt 2.txt)
1. bzip2压缩工具 同样不支持压缩目录
bzip2目录概要
- bzip2 1.txt 或 bzip2 -z 1.txt 压缩文件
- bzip2 -d 1.txt.bz2 或 bunzip2 1.txt.bz2 解压文件
- bzip -# 1.txt 范围1-9,默认9
- 不能压缩目录
- bzcat 1.txt.bz2 查看压缩文件的内容(含义为:先解压,后查看)
- bzip2 -c 1.txt > /root/1.txt.gz 压缩文件,并重定向文件的目录和名称
- bzip2 -c -d /root/1.txt.bz2 > /tmp/1.txt.nws 解压文件,并重定向文件的目录和名称
bzip2压缩文件
- 安装bzip2包——>yum install -y bzip2
- bzip2 1.txt 压缩文件
[root@hf-01 d6z]# du -sh 1.txt2.0M 1.txt[root@hf-01 d6z]# bzip2 1.txt[root@hf-01 d6z]# ls1.txt.bz2[root@hf-01 d6z]# du -sh 1.txt.bz2168K 1.txt.bz2
bzip2解压文件(两种办法)
- bzip2 -d 1.txt 解压文件
[root@hf-01 d6z]# bzip2 -d 1.txt.bz2[root@hf-01 d6z]# ls1.txt[root@hf-01 d6z]# du -sh 1.txt1.5M 1.txt
- bunzip2 1.txt.bz2 解压文件
[root@hf-01 d6z]# bunzip2 1.txt.bz2[root@hf-01 d6z]# ls1.txt
bzip2压缩文件,并指定目录 -C
- bzip2 -c 1.txt > /tmp/2.txt.bz2 压缩1.txt文件,并压缩到/tmp下2.txt.bz2
[root@hf-01 d6z]# bzip2 -c 1.txt > /tmp/2.txt.bz2[root@hf-01 d6z]# du -sh /tmp/2.txt.bz2168K /tmp/2.txt.bz2
bzip解压文件,并指定目录(两种方法)
- bzip2 -d -c /tmp/2.txt.bz2 > 4.txt 解压tmp目录下的文件,并解压到当前目录下,改名为4.txt
[root@hf-01 d6z]# bzip2 -d -c /tmp/2.txt.bz2 > 4.txt[root@hf-01 d6z]# ls1.txt 4.txt[root@hf-01 d6z]# du -sh 4.txt1.5M 4.txt
- bunzip2 1.txt.bz2 > 5.txt 解压文件,并解压到当前目录下,名称为5.txt
[root@hf-01 d6z]# bunzip2 1.txt.bz2 > 5.txt[root@hf-01 d6z]# ls1.txt 4.txt 5.txt
bzip2压缩级别
- bzip2默认压缩级别为 9
file查看压缩文件大小
- file命令,会知道压缩文件的类型,大小
[root@hf-01 d6z]# file 1.txt.bz21.txt.bz2: bzip2 compressed data, block size = 900k
- 若是文件名的格式被改错,或者不知道
- 在使用less、cat、head查看该文件的时候,会提示“该文件是二进制文件,是否继续查看呢”
- 这时候可以使用 file命令 去查看,会知道该文件的格式
bzcat查看压缩文件内容
- bzcat命令,查看压缩文件中的内容
- 格式 bzcat 1.txt.bz2
[root@hf-01 d6z]# bzcat 1.txt.bz2
查看的时候,需注意压缩文件是否为空,若为空,则看不到什么文件
xz压缩工具
- xz 1.txt 或 xz -z 1.txt 压缩文件
- xz -d 1.txt.xz 或 unxz 1.txt.xz 解压文件
- xz -# 1.txt 1.txt //#表示范围1-9,默认为 6
- 不能压缩目录
- xzcat 1.txt.xz 查看压缩文件的内容
- xz -c 1.txt > /root/1.txt.xz 压缩文件,压缩到指定目录下,并修改名称
- xz -d -c /root/1.txt.xz > 1.txt.news 解压文件,解压到指定目录下,并修改名称
xz压缩文件
[root@hf-01 d6z]# xz 1.txt[root@hf-01 d6z]# ls1.txt.xz 4.txt 5.txt[root@hf-01 d6z]# du -sh 1.txt.xz60K 1.txt.xz
xz解压文件(两种方法)
- xz -d 1.txt.xz 解压文件
[root@hf-01 d6z]# xz -d 1.txt.xz[root@hf-01 d6z]# ls1.txt 4.txt 5.txt[root@hf-01 d6z]# du -sh 1.txt1.5M 1.txt
- unxz 1.txt.xz 解压文件
[root@hf-01 d6z]# unxz 1.txt.xz[root@hf-01 d6z]# ls1.txt 4.txt 5.txt
xz压缩文件,并压缩到指定目录
- xz -c 1.txt > /tmp/ha.txt.xz
[root@hf-01 d6z]# xz -c 1.txt > /tmp/ha.txt.xz[root@hf-01 d6z]# du -sh /tmp/ha.txt.xz60K /tmp/ha.txt.xz
xz解压文件,并解压到指定目录
- xz -d -c /tmp/ha.txt.xz > ./8.txt
- unxz -c /tmp/ha.txt.xz > ./8.txt
[root@hf-01 d6z]# unxz -c /tmp/ha.txt.xz > ./8.txt[root@hf-01 d6z]# ls1.txt 4.txt 5.txt 8.txt[root@hf-01 d6z]# du -sh 8.txt1.5M 8.txt
xzcat查看压缩文件
- xzcat /tmp/ha.txt.xz
xz压缩级别
- xz压缩级别是1-9,默认是 6 级别
压缩工具排序
- xz >bzip2 >gzip(这不是绝对的,关键还得看文件)
- xz 压缩最为严谨,bzip2稍次之,gzip排在最后