不管是普通使用linux系统,还是做为一名运维人员,掌握文件的压缩与归档都是有必要的,在linux系统上压缩与归档的工具是比较多的,今天我就带大家了解一些比较常见的压缩与归档工具。
一、compress/uncompress 默认会删除原文件
compress [-dfvcVr] [-b maxbits] [file ...]
-d: 解压缩,相当于uncompress
-c: 结果输出至标准输出,不删除原文件
-v: 显示详情
uncompress 解压缩
zcat 不解压缩的前提下查看文本文件内容
示例:
[[email protected] ziptest]# compress -c locale-archive >locale-archive.Z
[[email protected] ziptest]# ll -h
total 148M
-rw-r--r--. 1 root root 102M Aug 15 17:23 locale-archive
-rw-r--r--. 1 root root 47M Aug 15 17:26 locale-archive.Z
[[email protected] ziptest]#
二、gzip/gunzip 默认会删除原文件
gzip [OPTION]... FILE ...
-d: 解压缩,相当于gunzip
-c: 将压缩或解压缩的结果输出至标准输出,不删除原文件
-#:1-9,指定压缩比,值越大压缩比越大
zcat:不解压缩的前提下查看文本文件内容
示例:
[[email protected] ziptest]# gzip -c locale-archive >locale-archive
.gz[[email protected] ziptest]# ll -h
total 171M
-rw-r--r--. 1 root root 102M Aug 15 17:23 locale-archive
-rw-r--r--. 1 root root 23M Aug 15 17:31 locale-archive.gz
-rw-r--r--. 1 root root 47M Aug 15 17:26 locale-archive.Z
[[email protected] ziptest]#
三、bzip2/bunzip2 对于大文件有着更大的压缩比
bzip2 [OPTION]... FILE ...
-k: keep, 保留原文件
-d:解压缩
-#:1-9,压缩比,默认为6
bzcat:不解压缩的前提下查看文本文件内容
示例:
[[email protected] ziptest]# bzip2 -k locale-archive locale-archive
.bz2bzip2: Input file locale-archive.bz2 already has .bz2 suffix.
[[email protected] ziptest]# ll -h
total 191M
-rw-r--r--. 1 root root 102M Aug 15 17:23 locale-archive
-rw-r--r--. 1 root root 21M Aug 15 17:23 locale-archive.bz2
-rw-r--r--. 1 root root 23M Aug 15 17:31 locale-archive.gz
-rw-r--r--. 1 root root 47M Aug 15 17:26 locale-archive.Z
[[email protected] ziptest]#
四、xz/unxz 压缩比最大
xz [OPTION]... FILE ...
-k: keep, 保留原文件;
-d:解压缩
-#:1-9,压缩比,默认为6;
xzcat: 不显式解压缩的前提下查看文本文件内容
示例:
[[email protected] ziptest]# xz -k locale-archive locale-archive.xz
xz: locale-archive.xz: File already has `.xz‘ suffix, skipping
[[email protected] ziptest]# ll -h
total 195M
-rw-r--r--. 1 root root 102M Aug 15 17:23 locale-archive
-rw-r--r--. 1 root root 21M Aug 15 17:23 locale-archive.bz2
-rw-r--r--. 1 root root 23M Aug 15 17:31 locale-archive.gz
-rw-r--r--. 1 root root 3.3M Aug 15 17:23 locale-archive.xz
-rw-r--r--. 1 root root 47M Aug 15 17:26 locale-archive.Z
[[email protected] ziptest]#
由此可见各个压缩工具的的压缩性能:xz > bz2 > gz > compress
五、zip/unzip 压缩并归档(不删除原文件)
zip -r zipname.zip file... 打包并压缩
zip –r sysconfig.zip /etc/sysconfig
unzip sysconfig.zip 解包并解压缩
六、tar 只归档不压缩
(1) 创建归档
tar -c -f /PATH/TO/SOMEFILE.tar FILE...
tar -cf /PATH/TO/SOMEFILE.tar FILE...
(2) 查看归档文件中的文件列表
tar -t -f /PATH/TO/SOMEFILE.tar
(3) 展开归档
tar -x -f /PATH/TO/SOMEFILE.tar
tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/
(4) 结合压缩工具实现:归档并压缩
tar -zcf:归档并调用gzip压缩
tar -zxf:调用gzip解压缩并展开归档
示例:
[[email protected] tartest]# cp -r /etc/sysconfig .
[[email protected] tartest]# ll -h
total 4.0K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
[[email protected] tartest]# tar -zcf sysconfig.tar.gz sysconfig
[[email protected] tartest]# ll -h
total 112K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]# mv sysconfig sysconfig2
[[email protected] tartest]# ll -h
total 112K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]# tar -zxf sysconfig.tar.gz
[[email protected] tartest]# ll -h
total 116K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]#
tar -jcf:归档并调用bzip压缩
tar -jxf:调用bzip解压缩并展开归档
示例:
[[email protected] tartest]# ll -h
total 112K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]# tar -jcf sysconfig.tar.bz2 sysconfig
[[email protected] tartest]# ll -h
total 212K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]# mv sysconfig sysconfig2
[[email protected] tartest]# ll -h
total 212K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig2
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]# tar -jxf sysconfig.tar.bz2
[[email protected] tartest]# ll -h
total 216K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig2
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]#
tar -Jcf:归档并调用xz压缩
tar -Jxf:调用xz解压缩并展开归档
示例:
[[email protected] tartest]# ll -h
total 212K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
[[email protected] tartest]# tar -Jcf sysconfig.tar.xz sysconfig
[[email protected] tartest]# ll -h
total 308K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
-rw-r--r--. 1 root root 94K Aug 15 20:24 sysconfig.tar.xz
[[email protected] tartest]# mv sysconfig sysconfig2
[[email protected] tartest]# ll -h
total 308K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig2
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
-rw-r--r--. 1 root root 94K Aug 15 20:24 sysconfig.tar.xz
[[email protected] tartest]# tar -Jxf sysconfig.tar.xz
[[email protected] tartest]# ll -h
total 312K
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig
drwxr-xr-x. 6 root root 4.0K Aug 15 20:15 sysconfig2
-rw-r--r--. 1 root root 100K Aug 15 20:20 sysconfig.tar.bz2
-rw-r--r--. 1 root root 105K Aug 15 20:16 sysconfig.tar.gz
-rw-r--r--. 1 root root 94K Aug 15 20:24 sysconfig.tar.xz
[[email protected] tartest]#
从这儿我们可以看出,小文件归档并压缩反而会变大,但是如果是大文件归档并压缩就可以看出压缩的效果了
七、cpio 复制 从或到文件
cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件。
cpio [选项] > 文件名或者设备名
cpio [选项] < 文件名或者设备名
选项
-o 将文件拷贝打包成文件或者将文件输出到设备上
-i 解包,将打包文件解压或将设备上的备份还原到系统
-t 预览,查看文件内容或者输出到设备上的文件内容
-v 显示打包过程中的文件名称
-d 解包生成目录,在cpio还原时,自动的建立目录
-c 一种较新的存储方式
示例:
[[email protected] cpiotest]# cp /boot/initrd-plymouth.img .
[[email protected] cpiotest]# ll -h
total 9.8M
-rw-r--r--. 1 root root 9.8M Aug 15 20:58 initrd-plymouth.img
[[email protected] cpiotest]# file initrd-plymouth.img
initrd-plymouth.img: gzip compressed data, from Unix, last modif
ied: Thu Jul 21 12:54:24 2016, max compression
[[email protected] cpiotest]# mv initrd-plymouth.img initrd-plymouth.img.gz
[[email protected] cpiotest]# gzip -d initrd-plymouth.img.gz
[[email protected] cpiotest]# ll -h
total 20M
-rw-r--r--. 1 root root 20M Aug 15 20:58 initrd-plymouth.img
[[email protected] cpiotest]# file initrd-plymouth.img
initrd-plymouth.img: ASCII cpio archive (SVR4 with no CRC)
[[email protected] cpiotest]#
cpio -t 预览一下这个镜像文件里面的内容
[[email protected] cpiotest]# cpio -t < initrd-plymouth.img
.
usr
usr/share
usr/share/plymouth
usr/share/plymouth/themes
usr/share/plymouth/themes/text
usr/share/plymouth/themes/text/text.plymouth
usr/share/plymouth/themes/details
usr/share/plymouth/themes/details/details.plymouth
usr/share/plymouth/themes/charge
usr/share/plymouth/themes/charge/animation-0001.png
usr/share/plymouth/themes/charge/animation-0002.png
usr/share/plymouth/themes/charge/animation-0003.png
usr/share/plymouth/themes/charge/animation-0004.png
usr/share/plymouth/themes/charge/animation-0005.png
usr/share/plymouth/themes/charge/animation-0006.png
usr/share/plymouth/themes/charge/animation-0007.png
usr/share/plymouth/themes/charge/animation-0008.png
usr/share/plymouth/themes/charge/animation-0009.png
usr/share/plymouth/themes/charge/animation-0010.png
usr/share/plymouth/themes/charge/animation-0011.png
usr/share/plymouth/themes/charge/animation-0012.png
usr/share/plymouth/themes/charge/animation-0013.png
usr/share/plymouth/themes/charge/animation-0014.png
usr/share/plymouth/themes/charge/animation-0015.png
usr/share/plymouth/themes/charge/animation-0016.png
usr/share/plymouth/themes/charge/animation-0017.png
usr/share/plymouth/themes/charge/animation-0018.png
usr/share/plymouth/themes/charge/animation-0019.png
usr/share/plymouth/themes/charge/animation-0020.png
usr/share/plymouth/themes/charge/animation-0021.png
usr/share/plymouth/themes/charge/animation-0022.png
usr/share/plymouth/themes/charge/animation-0023.png
usr/share/plymouth/themes/charge/animation-0024.png
usr/share/plymouth/themes/charge/animation-0025.png
usr/share/plymouth/themes/charge/animation-0026.png
usr/share/plymouth/themes/charge/animation-0027.png
usr/share/plymouth/themes/charge/animation-0028.png
usr/share/plymouth/themes/charge/animation-0029.png
usr/share/plymouth/themes/charge/animation-0030.png
usr/share/plymouth/themes/charge/animation-0031.png
usr/share/plymouth/themes/charge/animation-0032.png
usr/share/plymouth/themes/charge/animation-0033.png
usr/share/plymouth/themes/charge/animation-0034.png
usr/share/plymouth/themes/charge/animation-0035.png
usr/share/plymouth/themes/charge/animation-0036.png
usr/share/plymouth/themes/charge/background-tile.png
usr/share/plymouth/themes/charge/box.png
usr/share/plymouth/themes/charge/bullet.png
usr/share/plymouth/themes/charge/charge.plymouth
usr/share/plymouth/themes/charge/entry.png
usr/share/plymouth/themes/charge/lock.png
usr/share/plymouth/themes/charge/throbber-0001.png
usr/share/plymouth/themes/charge/throbber-0002.png
usr/share/plymouth/themes/charge/throbber-0003.png
usr/share/plymouth/themes/charge/throbber-0004.png
usr/share/plymouth/themes/charge/throbber-0005.png
usr/share/plymouth/themes/charge/throbber-0006.png
usr/share/plymouth/themes/charge/throbber-0007.png
usr/share/plymouth/themes/charge/throbber-0008.png
usr/share/plymouth/themes/charge/throbber-0009.png
usr/share/plymouth/themes/charge/throbber-0010.png
usr/share/plymouth/themes/charge/throbber-0011.png
usr/share/plymouth/themes/charge/throbber-0012.png
usr/share/plymouth/themes/charge/watermark.png
usr/share/plymouth/plymouthd.defaults
usr/share/pixmaps
usr/share/pixmaps/system-logo-white.png
usr/share/fonts
usr/share/fonts/dejavu
usr/share/fonts/dejavu/DejaVuSans.ttf
usr/share/fonts/vlgothic
usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf
usr/share/fonts/nhn-nanum
usr/share/fonts/nhn-nanum/NanumGothic.ttf
usr/share/fonts/lohit-assamese
usr/share/fonts/lohit-assamese/Lohit-Assamese.ttf
usr/share/fonts/lohit-bengali
usr/share/fonts/lohit-bengali/Lohit-Bengali.ttf
usr/share/fonts/lohit-gujarati
usr/share/fonts/lohit-gujarati/Lohit-Gujarati.ttf
usr/share/fonts/lohit-devanagari
usr/share/fonts/lohit-devanagari/Lohit-Devanagari.ttf
usr/share/fonts/lohit-kannada
usr/share/fonts/lohit-kannada/Lohit-Kannada.ttf
usr/share/fonts/smc
usr/share/fonts/smc/Meera.ttf
usr/share/fonts/lohit-oriya
usr/share/fonts/lohit-oriya/Lohit-Oriya.ttf
usr/share/fonts/lohit-punjabi
usr/share/fonts/lohit-punjabi/Lohit-Punjabi.ttf
usr/share/fonts/lohit-tamil
usr/share/fonts/lohit-tamil/Lohit-Tamil.ttf
usr/share/fonts/lohit-telugu
usr/share/fonts/lohit-telugu/Lohit-Telugu.ttf
usr/lib64
usr/lib64/libply.so.2.1.0
usr/lib64/libply.so.2
usr/lib64/libply-splash-core.so.2.1.0
usr/lib64/libply-splash-core.so.2
usr/lib64/libudev.so.1.6.2
usr/lib64/libudev.so.1
usr/lib64/librt-2.17.so
usr/lib64/librt.so.1
usr/lib64/libdw-0.163.so
usr/lib64/libdw.so.1
usr/lib64/libgcc_s-4.8.5-20150702.so.1
usr/lib64/libgcc_s.so.1
usr/lib64/libattr.so.1.1.0
usr/lib64/libattr.so.1
usr/lib64/libelf-0.163.so
usr/lib64/libelf.so.1
usr/lib64/liblzma.so.5.0.99
usr/lib64/liblzma.so.5
usr/lib64/libbz2.so.1.0.6
usr/lib64/libbz2.so.1
usr/lib64/plymouth
usr/lib64/plymouth/text.so
usr/lib64/plymouth/details.so
usr/lib64/plymouth/two-step.so
usr/lib64/plymouth/renderers
usr/lib64/plymouth/renderers/drm.so
usr/lib64/plymouth/renderers/frame-buffer.so
usr/lib64/plymouth/label.so
usr/lib64/libply-splash-graphics.so.2.1.0
usr/lib64/libply-splash-graphics.so.2
usr/lib64/libpng15.so.15.13.0
usr/lib64/libpng15.so.15
usr/lib64/libdrm.so.2.4.0
usr/lib64/libdrm.so.2
usr/lib64/libpangocairo-1.0.so.0.3600.8
usr/lib64/libpangocairo-1.0.so.0
usr/lib64/libpango-1.0.so.0.3600.8
usr/lib64/libpango-1.0.so.0
usr/lib64/libgobject-2.0.so.0.4200.2
usr/lib64/libgobject-2.0.so.0
usr/lib64/libglib-2.0.so.0.4200.2
usr/lib64/libglib-2.0.so.0
usr/lib64/libpangoft2-1.0.so.0.3600.8
usr/lib64/libpangoft2-1.0.so.0
usr/lib64/libgmodule-2.0.so.0.4200.2
usr/lib64/libgmodule-2.0.so.0
usr/lib64/libgthread-2.0.so.0.4200.2
usr/lib64/libgthread-2.0.so.0
usr/lib64/libharfbuzz.so.0.936.0
usr/lib64/libharfbuzz.so.0
usr/lib64/libfontconfig.so.1.7.0
usr/lib64/libfontconfig.so.1
usr/lib64/libfreetype.so.6.10.0
usr/lib64/libfreetype.so.6
usr/lib64/libpixman-1.so.0.32.6
usr/lib64/libpixman-1.so.0
usr/lib64/libEGL.so.1.0.0
usr/lib64/libEGL.so.1
usr/lib64/libxcb-shm.so.0.0.0
usr/lib64/libxcb-shm.so.0
usr/lib64/libxcb-render.so.0.0.0
usr/lib64/libxcb-render.so.0
usr/lib64/libxcb.so.1.1.0
usr/lib64/libxcb.so.1
usr/lib64/libXrender.so.1.3.0
usr/lib64/libXrender.so.1
usr/lib64/libX11.so.6.3.0
usr/lib64/libX11.so.6
usr/lib64/libXext.so.6.4.0
usr/lib64/libXext.so.6
usr/lib64/libGL.so.1.2.0
usr/lib64/libGL.so.1
usr/lib64/libffi.so.6.0.1
usr/lib64/libffi.so.6
usr/lib64/libgraphite2.so.3.0.1
usr/lib64/libgraphite2.so.3
usr/lib64/libexpat.so.1.6.0
usr/lib64/libexpat.so.1
usr/lib64/libX11-xcb.so.1.0.0
usr/lib64/libX11-xcb.so.1
usr/lib64/libxcb-dri2.so.0.0.0
usr/lib64/libxcb-dri2.so.0
usr/lib64/libxcb-xfixes.so.0.0.0
usr/lib64/libxcb-xfixes.so.0
usr/lib64/libxcb-shape.so.0.0.0
usr/lib64/libxcb-shape.so.0
usr/lib64/libgbm.so.1.0.0
usr/lib64/libgbm.so.1
usr/lib64/libselinux.so.1
usr/lib64/libXau.so.6.0.0
usr/lib64/libXau.so.6
usr/lib64/libglapi.so.0.0.0
usr/lib64/libglapi.so.0
usr/lib64/libXdamage.so.1.1.0
usr/lib64/libXdamage.so.1
usr/lib64/libXfixes.so.3.1.0
usr/lib64/libXfixes.so.3
usr/lib64/libxcb-glx.so.0.0.0
usr/lib64/libxcb-glx.so.0
usr/lib64/libxcb-dri3.so.0.0.0
usr/lib64/libxcb-dri3.so.0
usr/lib64/libxcb-present.so.0.0.0
usr/lib64/libxcb-present.so.0
usr/lib64/libxcb-randr.so.0.1.0
usr/lib64/libxcb-randr.so.0
usr/lib64/libxcb-sync.so.1.0.0
usr/lib64/libxcb-sync.so.1
usr/lib64/libxshmfence.so.1.0.0
usr/lib64/libxshmfence.so.1
usr/lib64/libXxf86vm.so.1.0.0
usr/lib64/libXxf86vm.so.1
usr/lib64/libpcre.so.1.2.0
usr/lib64/libpcre.so.1
usr/sbin
usr/sbin/plymouthd
usr/bin
usr/bin/plymouth
usr/lib
usr/lib/systemd
usr/lib/systemd/system
usr/lib/systemd/system/systemd-ask-password-plymouth.path
usr/lib/systemd/system/systemd-ask-password-plymouth.service
usr/lib/systemd/system/plymouth-switch-root.service
usr/lib/systemd/system/plymouth-start.service
usr/lib/systemd/system/plymouth-quit.service
usr/lib/systemd/system/plymouth-quit-wait.service
usr/lib/systemd/system/plymouth-reboot.service
usr/lib/systemd/system/plymouth-kexec.service
usr/lib/systemd/system/plymouth-poweroff.service
usr/lib/systemd/system/plymouth-halt.service
usr/lib/systemd/system/initrd-switch-root.target.wants
usr/lib/systemd/system/initrd-switch-root.target.wants/plymouth-
switch-root.serviceusr/lib/systemd/system/initrd-switch-root.target.wants/plymouth-
start.serviceusr/lib/systemd/system/sysinit.target.wants
usr/lib/systemd/system/sysinit.target.wants/plymouth-start.servi
ceusr/lib/systemd/system/multi-user.target.wants
usr/lib/systemd/system/multi-user.target.wants/plymouth-quit.ser
viceusr/lib/systemd/system/multi-user.target.wants/plymouth-quit-wai
t.serviceusr/lib/systemd/system/reboot.target.wants
usr/lib/systemd/system/reboot.target.wants/plymouth-reboot.servi
ceusr/lib/systemd/system/kexec.target.wants
usr/lib/systemd/system/kexec.target.wants/plymouth-kexec.service
usr/lib/systemd/system/poweroff.target.wants
usr/lib/systemd/system/poweroff.target.wants/plymouth-poweroff.s
erviceusr/lib/systemd/system/halt.target.wants
usr/lib/systemd/system/halt.target.wants/plymouth-halt.service
lib64
sbin
bin
etc
etc/os-release
etc/plymouth
etc/plymouth/plymouthd.conf
39590 blocks
[[email protected] cpiotest]#
cpio -i 解包(会在当前目录中生成打包时的目录及文件)
[[email protected] cpiotest]# cpio -i < initrd-plymouth.img
39590 blocks
[[email protected] cpiotest]# ll -h
total 20M
lrwxrwxrwx. 1 root root 7 Aug 15 21:28 bin -> usr/bin
drwxr-xr-x. 3 root root 4.0K Aug 15 21:28 etc
-rw-r--r--. 1 root root 20M Aug 15 21:23 initrd-plymouth.img
lrwxrwxrwx. 1 root root 9 Aug 15 21:28 lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 8 Aug 15 21:28 sbin -> usr/sbin
drwxr-xr-x. 7 root root 4.0K Aug 15 21:28 usr
[[email protected]os7 cpiotest]#
cpio 自制linux系统需要用到的工具,帮助我们去打包当前系统上的标准数据
基本上所有的运用示例都贴上来了,相信大家看的明白,理解的了。