6 Linux文件压缩和打包

补充重点:
1.gzip 压缩文件:gzip 1.txt # 压缩后文件1.txt就消失
2.gzip -#n 显示压缩等级:gzip -# 9 1.txt #默认是6
3.gzip -d 解
压缩文件:gzip -d 1.txt.gz
4.tar -c 建立tar包:tar -c 
5.tar -x 解开tar包:tar -x
6.tar -t 查看tar包里的文件:tar -t
7.tar -z 同时用gzip压缩
8.tar -v 可视化
9.tar -f 后面跟文件名
10.tar -czvf  可视化文件建立tar并gzip压缩 ,原文件会保留,tar -czvf all.txt.tar.gz 1.txt 2.txt 
11.tar -xzvf 可视化文件建立tar并gzip解压缩 ,原文件会保留 tar -xzvf all.txt.tar.gz 1.txt 2.txt
12.tar -tf
查看.tar.gz文件列表
13.zcat查看.gz文件内容

6.1 压缩打包介绍

常见的压缩文件类型:

Windows :     .rar      .zip      .7z

Linux : .gz    .bz2   .zip   .xz    .tar.gz    .tar.bz2   .tar.xz

6.2 gzip压缩工具

压缩级别:1-9级别(压缩级别越大,占用的cup资源越大!)默认压缩级别:6级别

gzip压缩工具:只能压缩文件,不支持压缩目录!!!

[root@localhost zip-test]# gzip 222.txt
[root@localhost zip-test]# ll
总用量 4
-rw-r--r--. 1 root root 3776 8月   8 21:11 222.txt.gz
[root@localhost zip-test]# gzip -d 222.txt.gz
[root@localhost zip-test]# ll
总用量 12
-rw-r--r--. 1 root root 8927 8月   8 21:11 222.txt
[root@localhost zip-test]# cd ..
[root@localhost tmp]# gzip zip-test
gzip: zip-test is a directory -- ignored
[root@localhost tmp]# gzip -9 zip-test/222.txt
[root@localhost tmp]# zcat 222.txt.gz
gzip: 222.txt.gz: No such file or directory
[root@localhost tmp]# zcat zip-test/222.txt.gz
±³̐ʍ϶עˍӫτ

...
[root@localhost tmp]# wc -l zip-test/333.txt  #@查看文件内容行数: wc -l  文件名
46886 zip-test/333.txt
[root@localhost tmp]# find /etc/ -type f -name "*conf" -exec cat {} >> zip-test/333.txt \;

1. gzip压缩 gzip 原文件gzip压缩工具会把原文件替换掉)

[root@localhost ~]# gzip 22.txt

2. 查看gz格式的压缩文件:(gzip压缩工具会把原文件替换掉,生成.gz后缀格式的压缩文件!)

[root@localhost ~]# ls 22.txt.gz

3. 查看.gz压缩文件内容: zcat  压缩文件名

[root@localhost ~]# zcat 22.txt.gz

4gzip解压: gzip  -d  .gz压缩文件

[root@localhost ~]# gzip -d 22.txt.gz

5. gzip解压: gunzip .gz压缩文件

[root@localhost ~]# gunzip 22.txt.gz

6. gzip压缩原文件,同时原文件不被替换掉:  gzip -c 原文件 > 压缩文件名

[root@localhost ~]# gzip -c 22.txt > 22.txt.gz

7. gzip解压.gz压缩文件,同时.gz压缩文件不被替换掉:gzip -d -c  .gz压缩文件 解压文件名

[root@localhost ~]# gzip -d -c 22.txt.gz > 22.txt

 

6.3 bzip2压缩工具

yum安装bzip2压缩工具:

[root@localhost ~]# yum install -y bzip2

压缩级别:1-9级别(压缩级别越大,占用的cup资源越大!)默认压缩级别:6级别

bzip2压缩工具:只能压缩文件,不支持压缩目录!!!

查看文件格式

[root@localhost ~]# file 22.txt.gz

1. bzip2压缩 bzip2 原文件bzip2压缩工具会把原文件替换掉)

[root@localhost ~]# bzip2 22.txt

2. 查看bz2格式的压缩文件:(bzip2压缩工具会把原文件替换掉,生成.bz2后缀的压缩文件!)

[root@localhost ~]# ls 22.txt.bz2

3. 查看.bz2压缩文件内容: bzcat  压缩文件名

[root@localhost ~]# bzcat 22.txt.bz2

4. bzip2解压: bzip2  -d  .bz2压缩文件

[root@localhost ~]# bzip2 -d 22.txt.bz2

5. bzip2解压: bunzip2 .bz2压缩文件

[root@localhost ~]# bunzip2 22.txt.bz2

6. bzip2压缩文件,同时文件不被替换掉:  bzip2 -c 原文件 > 压缩文件名

[root@localhost ~]# bzip2 -c 22.txt > 22.txt.bz2

7. bzip2解压.bz2压缩文件,同时.bz2压缩文件不被替换掉:bzip2 -d -c  .bz2压缩文件 解压文件名

[root@localhost ~]# bzip2 -d -c 22.txt.bz2 > 22.txt

6.4 xz压缩工具

1. xz压缩 xz 原文件xz压缩工具会把原文件替换掉)

[root@localhost ~]# xz 22.txt

2. 查看xz格式的压缩文件:(xz压缩工具会把原文件替换掉,生成.xz后缀的压缩文件!)

[root@localhost ~]# ls 22.txt.xz

3. 查看.xz压缩文件内容: xz  压缩文件名

[root@localhost ~]# xzcat 22.txt.xz

4. xz解压: xz  -d  .bz2压缩文件

[root@localhost ~]# xz -d 22.txt.xz

5. xz解压: unxz .xz压缩文件

[root@localhost ~]# unxz 22.txt.xz

6. xz压缩文件,同时文件不被替换掉:  xz -c 原文件 > 压缩文件名

[root@localhost ~]# xz -c 22.txt > 22.txt.xz

7. xz解压.xz压缩文件,同时.xz压缩文件不被替换掉:xz -d -c  .xz压缩文件 解压文件名

[root@localhost ~]# xz -d -c 22.txt.xz > 22.txt

6.5 zip压缩工具

yum安装zip压缩工具:

[root@localhost ~]# yum install -y zip

1. zip压缩文件zip 压缩文件名 原文件

[root@localhost ~]# zip hao.txt.zip hao.txt

2. zip压缩目录zip -r   压缩目录名 原目录

[root@localhost ~]# zip -r mulu1.zip mulu

yum安装zip解压工具:

[root@localhost ~]# yum install -y unzip

3. zip解压文件unzip .zip压缩文件

[root@localhost ~]# unzip hao.txt.zip

4. zip解压文件,同时解压到指定路径unzip .zip压缩文件  -d 解压指定路径

[root@localhost ~]# unzip  hao.txt.zip   -d /tmp

5. zip解压目录unzip .zip目录压缩包

[root@localhost ~]# unzip mulu.zip

6. zip解压目录,同时解压到指定路径unzip .zip目录压缩包  -d 解压指定路径

[root@localhost ~]# unzip  mulu.zip   -d /tmp

7. 列出zip目录压缩包 文件列表unzip -l .zip目录压缩包

[root@localhost ~]# unzip -l mulu.zip

6.6 tar打包

1. tar打包目录tar -cvf   目录包名   原目录(v 的作用,可视打包过程,可以不添加!)

[root@localhost ~]# tar -cvf mulu.tar mulu1

2. tar解包.tar包tar -xvf .tar包  

[root@localhost ~]# tar -xvf mulu.tar

3. tar同时打包目录文件: tar -cvf 目录包名  原目录  文件

[root@localhost ~]# tar -cvf  mulu.tar mulu   hao.txt

4. 查看.tar目录包 文件列表: tar -tf tar目录包

[root@localhost ~]# tar -tf mulu.tar

5. tar打包目录,同时过滤目录中的文件目录不进行打包!:tar -cvf 目录包名  --exclude 过滤文件  --exclude 过滤目录  原目录

[root@localhost ~]# tar -cvf  mulu.tar   --exclude 22.txt   --exclude mulu3    mulu

6. tar打包目录,同时过滤目录中的所有后缀带有.txt的文件 不进行打包!:tar -cvf 目录包名  --exclude ".txt" 原目录

[root@localhost ~]# tar -cvf mulu.txt   --exclude ".txt"    mulu

6.7 打包并压缩

  • tar打包并gzip压缩

1. tar打包gzip压缩tar -zcvf .tar.gz打包压缩包名  文件  原目录

[root@localhost ~]# tar -zcvf mulu.tar.gz  hao.txt   mulu

2. gzip解压缩tar解包tar -zxvf  .tar.gz打包压缩包

[root@localhost ~]# tar -zxvf  mulu.tar.gz

3. 列出.tar.gz打包压缩包 文件列表:tar -tf .tar.gz打包压缩包

[root@localhost ~]# tar -tf mulu1.tar.gz

  • tar打包并bzip2压缩

1. tar打包bzip2压缩tar -jcvf .tar.bz2打包压缩包名  文件  原目录

[root@localhost ~]# tar -jcvf mulu.tar.bz2  hao.txt   mulu

2. gzip解压缩bzip2解包tar -jxvf  .tar.bz2打包压缩包

[root@localhost ~]# tar -jxvf  mulu.tar.bz2

3. 列出.tar.bzip2打包压缩包 文件列表:tar -tf .tar.bz2打包压缩包

[root@localhost ~]# tar -tf mulu1.tar.bz2

  • tar打包并xz压缩

1. tar打包并xz压缩tar -Jcvf .tar.xz打包压缩包名  文件  原目录

[root@localhost ~]# tar -Jcvf mulu.tar.xz  hao.txt   mulu

2. gzip解压缩并xz解包tar -Jxvf  .tar.xz打包压缩包

[root@localhost ~]# tar -Jxvf  mulu.tar.xz

3. 列出.tar.xz打包压缩包 文件列表:tar -tf .tar.xz打包压缩包

[root@localhost ~]# tar -tf mulu1.tar.xz

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值