常见的压缩文件
Windows:.rar , .zip , 7z
linux: .zip , gz , bz2 , .xz , .tar.gz , .tar.bz2 , .tar.xz
当然,Linux下文件名字是可以自己定义的,但对后缀的使用也还是要规范,通常情况下,我们通过后缀名来识别文件。
linux下的压缩工具
linux的压缩工具:gzip , bzip2 ,xz , zip , tar
大文件压缩,下载时候可以少占带宽。
先创建一个大文件用来打包用。
[root@shuai-01 d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
多执行几次,得到文件。
[root@shuai-01 d6z]# du -sh /tmp/d6z/1.txt
1.3M /tmp/d6z/1.txt
gzip压缩工具(只能压缩文件)
命令:gzip
命令格式:gzip [选项] filename
选项:
-d 解压缩时候用
-# #代表数字1~9,表示压缩级别,1为最小,9为最高,6为默认。
-c 压缩时重新产生一个新文件
压缩:[root@shuai-01 d6z]# ls 1.txt [root@shuai-01 d6z]# gzip 1.txt [root@shuai-01 d6z]# ls 1.txt.gz
解压缩:
[root@shuai-01 d6z]# ls 1.txt.gz [root@shuai-01 d6z]# gzip -d 1.txt.gz [root@shuai-01 d6z]# ls 1.txt
指定等级的压缩:(一般都不用)
[root@shuai-01 d6z]# ls 1.txt [root@shuai-01 d6z]# gzip -9 1.txt [root@shuai-01 d6z]# ls 1.txt.gz [root@shuai-01 d6z]# du -sh 1.txt.gz 332K 1.txt.gz
指定压缩后生成的新文件:(生成压缩文件,原文件没变)
[root@shuai-01 d6z]# ls 1.txt [root@shuai-01 d6z]# gzip -c 1.txt > /tmp/1.txt.gz [root@shuai-01 d6z]# ls 1.txt [root@shuai-01 d6z]# ls /tmp/1.txt.gz /tmp/1.txt.gz
指定解压缩生成新文件:(生成了新文件,压缩文件没有删除)
[root@shuai-01 d6z]# ls /tmp/1.txt.gz /tmp/1.txt.gz [root@shuai-01 d6z]# gzip -d -c /tmp/1.txt.gz > 2.txt [root@shuai-01 d6z]# ls 1.txt 2.txt [root@shuai-01 d6z]# ls /tmp/1.txt.gz /tmp/1.txt.gz
解压缩命令:
[root@shuai-01 d6z]# gunzip /tmp/1.txt.gz
bzip2压缩工具(只能压缩文件)
使用bzip2 压缩,能压缩的更小。[root@shuai-01 d6z]# bzip2 1.txt -bash: bzip2: 未找到命令
这个时候还没安装bzip2 ,安装
[root@shuai-01 d6z]# yum install -y bzip2
bzip2根gzip很像。
命令:bzip2
命令格式:bzip2 [选项] filename
选项:
-d 解压缩
-# #代表数字1~9,表示压缩级别,1为最小,9为最高,9为默认
-c 生成压缩文件,原文件没变
查看文件信息:file filename[root@shuai-01 d6z]# file 1.txt.bz2 1.txt.bz2: bzip2 compressed data, block size = 900k
压缩:
[root@shuai-01 d6z]# ls
1.txt 2.txt
[root@shuai-01 d6z]# bzip2 1.txt
[root@shuai-01 d6z]# ls
1.txt.bz2 2.txt
解压缩:
[root@shuai-01 d6z]# ls
1.txt.bz2 2.txt
[root@shuai-01 d6z]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
[root@shuai-01 d6z]# bzip2 -d 1.txt.bz2
[root@shuai-01 d6z]# ls
1.txt 2.txt
指定压缩在哪:(生成压缩文件,原文件没变)
[root@shuai-01 d6z]# bzip2 -c 2.txt > /tmp/2.txt.bz2
[root@shuai-01 d6z]# ls
1.txt 2.txt
指定解压缩:
[root@shuai-01 d6z]# bzip2 -d -c /tmp/2.txt.bz2 > 3.txt
[root@shuai-01 d6z]# ls
1.txt 2.txt 3.txt
解压缩:
[root@shuai-01 d6z]# ls
1.txt.bz2 2.txt 3.txt
[root@shuai-01 d6z]# bunzip2 1.txt.bz2
[root@shuai-01 d6z]# ls
1.txt 2.txt 3.txt
3.xz压缩工具
和gzip,bzip2用法差不多。xz压缩能力最强,bzip2第二,gzip最次
命令:xz
命令格式:xz [选项] filename
选项:
-d 解压缩
-c 保留原文件的压缩和解压缩
压缩:
[root@shuai-01 d6z]# xz 2.txt
[root@shuai-01 d6z]# ls
1.txt 2.txt.xz 3.txt
解压缩:
[root@shuai-01 d6z]# xz -d 2.txt.xz
[root@shuai-01 d6z]# ls
1.txt 2.txt 3.txt
保留原文件的压缩:
[root@shuai-01 d6z]# xz -c 1.txt > /tmp/1.txt.xz
[root@shuai-01 d6z]# ls
1.txt 2.txt 3.txt
[root@shuai-01 d6z]# ls /tmp/1.txt.xz
/tmp/1.txt.xz
保留压缩文件的解压缩:
[root@shuai-01 d6z]# xz -d -c /tmp/1.txt.xz > 4.txt
[root@shuai-01 d6z]# ls
1.txt 2.txt 3.txt 4.txt
[root@shuai-01 d6z]# ls /tmp/1.txt.xz
/tmp/1.txt.xz
查看压缩文件:
[root@shuai-01 d6z]# xzcat 2.txt.xz
解压缩:
[root@shuai-01 d6z]# ls
1.txt.xz 2.txt 3.txt 4.txt
[root@shuai-01 d6z]# unxz 1.txt.xz
[root@shuai-01 d6z]# ls
1.txt 2.txt 3.txt 4.txt
4. zip压缩工具(支持目录压缩)
安装zip工具
yum install -y zip
zip 能支持压缩目录,并且压缩时原文件还在
命令:zip
命令格式:zip [选项] filename.zip filename
-r 压缩目录
压缩:
[root@shuai-01 d6z]# zip 1.txt.zip 1.txt
adding: 1.txt (deflated 74%)
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai
压缩目录:
[root@shuai-01 d6z]# zip -r shuai.zip 3.txt shuai
adding: 3.txt (deflated 74%)
adding: shuai/ (stored 0%)
adding: shuai/4.txt (deflated 74%)
解压缩:
命令:unzip
命令格式:unzip [选项] filename
选项:
-d 指定目录解压缩
-l 查看压缩的文件名
装unzip:
[root@shuai-01 d6z]# yum install -y unzip
解压缩:
[root@shuai-01 d6z]# unzip 1.txt.zip
Archive: 1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
解压到指定目录:
[root@shuai-01 d6z]# unzip 1.txt.zip -d test/
Archive: 1.txt.zip
inflating: test/1.txt
查看压缩的文件名:
[root@shuai-01 d6z]# unzip -l shuai.zip
Archive: shuai.zip
Length Date Time Name
--------- ---------- ----- ----
1286050 11-09-2017 22:20 3.txt
0 11-10-2017 18:54 shuai/
1286050 11-09-2017 22:32 shuai/4.txt
--------- -------
2572100 3 files
4. tar打包工具
tar是一个打包工具,可以把目录打包成一个文件。
命令:tar
命令格式:tar [选项] filename.tar filename
选项:
-z 用gzip压缩
-j 用bzip2压缩
-J 用xz压缩
-c 表示建立一个tar的包或压缩包
-v 可视化
-f 压缩后的文件名
-x 表示解包或解压缩
-t 查看tar包里的文件名
–exclude filename 打包时将filename排除在外
打包:
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai shuai.zip test
[root@shuai-01 d6z]# tar -cvf shuai.tar shuai/
shuai/
shuai/4.txt
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai shuai.tar shuai.zip test
解包:
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai shuai.tar shuai.zip test
[root@shuai-01 d6z]# tar -xvf shuai.tar
shuai/
shuai/4.txt
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai shuai.tar shuai.zip test
多个文件目录一起打包:
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai shuai.tar shuai.zip test
[root@shuai-01 d6z]# tar -cvf shuai.tar shuai/ test/ 1.txt
shuai/
shuai/4.txt
test/
test/1.txt
1.txt
[root@shuai-01 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt shuai shuai.tar shuai.zip test
查看tar包里的文件:
[root@shuai-01 d6z]# tar -tf shuai.tar
shuai/
shuai/4.txt
test/
test/1.txt
1.txt
打包时过滤掉一些文件或目录
打包并用gzip压缩:
[root@shuai-01 d6z]# tar -czvf shuai.tar.gz shuai 1.txt
shuai/
shuai/4.txt
1.txt
[root@shuai-01 d6z]# du -sh shuai.tar.gz
660K shuai.tar.gz
解包并解压缩gzip:
[root@shuai-01 d6z]# tar -xzvf shuai.tar.gz
shuai/
shuai/4.txt
1.txt
打包并用bzip2压缩:
[root@shuai-01 d6z]# tar -cjvf shuai.tar.bz2 shuai 1.txt
shuai/
shuai/4.txt
1.txt
[root@shuai-01 d6z]# du -sh shuai.tar.bz2
260K shuai.tar.bz2
解包并解压缩bzip2:
[root@shuai-01 d6z]# tar -xjvf shuai.tar.bz2
shuai/
shuai/4.txt
1.txt
打包并用xz压缩:
[root@shuai-01 d6z]# tar -cJvf shuai.tar.xz shuai 1.txt
shuai/
shuai/4.txt
1.txt
[root@shuai-01 d6z]# du -sh shuai.tar.xz
60K shuai.tar.xz
解包并解压缩xz:
[root@shuai-01 d6z]# tar -xJvf shuai.tar.xz
shuai/
shuai/4.txt
1.txt
查看压缩包内文件名:
[root@shuai-01 d6z]# tar -tf shuai.tar.gz
shuai/
shuai/4.txt
1.txt