第二章 Linux 归档及压缩

归档和压缩

  1. 归档的含义,就是将许多零散的文件整理为一个文件,文件总的大小基本不变。

  2. 压缩的含义,按某种算法减小文件所占用空间的大小,恢复时按对应的逆向算法解压

常见的压缩格式及命令工具:
. gz —> gzip、gunzip
. bz2 —> bzip2、bunzip2
. xz —> xz、unxz

tar命令是什么

tape archive (简称tar)命令一般用于备份文件,在维护配置服务器时,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是必不可少的工具。
在Linux里最常用的打包程序就是tar,使用tar程序打出来的包,我们通常称为tar包,tar包文件的命令都是以三种格式.tar 结尾的。生成tar包后,就可以用其它的程序来进行压缩了。

tar工具的常用选项

  • tar 集成备份工具

    • -c : 创建归档
    • -x :释放归档
    • -f :指定归档文件名称
    • -t: 显示归档中的文件清单
    • -C:指定释放路径
    • -z、-j、-J:调用.gz.bz2.xz 格式的工具进行处理

制作tar包

  • 使用格式: tar -c ……命令
    • tar -zcf 备份文件 .tar.gz 被备份的文档……
    • tar -jcf 备份文件 .tar.bz2 被备份的文档……
    • tar -Jcf 备份文件 .tar.xz 被备份的文档……
[root@localhost ~]# tar  -zcf  /opt/home.tar.gz    /home     #:打包.gz格式的tar包
[root@localhost ~]# tar  -jcf  /opt/home.tar.bz2    /home     #:打包.bz2格式的tar包
[root@localhost ~]# tar  -Jcf  /opt/home.tar.xz    /home       #:打包.xz格式的tar包

[root@localhost ~]# ls   -lh   /opt/*
-rw-r--r--. 1 root root 610 10月  4 17:50 /opt/home.tar.bz2
-rw-r--r--. 1 root root 578 10月  4 17:50 /opt/home.tar.gz
-rw-r--r--. 1 root root 592 10月  4 17:50 /opt/home.tar.xz

/opt/rh:
总用量 0

查看tar包内容

  • 使用格式 :tar -t …… 命令

    • tar -tf 备份文件 . tar.gz
[root@localhost ~]# tar   -tf   /opt/home.tar.gz     #:查看.gz格式的tar包
home/
home/xh/
home/xh/.mozilla/
home/xh/.mozilla/extensions/
home/xh/.mozilla/plugins/
home/xh/.bash_logout
home/xh/.bash_profile
home/xh/.bashrc
[root@localhost ~]# tar   -tf   /opt/home.tar.bz2     #:查看.bz2格式的tar包
home/
home/xh/
home/xh/.mozilla/
home/xh/.mozilla/extensions/
home/xh/.mozilla/plugins/
home/xh/.bash_logout
home/xh/.bash_profile
home/xh/.bashrc
[root@localhost ~]# tar   -tf   /opt/home.tar.xz      #:查看.xz格式的tar包
home/
home/xh/
home/xh/.mozilla/
home/xh/.mozilla/extensions/
home/xh/.mozilla/plugins/
home/xh/.bash_logout
home/xh/.bash_profile
home/xh/.bashrc

解压tar包

  • 使用格式:tar -x …… 命令

    • tar -xf 备份文件.tar.gz [-C 目标文件夹]
[root@localhost ~]# rm  -rf  /home

[root@localhost ~]# tar  -xf   /opt/home.tar.gz  -C  /
[root@localhost ~]# ls   -lh  /home
总用量 0
drwx------. 3 xh xh 78 10月  4 15:12 xh

[root@localhost ~]# tar   -xf   /opt/home.tar.bz2   -C  /etc
[root@localhost ~]# ls   -ld   /etc/home
drwxr-xr-x. 3 root root 16 10月  4 15:19 /etc/home

[root@localhost ~]# tar   -xf   /opt/home.tar.xz   -C  /dev
[root@localhost ~]# ls   -ld   /dev/home
drwxr-xr-x. 3 root root 60 10月  4 15:19 /dev/home

zip命令是什么

zip是一个使用广泛具备兼容性的压缩程序,压缩后的文件后缀名为.zip,而且在Windows和Linux系统都是通用的,解压命令为unzip

zip工具的常用选项

  • zip压缩包格式文件是 Windows 和 Linux 系统都通用的压缩文件类型

  • zip工具的常用选项:

    • -r:递归压缩目录,包括目录下的所有文件跟子目录全部压缩

    • -m : 压缩文件后,删除原文件,相当于移动文件到压缩包中

    • -v : 显示详细的压缩过程

    • -q: 压缩的时候不显示信息

    • -1-9:压缩级别,从1~9的数字,-1代表压缩速度最快,-9代表压缩效果最好

    • -u:更新压缩文件,添加新文件

制作zip包

  • 使用: zip 压缩包名称.zip 被压缩的文件或目录

    • 格式:zip -r 压缩包名称.zip 被压缩的文件或目录
#: 打包mnt
[root@localhost ~]# zip   /opt/bian.zip  /mnt
  adding: mnt/ (stored 0%)
[root@localhost ~]# ls  -ld   /opt/bian.zip 
-rw-r--r--. 1 root root 158 10月  4 21:51 /opt/bian.zip


#: 同时打包多个文件跟,目录
[root@localhost ~]# zip  /opt/bin.zip   /etc/passwd   /boot/grub2   /mnt
  adding: etc/passwd (deflated 61%)
  adding: boot/grub2/ (stored 0%)
  adding: mnt/ (stored 0%)
[root@localhost ~]# ls   -lh   /opt/bin.zip
-rw-r--r--. 1 root root 1.4K 10月  4 21:52 /opt/bin.zip

unzip命令常用选项

  • zip包需要用到unzip来解压
  • unzip常用的选项
    • -d 目录名:将压缩文件指定到目录下解压
    • -n:解压时不覆盖已经存在的文件
    • -o:解压时覆盖已经存在的文件
    • -v:查看压缩文件的详细信息
    • -t:检测文件是否损坏,并不压缩
    • -x 文件列表:压缩文件,但不包含文件列表中指定的文件

解压zip包

  • 使用:unzip 压缩包名.zip
    • 格式:unzip -d /指定解压目录/ 压缩包名`.zip
#: 无论文件压缩包,还是目录,unzip都可以直接解压,不加选项,只能解压在当前目录
[root@localhost ~]# cd   /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# unzip   /opt/bian.zip 
Archive:  /opt/bian.zip
   creating: mnt/
[root@localhost mnt]# ls  
mnt



#:  加上 -d  指定dev目录 解压释放压缩文件
[root@localhost mnt]# unzip   -d  /dev/    /opt/bian.zip
Archive:  /opt/bian.zip
   creating: /dev/mnt/
[root@localhost mnt]# ls  -ld  /dev/mnt
drwxr-xr-x. 2 root root 40 10月  4 21:48 /dev/mnt

gzip命令是什么

gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。gzip不仅可以用来压缩大的、较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计,gzip命令对文本文件有60%~70%的压缩率。文件经它压缩过后,其名称后面会多出”.gz“的扩展名。

gzip工具的常用选项

  • gzip是个使用广泛的压缩程序
  • gzip 参数 [文件或者目录]
    • -a--ascii  使用ASCII文字模式。
    • -c--stdout--to-stdout  把压缩后的文件输出到标准输出设备,不去更动原始文件。
    • -d--decompress----uncompress  解开压缩文件。
    • -f--force  强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接。
    • -h--help  在线帮助。
    • -l--list  列出压缩文件的相关信息。
    • -L--license  显示版本与版权信息。
    • -n--no-name  压缩文件时,不保存原来的文件名称及时间戳记。
    • -N--name  压缩文件时,保存原来的文件名称及时间戳记。
    • -q--quiet  不显示警告信息。
    • -r--recursive  递归处理,将指定目录下的所有文件及子目录一并处理。
    • -S<压缩字尾字符串>或----suffix<压缩字尾字符串>  更改压缩字尾字符串。
    • -t--test  测试压缩文件是否正确无误。
    • -v--verbose  显示指令执行过程。
    • -V--version  显示版本信息。
    • -num 用指定的数字num调整压缩的速度,-1--fast表示最快压缩方法(低压缩比),-9--best表示最慢压缩方法(高压缩比)。系统缺省值为6
#:把opt下的文件压缩成 .gz 文件
[root@localhost ~]# gzip  /opt/*
[root@localhost ~]# ll   /opt/*
-rw-r--r--. 1 root root     416 10月  4 22:26 /opt/00-default.cfg.gz

#: 把opt目录下的文件解压,列出详细信息
[root@localhost ~]# gzip  -dv /opt/*
/opt/00-default.cfg.gz:	 56.7% -- replaced with /opt/00-default.cfg
[root@localhost ~]# ll  /opt
总用量 4
-rw-r--r--. 1 root root 885 10月  4 22:26 00-default.cfg
[root@localhost ~]# 


#:查看opt目录下的压缩包内容,不解压
[root@localhost ~]# gzip  /opt/*
[root@localhost ~]# ls  /opt/
00-default.cfg.gz
[root@localhost ~]# gzip  -l  /opt/*
         compressed        uncompressed  ratio uncompressed_name
                416                 885  56.7% /opt/00-default.cfg



#:压缩tar包,此时压缩文件的后缀名, 变成.tar.gz
[root@localhost ~]# ls   -ld  /opt/dbi.tar 
-rw-r--r--. 1 root root 10240 10月  4 22:39 /opt/dbi.tar
[root@localhost ~]# gzip  -r  /opt/dbi.tar   
[root@localhost ~]# ls   -ld  /opt/dbi.tar.gz 
-rw-r--r--. 1 root root 527 10月  4 22:39 /opt/dbi.tar.gz

gzip递归压缩

  • 使用格式:gzip -rv 需要压缩的目录或文件
[root@localhost ~]# gzip  -rv   /opt/*
/opt/ar.ttb:	 67.3% -- replaced with /opt/ar.ttb.gz
/opt/cil:	 -0.1% -- replaced with /opt/cil.gz
/opt/cs.ttb:	 66.7% -- replaced with /opt/cs.ttb.gz
/opt/generic:	 65.3% -- replaced with /opt/generic.gz
/opt/gnome-applications.menu:	 82.5% -- replaced with /opt/gnome-applications.menu.gz
/opt/hll:	 10.0% -- replaced with /opt/hll.gz
/opt/is.ttb:	 67.2% -- replaced with /opt/is.ttb.gz
/opt/nl_NL.ttb:	 69.6% -- replaced with /opt/nl_NL.ttb.gz
/opt/speechd.conf:	 61.5% -- replaced with /opt/speechd.conf.gz
/opt/xmldsig-core-schema.xsd:	 80.1% -- replaced with /opt/xmldsig-core-schema.xsd.gz

gzip递归解压

  • 使用格式:gzip -dr 压缩文件或目录名
[root@localhost ~]# ll  /opt/
总用量 56
-rw-r--r--. 1 root root 3073 10月  4 22:45 ar.ttb.gz
-rw-------. 1 root root 9508 10月  4 22:45 cil.gz
-rw-r--r--. 1 root root 3214 10月  4 22:45 cs.ttb.gz
-rw-r--r--. 1 root root 3534 10月  4 22:45 generic.gz
-rw-r--r--. 1 root root 1825 10月  4 22:45 gnome-applications.menu.gz
-rw-------. 1 root root 8791 10月  4 22:45 hll.gz
-rw-r--r--. 1 root root 3115 10月  4 22:45 is.ttb.gz
-rw-r--r--. 1 root root 3035 10月  4 22:45 nl_NL.ttb.gz
-rw-r--r--. 1 root root 3758 10月  4 22:45 speechd.conf.gz
-rw-r--r--. 1 root root 2058 10月  4 22:45 xmldsig-core-schema.xsd.gz
[root@localhost ~]# gzip  -dr   /opt/*
[root@localhost ~]# ll  /opt/
总用量 120
-rw-r--r--. 1 root root  9307 10月  4 22:45 ar.ttb
-rw-------. 1 root root  9481 10月  4 22:45 cil
-rw-r--r--. 1 root root  9581 10月  4 22:45 cs.ttb
-rw-r--r--. 1 root root 10106 10月  4 22:45 generic
-rw-r--r--. 1 root root 10212 10月  4 22:45 gnome-applications.menu
-rw-------. 1 root root  9746 10月  4 22:45 hll
-rw-r--r--. 1 root root  9409 10月  4 22:45 is.ttb
-rw-r--r--. 1 root root  9885 10月  4 22:45 nl_NL.ttb
-rw-r--r--. 1 root root  9669 10月  4 22:45 speechd.conf
-rw-r--r--. 1 root root 10144 10月  4 22:45 xmldsig-core-schema.xsd

gunzip命令是什么

在Linux里面,是一个使用广泛的解压缩命令,它用于解压被 gzip 压缩过的文件后缀名为 .gz,当然gzip -d 也可以解压gzip的压缩包。

gunzip工具的常用选项

  • gunzip命令的常用选项及含义
    • -r :递归解压
    • -c :把解压后的文件输出到标准输出设备
    • -f : 强制解压缩文件
    • -l :查看压缩文件内容
    • -v : 显示命令执行过程
    • -t :检测压缩文件是否正常,不解压

gunzip解压缩

  • 使用格式:gunzip 压缩包名
#:要是单个解压文件,则不需要加-r 
[root@localhost ~]# gzip  /opt/*
[root@localhost ~]# ll  /opt
总用量 56
-rw-r--r--. 1 root root 3073 10月  4 22:45 ar.ttb.gz
-rw-------. 1 root root 9508 10月  4 22:45 cil.gz
-rw-r--r--. 1 root root 3214 10月  4 22:45 cs.ttb.gz
-rw-r--r--. 1 root root 3534 10月  4 22:45 generic.gz
-rw-r--r--. 1 root root 1825 10月  4 22:45 gnome-applications.menu.gz
-rw-------. 1 root root 8791 10月  4 22:45 hll.gz
-rw-r--r--. 1 root root 3115 10月  4 22:45 is.ttb.gz
-rw-r--r--. 1 root root 3035 10月  4 22:45 nl_NL.ttb.gz
-rw-r--r--. 1 root root 3758 10月  4 22:45 speechd.conf.gz
-rw-r--r--. 1 root root 2058 10月  4 22:45 xmldsig-core-schema.xsd.gz
[root@localhost ~]# gunzip  -r   /opt/
[root@localhost ~]# ls  /opt
ar.ttb  cs.ttb   gnome-applications.menu  is.ttb     speechd.conf
cil     generic  hll                      nl_NL.ttb  xmldsig-core-schema.xsd


  • 要是压缩的是一个纯文本文件,则可以直接使用 zcat 命令在不解压缩的情况下查看这个文本文件中的内容
[root@localhost ~]# gzip   /opt/ar.ttb 
[root@localhost ~]# ls  -ld  /opt/ar.ttb.gz 
-rw-r--r--. 1 root root 3073 10月  4 22:45 /opt/ar.ttb.gz
[root@localhost ~]# zcat   /opt/ar.ttb.gz 
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 2008-2013 by The BRLTTY Developers.
#
…… 下面内容省略……

bzip2命令是什么

bzip2 命令同 gzip 命令类似,只能对文件进行压缩(或解压缩),对于目录只能压缩(或解压缩)该目录及子目录下的所有文件。当执行压缩任务完成后,会生成一个以.bz2为后缀名的压缩包。.bz2格式是 Linux 的另一种压缩格式,从理论上来讲,.bz2格式的算法更先进、压缩质量更好;而 .gz格式相对来讲压缩更快但压缩的质量没.bz2好。所以兄弟们应该知道怎么选择了嘻嘻。

bzip2工具的常用选项

  • -d:执行解压缩,此时该选项后的源文件应为标记有.bz2后缀的压缩包文件
  • -k:bzip2在压缩跟解压完文件后,会删除原文件,需要保留则选择,此选项
  • -f: 强制覆盖现有文件
  • -t:测试压缩包文件的完整性
  • -v: 显示压缩跟解压过程
  • -1-9:参数和gzip命令的作用一样,用于指定压缩等级,-1 压缩等级最低,压缩比最差,-9 压缩比最高

bzip2压缩文件

  • 使用格式:bzip2 文件或目录名

  • bizp2压缩命令默认会在压缩的同时删除源文件,需要保留源文件加上`-k

[root@localhost ~]# bzip2   /opt/cil

[root@localhost ~]# ls  /opt/cil
ls: 无法访问/opt/cil: 没有那个文件或目录

[root@localhost ~]# ls  -ld  /opt/cil.bz2
-rw-------. 1 root root 9950 10月  4 22:45 /opt/cil.bz2


#:压缩的同时保留源文件
[root@localhost ~]# ll  /opt/
总用量 12
-rw-------. 1 root root 9746 10月  4 22:45 hll
[root@localhost ~]# bzip2   -k  /opt/hll
[root@localhost ~]# ll  /opt
总用量 24
-rw-------. 1 root root 9746 10月  4 22:45 hll
-rw-------. 1 root root 9358 10月  4 22:45 hll.bz2

bzip2解压文件

  • 使用格式:bzip2 -d 压缩目录或文件
[root@localhost ~]# ll  /opt/
总用量 12
-rw-------. 1 root root 9358 10月  4 22:45 hll.bz2
[root@localhost ~]# bzip2  -d  /opt/hll.bz2
[root@localhost ~]# ll  /opt
总用量 12
-rw-------. 1 root root 9746 10月  4 22:45 hll

bunzip2命令是什么

要解压.bz2格式的压缩包文件,除了使用bzip2 -d 压缩包名 命令外,还可以使用 bunzip2 命令。bunzip2 命令的使用和 gunzip 命令大致相同,bunzip2 命令只能用于解压文件,即便解压目录,也是解压该目录以及所含子目录下的所有文件。

bunzip2工具的常用选项

  • -k:系统在解压缩完成后,会默认删除原文件,需要保留就用此参数

  • -f : 覆盖现有文件

  • -v:显示执行过程

  • -L:查看压缩文件内容

bunzip2解压文件

  • 使用格式: bunzip2 压缩目录或文件
[root@localhost ~]# bzip2   /opt/hll
[root@localhost ~]# ll    /opt
总用量 12
-rw-------. 1 root root 9358 10月  4 22:45 hll.bz2
[root@localhost ~]# bunzip2   /opt/hll.bz2 
[root@localhost ~]# ll   /opt
总用量 12
-rw-------. 1 root root 9746 10月  4 22:45 hll


#: .bz2 格式压缩的纯文本文件也可以不解压缩直接查看,使用的命令是 bzcat
[root@localhost ~]# bzip2  /opt/hll 
[root@localhost ~]# ll  /opt
总用量 12
-rw-------. 1 root root 9358 10月  4 22:45 hll.bz2
[root@localhost ~]# bzcat  /opt/hll.bz2 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值