6.3 bzip2压缩工具

时间:2022-04-27
本文章向大家介绍6.3 bzip2压缩工具,主要内容包括bzip2目录概要、bzip2压缩文件、bzip2解压文件(两种办法)、bzip2压缩文件,并指定目录、bzip解压文件,并指定目录(两种方法)、bzip2压缩级别、file查看压缩文件大小、bzcat查看压缩文件内容、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

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.txt
2.0M	1.txt
[root@hf-01 d6z]# bzip2 1.txt
[root@hf-01 d6z]# ls
1.txt.bz2
[root@hf-01 d6z]# du -sh 1.txt.bz2
168K	1.txt.bz2

bzip2解压文件(两种办法)

  • bzip2 -d 1.txt 解压文件
[root@hf-01 d6z]# bzip2 -d 1.txt.bz2
[root@hf-01 d6z]# ls
1.txt
[root@hf-01 d6z]# du -sh 1.txt
1.5M	1.txt
  • bunzip2 1.txt.bz2 解压文件
[root@hf-01 d6z]# bunzip2 1.txt.bz2
[root@hf-01 d6z]# ls
1.txt

bzip2压缩文件,并指定目录

  • 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.bz2
168K	/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]# ls
1.txt  4.txt
[root@hf-01 d6z]# du -sh 4.txt
1.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]# ls
1.txt  4.txt  5.txt

bzip2压缩级别

  • bzip2默认压缩级别为 9

file查看压缩文件大小

  • file命令,会知道压缩文件的类型,大小
[root@hf-01 d6z]# file 1.txt.bz2
1.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

查看的时候,需注意压缩文件是否为空,若为空,则看不到什么文件