您的位置:首页 > 运维架构 > Linux

Linux 压缩归档总结

2015-09-23 11:57 791 查看
linux压缩有文件, 目录压缩和归档

文件压缩种类:gzip, bzip2, xz
目录压缩:zip
归档:tar

各种文件压缩的命令都是实现下面的几个功能, 只是语法略有不同
gzip:
文件后缀: gz
压缩: gzip 文件, 删除原文件
[root@centos test]# ll
total 192
-rw-------. 1 root root 193746 Sep 23 12:05 test01
[root@centos test]# gzip test01
[root@centos test]# ll
total 32
-rw-------. 1 root root 29378 Sep 23 12:05 test01.gz
解压: gunzip 压缩文件, 删除压缩文件
[root@centos test]# gunzip test01.gz
[root@centos test]# ll
total 192
-rw-------. 1 root root 193746 Sep 23 12:05 test01
保留原文件压缩: gzip -c 文件 > 压缩后文件
[root@centos test]# gzip -c test01 > test02.gz
[root@centos test]# ll
total 224
-rw-------. 1 root root 193746 Sep 23 12:05 test01
-rw-r--r--. 1 root root  29378 Sep 23 13:24 test02.gz
在压缩包内直接查看文件 zcat 压缩文件
[root@centos test]# zcat test02.gz | less

bzip2文件后缀: bz2压缩: bzip2 文件, 会删除原文件解压: bunzip2 压缩文件, 会删除压缩文件保留原文件压缩: bzip2 -k 原文件
[root@centos test]# bzip2 -k test02
[root@centos test]# ll
total 212
-rw-r--r--. 1 root root 193746 Sep 23 13:24 test02
-rw-r--r--. 1 root root  19565 Sep 23 13:24 test02.bz2
在压缩包内直接查看文件: bzcat 压缩文件

xz文件后缀: xz压缩: xz 文件, 会删除原文件解压: unxz 压缩文件, 会删除压缩文件保留原文件压缩: xz -k 源文件在压缩包内直接查看文件: xzcat 压缩文件

这几个命令的压缩比分别是
xz>bzip2>gzip

目录压缩zip
特殊用法: 1. 需要自己制定压缩后文件, 2压缩后原文件不会被删除 3 可以单独压缩文件
格式: zip 压缩后文件.zip 原文件
[root@centos test]# zip test02.zip test02
adding: test02 (deflated 85%)
[root@centos test]# ll
total 244
-rw-r--r--. 1 root root 193746 Sep 23 13:24 test02
-rw-r--r--. 1 root root  19565 Sep 23 13:24 test02.bz2
-rw-r--r--. 1 root root  29515 Sep 23 13:32 test02.zip
压缩目录, 需要指定所有文件
[root@centos tmp]# zip test.zip test/*
adding: test/test02 (deflated 85%)
adding: test/test02.bz2 (deflated 3%)
adding: test/test02.zip (stored 0%)
解压缩: unzip 压缩文档.zip, 只能解压到当前目录
[root@centos test02]# unzip test.zip
Archive:  test.zip
inflating: test/test02
inflating: test/test02.bz2
extracting: test/test02.zip
[root@centos test02]# ll test/
total 244
-rw-r--r--. 1 root root 193746 Sep 23 13:24 test02
-rw-r--r--. 1 root root  19565 Sep 23 13:24 test02.bz2
-rw-r--r--. 1 root root  29515 Sep 23 13:32 test02.zip


归档
创建归档
格式: tar cf 归档文件.tar 要归档的所有文件夹或文件
不用像zip,指定目录内所有文件
[root@centos test02]# tar -cf testtar.tar test/ dir/


展开归档, 到当前目录, 不会删除压缩文件
tar xf tar文件
[root@centos test02]# tar -xf testtar.tar


查看归档内文件
tar -tf tar文件
[root@centos test02]# tar -tf testtar.tar
test/
test/test02.zip
test/test02
test/test02.bz2
dir/


归档后压缩调用
压缩并归档
格式:tar -zcf 文件.tar.gz 文件
-z可替换其他压缩格式, 后缀也要有相应修改
-z: gzip, -j: bzip2, -J: xz
[root@centos test02]# tar -zcf test.tar.gz test/
[root@centos test02]# ll test.tar.gz
-rw-r--r--. 1 root root 78836 Sep 23 13:54 test.tar.gz
解压归档并压缩过的文件, 不用关心是那种格式压缩的
t格式 ar xf 压缩并归档后的文件
[root@centos test02]# tar -xf test.tar.gz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: