Linux 压缩,解压缩,打包与解包

.gz gzip 压缩工具压缩的文件

.bz2 bzip2 压缩工具压缩的文件

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

.tar.gz 可以理解为先用tar打包,然后再gzip压缩

.tar.bz2 同上,先用tar打包,然后再bzip2压缩

gzip压缩工具 gzip 是不支持压缩目录的
语法: gzip [-d#] filename 其中#为1-9的数字
“-d” : 解压缩时使用
“-#” : 压缩等级,1压缩最差,9压缩最好,6为默认
至于 “-#” 选项,平时很少用,使用默认压缩级别足够了。
压缩
[root@localhost test]# ls
test.txt
[root@localhost test]# gzip test.txt
[root@localhost test]# ls
test.txt.gz
解压缩
[root@localhost test]# gzip -d test.txt.gz
[root@localhost test]# ls
test.txt

bzip2压缩工具 bzip2 同样也不可以压缩目录:
语法: bzip2 [-dz] filename
bzip2 只有两个选项需要您掌握。
“-d” : 解压缩
“-z” : 压缩
压缩时,可以加 “-z” 也可以不加,都可以压缩文件,”-d” 则为解压的选项:

[root@localhost test]# bzip2 test.txt
[root@localhost test]# ls
test.txt.bz2
[root@localhost test]# bzip2 -d test.txt.bz2
[root@localhost test]# bzip2 -z test.txt
[root@localhost test]# ls
test.txt.bz2

tar压缩工具
tar 本身为一个打包工具,可以把目录打包成一个文件,它的好处是它把所有文件整合成一个大文件整体,方便拷贝或者移动。
语法:tar [-zjxcvfpP] filename tar 命令有多个选项,
“-z” : 同时用gzip压缩
“-j” : 同时用bzip2压缩
“-x” : 解包或者解压缩
“-t” : 查看tar包里面的文件
“-c” : 建立一个tar包或者压缩文件包
“-v” : 可视化
“-f” : 后面跟文件名,压缩时跟 “-f 文件名”,意思是压缩后的文件名为filename, 解压时跟 “-f 文件名”,意思是解压filename. 请注意,如果是多个参数组合的情况下带有 “-f”,请把 “-f” 写到最后面。
“-p” : 使用原文件的属性,压缩前什么属性压缩后还什么属性。(不常用)
“-P” : 可以使用绝对路径。(不常用)

[root@localhost test]# tar -cvf test.tar test
test/
test/test2.txt
[root@localhost test]# ls
test test.tar test.txt.bz2
tar 打包后,原文件不会消失,而依旧存在
不管是打包还是解包,原来的文件是不会删除的,而且它会覆盖当前已经存在的文件或者目录。
[root@GeFY001 gefy]# ll
总用量 16
drwxr-xr-x. 2 root root 4096 4月 29 02:17 dir1
-rw-r–r–. 1 root root 10240 4月 29 02:18 text1.tar
[root@GeFY001 gefy]# rm -rf dir1/
[root@GeFY001 gefy]# tar -xvf text1.tar
dir1/
dir1/text.text
[root@GeFY001 gefy]# ll
总用量 16
drwxr-xr-x. 2 root root 4096 4月 29 02:17 dir1
-rw-r–r–. 1 root root 10240 4月 29 02:18 text1.tar
[root@GeFY001 gefy]#

打包的同时使用gzip压缩
[root@localhost test]# tar -czvf test111.tar.gz test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111 test111.tar.gz test.tar test.txt.bz2

“-tf” 可以查看包或者压缩包的文件列表:
[root@localhost test]# tar -tf test111.tar.gz
test111/
test111/test2.txt
[root@localhost test]# tar -tf test.tar
test111/
test111/test2.txt
test.txt.bz2

“-zxvf” 用来解压.tar.gz的压缩包
[root@localhost test]# rm -rf test111
[root@localhost test]# ls
test111.tar.gz test.tar test.txt.bz2
[root@localhost test]# tar -zxvf test111.tar.gz
test111/
test111/test2.txt
[root@localhost test]# ls
test111 test111.tar.gz test.tar test.txt.bz2

打包的同时使用bzip2压缩
和gzip压缩不同的是,这里使用 “-cjvf” 选项来压缩
[root@localhost test]# tar -cjvf test111.tar.bz2 test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111 test111.tar.bz2 test111.tar.gz test.tar test.txt.bz2

同样可以使用 “-tf” 来查看压缩包文件列表
[root@localhost test]# tar -tf test111.tar.bz2
test111/
test111/test2.txt

解压.tar.bz2 的压缩包也很简单
[root@localhost test]# tar -jxvf test111.tar.bz2
test111/
test111/test2.txt

–exclude 这个选项的使用,因为在日常的管理工作中您也许会用到它。
[root@localhost test]# tar -cvf test111.tar –exclude test3.txt test111
test111/
test111/test4.txt
test111/test2.txt
test111/test5/
是将 test3.txt 从打包中排除掉,

–exclude 选项的前面。除了可以排除文件,也可以排除目录:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值