归档和压缩
归档的含义,就是将许多零散的文件整理为一个文件,文件总的大小基本不变。
压缩的含义linux压缩,按某种算法减小文件所占用空间的大小,恢复时按对应的逆向算法解压
常见的压缩格式及命令工具:
. gz —> gzip、gunzip
. bz2 —> bzip2、bunzip2
. xz —> xz、unxz
tar命令是什么
tape archive (简称tar)命令一般用于备份文件,在维护配置服务器时,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是必不可少的工具。
在Linux里最常用的打包程序就是tar,使用tar程序打出来的包,我们通常称为tar包,tar包文件的命令都是以三种格式.tar 结尾的。生成tar包后,就可以用其它的程序来进行压缩了。
tar工具的常用选项 制作tar包
[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包内容
[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包
[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包
#: 打包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都可以直接解压,不加选项,只能解压在当前目录
[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工具的常用选项
#:把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递归压缩
[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递归解压
[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
[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
[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工具的常用选项 bzip2压缩文件
[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解压文件
[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工具的常用选项 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
(编辑:成都站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|