实验系统环境:
[[email protected] tmp]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
[[email protected] tmp]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[[email protected] tmp]# uname -a
Linux cacti.getg.com 2.6.32-504.3.3.el6.x86_64 #1 SMP Wed Dec 17 01:55:02 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
以下案例涉及如下这些命令的使用演示:
zip、unzip、gzip、gunzip、bzip2、bunzip2、xz、unxz、zcat、tar
zip/unzip:
[[email protected] tmp]# ls -l
total 3096
-rw------- 1 root root 3167456 Apr 7 11:30 messages
//常规压缩,不删除原文件
[[email protected] tmp]# zip messages.zip messages
adding: messages (deflated 91%)
[[email protected] tmp]# ls -l
total 3384
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw-r--r-- 1 root root 293753 Apr 7 11:33 messages.zip
//压缩目录(使用-r选项)
[[email protected] tmp]# zip -r ziptest.zip ./ziptest/
adding: ziptest/ (stored 0%)
adding: ziptest/messages.2 (deflated 91%)
adding: ziptest/messages.1 (deflated 91%)
adding: ziptest/messages (deflated 91%)
[[email protected] tmp]# ls -l
total 868
drwxr-xr-x 2 root root 4096 Apr 7 11:43 ziptest
-rw-r--r-- 1 root root 881415 Apr 7 11:46 ziptest.zip
//显示压缩过程详细信息(使用-v选项)
[[email protected] tmp]# zip -rv ziptest1.zip ziptest
adding: ziptest/ (in=0) (out=0) (stored 0%)
adding: ziptest/messages.2 (in=3167456) (out=293587) (deflated 91%)
adding: ziptest/messages.1 (in=3167456) (out=293587) (deflated 91%)
adding: ziptest/messages (in=3167456) (out=293587) (deflated 91%)
total bytes=9502368, compressed=880761 -> 91% savings
//解压缩文件
[[email protected] tmp]# unzip ziptest.zip
Archive: ziptest.zip
creating: ziptest/
inflating: ziptest/messages.2
inflating: ziptest/messages.1
inflating: ziptest/messages
[[email protected] tmp]# ls -l ziptest
total 9288
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw------- 1 root root 3167456 Apr 7 11:42 messages.1
-rw------- 1 root root 3167456 Apr 7 11:42 messages.2
//仅用于归档存储(使用-0选项),不压缩文件,相当于tar的作用
[[email protected] tmp]# zip -0 -r ziptest.zip ziptest/
adding: ziptest/ (stored 0%)
adding: ziptest/messages.2 (stored 0%)
adding: ziptest/messages.1 (stored 0%)
adding: ziptest/messages (stored 0%)
[[email protected] tmp]# du -s ziptest
9292 ziptest
[[email protected] tmp]# ls -l
total 12384
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 11:43 ziptest
-rw-r--r-- 1 root root 9503022 Apr 7 11:54 ziptest.zip
[[email protected] tmp]# du -s ziptest.zip
9284 ziptest.zip
//验证压缩文件是否有损毁
[[email protected] tmp]# zip -T ziptest.zip
test of ziptest.zip OK
//压缩文件添加注释
[[email protected] tmp]# zip -z ziptest.zip
enter new zip file comment (end with .):
This is a test file.
.
//仅显示压缩文件中的注释信息
[[email protected] tmp]# unzip -z ziptest.zip
Archive: ziptest.zip
This is a test file.
//对目录进行压缩时,排除指定的文件不压缩
[[email protected] tmp]# zip -rv ziptest1.zip ziptest -x ziptest/messages
adding: ziptest/ (in=0) (out=0) (stored 0%)
adding: ziptest/messages.2 (in=3167456) (out=293587) (deflated 91%)
adding: ziptest/messages.1 (in=3167456) (out=293587) (deflated 91%)
total bytes=6334912, compressed=587174 -> 91% savings
//同时对多个文件进行压缩
[[email protected] ziptest]# zip messages.zip messages messages.1 messages.2
adding: messages (deflated 91%)
adding: messages.1 (deflated 91%)
adding: messages.2 (deflated 91%)
gzip/gunzip:
//gzip常用选项及用法说明
[[email protected] ~]# gzip -h
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).
Mandatory arguments to long options are mandatory for short options too.
-c, --stdout write on standard output, keep original files unchanged
-d, --decompress decompress
-f, --force force overwrite of output file and compress links
-h, --help give this help
-l, --list list compressed file contents
-L, --license display software license
-n, --no-name do not save or restore the original name and time stamp
-N, --name save or restore the original name and time stamp
-q, --quiet suppress all warnings
-r, --recursive operate recursively on directories
-S, --suffix=SUF use suffix SUF on compressed files
-t, --test test compressed file integrity
-v, --verbose verbose mode
-V, --version display version number
-1, --fast compress faster
-9, --best compress better
//gunzip常用选项及用法说明
[[email protected] tmp]# gunzip -h
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).
Mandatory arguments to long options are mandatory for short options too.
-c, --stdout write on standard output, keep original files unchanged
-d, --decompress decompress
-f, --force force overwrite of output file and compress links
-h, --help give this help
-l, --list list compressed file contents
-L, --license display software license
-n, --no-name do not save or restore the original name and time stamp
-N, --name save or restore the original name and time stamp
-q, --quiet suppress all warnings
-r, --recursive operate recursively on directories
-S, --suffix=SUF use suffix SUF on compressed files
-t, --test test compressed file integrity
-v, --verbose verbose mode
-V, --version display version number
-1, --fast compress faster
-9, --best compress better
--rsyncable Make rsync-friendly archive
//常规压缩,但会删除原文件,不需要指定压缩文件名
[[email protected] tmp]# gzip messages
[[email protected] tmp]# ls -l
total 292
-rw------- 1 root root 293614 Apr 7 11:51 messages.gz
drwxr-xr-x 2 root root 4096 Apr 7 13:40 ziptest
//gzip对目录进行压缩(使用-r选项),不会将目录压缩成一个文件,他会将目录中的每一个文件分别压缩
[[email protected] tmp]# gzip -r ziptest/
[[email protected] tmp]# ls -l
total 292
-rw------- 1 root root 293614 Apr 7 11:51 messages.gz
drwxr-xr-x 2 root root 4096 Apr 7 13:47 ziptest
[[email protected] tmp]# cd ziptest/
[[email protected] ziptest]# ls
messages.1.gz messages.2.gz messages.gz
[[email protected] ziptest]# ls -l
total 864
-rw------- 1 root root 293616 Apr 7 11:42 messages.1.gz
-rw------- 1 root root 293616 Apr 7 11:42 messages.2.gz
-rw------- 1 root root 293614 Apr 7 11:30 messages.gz
//解压缩文件可以使用gunzip或gzip -d,for example:
[[email protected] tmp]# ls -l
total 292
-rw------- 1 root root 293614 Apr 7 11:51 messages.gz
drwxr-xr-x 2 root root 4096 Apr 7 13:47 ziptest
[[email protected] tmp]#
[[email protected] tmp]#
[[email protected] tmp]# gunzip messages.gz
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:47 ziptest
[[email protected] tmp]# gzip messages
[[email protected] tmp]# ls -l
total 292
-rw------- 1 root root 293614 Apr 7 11:51 messages.gz
drwxr-xr-x 2 root root 4096 Apr 7 13:47 ziptest
[[email protected] tmp]# gzip -d messages.gz
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:47 ziptest
//gzip不能压缩目录,所直接解压缩目录中的文件,必须使用-r选项,如果待解压的文件在目录中,如果对这个目录进行解压必须加上-r选项,才能解压目录中的压缩文件,for example:
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:53 ziptest
[[email protected] tmp]# ls -l ziptest/
total 864
-rw------- 1 root root 293616 Apr 7 11:42 messages.1.gz
-rw------- 1 root root 293616 Apr 7 11:42 messages.2.gz
-rw------- 1 root root 293614 Apr 7 11:30 messages.gz
[[email protected] tmp]# gzip -d ziptest/
gzip: ziptest/ is a directory -- ignored
[[email protected] tmp]# gzip -dr ziptest/
[[email protected] tmp]# ls -l ziptest/
total 9288
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw------- 1 root root 3167456 Apr 7 11:42 messages.1
-rw------- 1 root root 3167456 Apr 7 11:42 messages.2
//显示压缩过程信息
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
[[email protected] tmp]# gzip -v messages
messages: 90.7% -- replaced with messages.gz
[[email protected] tmp]# gzip -dv messages.gz
messages.gz: 90.7% -- replaced with messages
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
//不解压文件,直接查看文件内容,使用zcat命令,注意对于打包之后进行的压缩文件不可以使用该命令
[[email protected] tmp]# ls -l
total 292
-rw------- 1 root root 293614 Apr 7 11:51 messages.gz
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
[[email protected] tmp]# zcat messages.gz |tail
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init()
Jan 14 16:44:22 cacti /usr/sbin/bmc-watchdog[14102]: Get Watchdog Timer: driver timeout
Jan 14 16:45:01 cacti CROND[14995]: (cacti) CMD (php /var/www/html/cacti/poller.php > /dev/null 2>&1)
---------------------------------------------------------------------------------------------------------
from here start!!
[[email protected] tmp]#
bzip2/bunzip2:
//bzip2常用选项及用法说明
[[email protected] tmp]# bzip2 -h
bzip2, a block-sorting file compressor. Version 1.0.5, 10-Dec-2007.
usage: bzip2 [flags and input files in any order]
-h --help print this message
-d --decompress force decompression
-z --compress force compression
-k --keep keep (don‘t delete) input files
-f --force overwrite existing output files
-t --test test compressed file integrity
-c --stdout output to standard out
-q --quiet suppress noncritical error messages
-v --verbose be verbose (a 2nd -v gives more)
-L --license display software version & license
-V --version display software version & license
-s --small use less memory (at most 2500k)
-1 .. -9 set block size to 100k .. 900k
--fast alias for -1
--best alias for -9
//bunzip2常用选项及用法说明
[[email protected] tmp]# bunzip2 -h
bzip2, a block-sorting file compressor. Version 1.0.5, 10-Dec-2007.
usage: bunzip2 [flags and input files in any order]
-h --help print this message
-d --decompress force decompression
-z --compress force compression
-k --keep keep (don‘t delete) input files
-f --force overwrite existing output files
-t --test test compressed file integrity
-c --stdout output to standard out
-q --quiet suppress noncritical error messages
-v --verbose be verbose (a 2nd -v gives more)
-L --license display software version & license
-V --version display software version & license
-s --small use less memory (at most 2500k)
-1 .. -9 set block size to 100k .. 900k
--fast alias for -1
--best alias for -9
//常规压缩,不保留原文件,请留意,bzip2在压缩的过程中会明显感觉比gzip工具慢,但压缩比会很高
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
[[email protected] tmp]# bzip2 messages
[[email protected] tmp]# ls -l
total 172
-rw------- 1 root root 170116 Apr 7 11:51 messages.bz2
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
//使用-v选项显示压缩过程详细信息
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
[[email protected] tmp]# bzip2 -v messages
messages: 18.619:1, 0.430 bits/byte, 94.63% saved, 3167456 in, 170116 out.
[[email protected] tmp]# ls -l
total 172
-rw------- 1 root root 170116 Apr 7 11:51 messages.bz2
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
//使用-k选项,保留源文件不被删除
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
[[email protected] tmp]# bzip2 -vk messages
messages: 18.619:1, 0.430 bits/byte, 94.63% saved, 3167456 in, 170116 out.
[[email protected] tmp]# ls -l
total 3268
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw------- 1 root root 170116 Apr 7 11:51 messages.bz2
drwxr-xr-x 2 root root 4096 Apr 7 13:54 ziptest
//bzip2不能直接对目录进行压缩或解压缩,bzip2只能对文件进行操作,如果想同时对目录中的多个文件进行压缩,可以使用通配符,不支持-r选项,for example:
[[email protected] tmp]# ls -l ziptest/
total 9288
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw------- 1 root root 3167456 Apr 7 11:42 messages.1
-rw------- 1 root root 3167456 Apr 7 11:42 messages.2
[[email protected] tmp]# bzip2 -v ziptest/*
ziptest/messages: 18.619:1, 0.430 bits/byte, 94.63% saved, 3167456 in, 170116 out.
ziptest/messages.1: 18.619:1, 0.430 bits/byte, 94.63% saved, 3167456 in, 170116 out.
ziptest/messages.2: 18.619:1, 0.430 bits/byte, 94.63% saved, 3167456 in, 170116 out.
[[email protected] tmp]# ls -l ziptest/
total 504
-rw------- 1 root root 170116 Apr 7 11:42 messages.1.bz2
-rw------- 1 root root 170116 Apr 7 11:42 messages.2.bz2
-rw------- 1 root root 170116 Apr 7 11:30 messages.bz2
//解压缩文件,可以使用bunzip2或bzip -d,常规解压缩,for example:
[[email protected] tmp]# ls -l
total 172
-rw------- 1 root root 170116 Apr 7 11:51 messages.bz2
drwxr-xr-x 2 root root 4096 Apr 7 14:39 ziptest
[[email protected] tmp]# bunzip2 messages.bz2
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 14:39 ziptest
[[email protected] tmp]# bzip2 messages
[[email protected] tmp]# ls -l
total 172
-rw------- 1 root root 170116 Apr 7 11:51 messages.bz2
drwxr-xr-x 2 root root 4096 Apr 7 14:39 ziptest
[[email protected] tmp]# bzip2 -d messages.bz2
[[email protected] tmp]# ls -l
total 3100
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 2 root root 4096 Apr 7 14:39 ziptest
//对目录中的多个文件进行解压缩
[[email protected] tmp]# ls -l ziptest/
total 504
-rw------- 1 root root 170116 Apr 7 11:42 messages.1.bz2
-rw------- 1 root root 170116 Apr 7 11:42 messages.2.bz2
-rw------- 1 root root 170116 Apr 7 11:30 messages.bz2
[[email protected] tmp]# bzip2 -d -v ziptest/*
ziptest/messages.1.bz2: done
ziptest/messages.2.bz2: done
ziptest/messages.bz2: done
[[email protected] tmp]# ls -l ziptest/
total 9288
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw------- 1 root root 3167456 Apr 7 11:42 messages.1
-rw------- 1 root root 3167456 Apr 7 11:42 messages.2
//将解压缩文件内容输出到标准输出中,及屏幕上,不生成解压缩文件(使用-c和-d选项),for example:
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# cat testbzip2.txt
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init()
Jan 14 16:44:22 cacti /usr/sbin/bmc-watchdog[14102]: Get Watchdog Timer: driver timeout
Jan 14 16:45:01 cacti CROND[14995]: (cacti) CMD (php /var/www/html/cacti/poller.php > /dev/null 2>&1)
---------------------------------------------------------------------------------------------------------
from here start!!
[[email protected] tmp]# bzip2 testbzip2.txt
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 383 Apr 7 14:50 testbzip2.txt.bz2
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# bzip2 -c -d testbzip2.txt.bz2
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] Unable to locate theme engine in module_path: "clearlooks",
Jan 14 16:44:20 cacti vmusr[14641]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init()
Jan 14 16:44:22 cacti /usr/sbin/bmc-watchdog[14102]: Get Watchdog Timer: driver timeout
Jan 14 16:45:01 cacti CROND[14995]: (cacti) CMD (php /var/www/html/cacti/poller.php > /dev/null 2>&1)
---------------------------------------------------------------------------------------------------------
from here start!!
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 383 Apr 7 14:50 testbzip2.txt.bz2
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
xz/unxz:
[[email protected] ~]# xz -h
Usage: xz [OPTION]... [FILE]...
Compress or decompress FILEs in the .xz format.
Mandatory arguments to long options are mandatory for short options too.
-z, --compress force compression
-d, --decompress force decompression
-t, --test test compressed file integrity
-l, --list list information about files
-k, --keep keep (don‘t delete) input files
-f, --force force overwrite of output file and (de)compress links
-c, --stdout write to standard output and don‘t delete input files
-0 .. -9 compression preset; 0-2 fast compression, 3-5 good
compression, 6-9 excellent compression; default is 6
-e, --extreme use more CPU time when encoding to increase compression
ratio without increasing memory usage of the decoder
-q, --quiet suppress warnings; specify twice to suppress errors too
-v, --verbose be verbose; specify twice for even more verbose
-h, --help display this short help
-H, --long-help display the long help (lists also the advanced options)
-V, --version display the version number
[[email protected] ~]# unxz -h
Usage: unxz [OPTION]... [FILE]...
Compress or decompress FILEs in the .xz format.
Mandatory arguments to long options are mandatory for short options too.
-z, --compress force compression
-d, --decompress force decompression
-t, --test test compressed file integrity
-l, --list list information about files
-k, --keep keep (don‘t delete) input files
-f, --force force overwrite of output file and (de)compress links
-c, --stdout write to standard output and don‘t delete input files
-0 .. -9 compression preset; 0-2 fast compression, 3-5 good
compression, 6-9 excellent compression; default is 6
-e, --extreme use more CPU time when encoding to increase compression
ratio without increasing memory usage of the decoder
-q, --quiet suppress warnings; specify twice to suppress errors too
-v, --verbose be verbose; specify twice for even more verbose
-h, --help display this short help
-H, --long-help display the long help (lists also the advanced options)
-V, --version display the version number
//常规压缩,源文件会被删除。
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# xz messages
[[email protected] tmp]# ls -l
total 104
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
//显示压缩过程详细信息
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# xz -v messages
messages (1/1)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
[[email protected] tmp]# ls -l
total 104
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
//显示压缩过程,同时保持原文件不被删除(使用-v 和 -k 选项)
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# xz -v -k messages
messages (1/1)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
[[email protected] tmp]# ls -l
total 3200
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
//解压缩文件,unxz file_name 或 xz -d file_name
[[email protected] tmp]# ls -l
total 104
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# unxz messages.xz
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# xz messages
[[email protected] tmp]# ls -l
total 104
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
[[email protected] tmp]# xz -d messages.xz
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 14:46 ziptest
//xz命令同样不支持对目录进行操作,使用方法与bzip2类似,对目录中的文件进行压缩,必须使用通配符,并且如果目录中有多个文件,会对每个文件生成一个压缩文件,for example:
[[email protected] tmp]# ls -l ziptest/
total 9288
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw------- 1 root root 3167456 Apr 7 11:42 messages.1
-rw------- 1 root root 3167456 Apr 7 11:42 messages.2
[[email protected] tmp]# xz -v ziptest/
xz: ziptest/: Is a directory, skipping
[[email protected] tmp]# xz -v ziptest/*
ziptest/messages (1/3)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
ziptest/messages.1 (2/3)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
ziptest/messages.2 (3/3)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
[[email protected] tmp]# ls -l ziptest/
total 288
-rw------- 1 root root 97456 Apr 7 11:42 messages.1.xz
-rw------- 1 root root 97456 Apr 7 11:42 messages.2.xz
-rw------- 1 root root 97456 Apr 7 11:30 messages.xz
[[email protected] tmp]# xz -d -v ziptest/
xz: ziptest/: Is a directory, skipping
[[email protected] tmp]# xz -d -v ziptest/*
ziptest/messages.1.xz (1/3)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
ziptest/messages.2.xz (2/3)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
ziptest/messages.xz (3/3)
100.0 % 95.2 KiB / 3,093.2 KiB = 0.031
[[email protected] tmp]# ls -l ziptest/
total 9288
-rw------- 1 root root 3167456 Apr 7 11:30 messages
-rw------- 1 root root 3167456 Apr 7 11:42 messages.1
-rw------- 1 root root 3167456 Apr 7 11:42 messages.2
tar:
NAME
tar - manual page for tar 1.23
SYNOPSIS
tar [OPTION...] [FILE]...
DESCRIPTION
GNU ‘tar’ saves many files together into a single tape or disk archive, and can restore individual files from the
archive.
Note that this manual page contains just very brief description (or more like a list of possible functionality)
originally generated by the help2man utility. The full documentation for tar is maintained as a Texinfo manual.
If the info and tar programs are properly installed at your site, the command ‘info tar’ should give you access
to the complete manual.
EXAMPLES
tar -cf archive.tar foo bar
# Create archive.tar from files foo and bar.
tar -tvf archive.tar
# List all files in archive.tar verbosely.
tar -xf archive.tar
# Extract all files from archive.tar.
Main operation mode:
-A, --catenate, --concatenate
append tar files to an archive
-c, --create
create a new archive
-d, --diff, --compare
find differences between archive and file system
--delete
delete from the archive (not on mag tapes!)
-r, --append
append files to the end of an archive
-t, --list
list the contents of an archive
--test-label
test the archive volume label and exit
-u, --update
only append files newer than copy in archive
-x, --extract, --get
extract files from an archive
-C, --directory=DIR
change to directory DIR
-f, --file=ARCHIVE
use archive file or device ARCHIVE
-j, --bzip2
filter the archive through bzip2
-J, --xz
filter the archive through xz
-p, --preserve-permissions
extract information about file permissions (default for superuser)
-v, --verbose
verbosely list files processed
-z, --gzip
filter the archive through gzip
//常规打包文件夹,打包归档不会删除原文件,-c创建文件、-v显示打包过程、-f表示指定文件
[[email protected] tmp]# ls -l
total 112
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
drwx------ 2 root root 4096 Jan 1 1970 orbit-root
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwx------ 2 root root 4096 Apr 8 09:32 vmware-root
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
[[email protected] tmp]# tar -cvf ziptest.tar ziptest/
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 9404
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
drwx------ 2 root root 4096 Jan 1 1970 orbit-root
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwx------ 2 root root 4096 Apr 8 09:32 vmware-root
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 9512960 Apr 8 09:43 ziptest.tar
//不解包,显示归档中有哪些文件
[[email protected] tmp]# tar -tvf ziptest.tar
drwxr-xr-x root/root 0 2015-04-07 17:22 ziptest/
-rw------- root/root 3167456 2015-04-07 11:42 ziptest/messages.2
-rw------- root/root 3167456 2015-04-07 11:42 ziptest/messages.1
-rw------- root/root 3167456 2015-04-07 11:30 ziptest/messages
//解包归档的文件,使用-x选项
[[email protected] tmp]# ls -l
total 9400
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
drwx------ 2 root root 4096 Jan 1 1970 orbit-root
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwx------ 2 root root 4096 Apr 8 09:32 vmware-root
-rw-r--r-- 1 root root 9512960 Apr 8 09:43 ziptest.tar
[[email protected] tmp]#
[[email protected] tmp]#
[[email protected] tmp]# tar -xvf ziptest.tar
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 9404
-rw------- 1 root root 97456 Apr 7 11:51 messages.xz
drwx------ 2 root root 4096 Jan 1 1970 orbit-root
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwx------ 2 root root 4096 Apr 8 09:32 vmware-root
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 9512960 Apr 8 09:43 ziptest.tar
//在打包的过程中,调用压缩程序,从而达到对目录进行归档压缩的目的。使用-z选项调用gzip压缩、使用-j调用bzip2压缩、使用-J调用xz压缩,他们的区别是xz的压缩比大于bzip2,bzip2的压缩比大于gzip,for example:
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
[[email protected] tmp]# tar -zcvf ziptest.tar.gz ziptest/
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 3968
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
[[email protected] tmp]# tar -Jcvf ziptest.tar.xz ziptest
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4068
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# tar -jcvf ziptest.tar.bz2 ziptest
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
//对于在打包过程中调用压缩程序的打包文件,解压的时候无需指定相应的参数也能解压文件,当然指定选项也不会报错,for Example:
[[email protected] tmp]# ls -l
total 4560
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# tar -xvf ziptest.tar.bz2
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# rm -rf ziptest
[[email protected] tmp]# tar -xvf ziptest.tar.gz
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# rm -rf ziptest
[[email protected] tmp]# tar -xvf ziptest.tar.xz
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# rm -rf ziptest
[[email protected] tmp]# tar -jxvf ziptest.tar.bz2
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# rm -rf ziptest
[[email protected] tmp]# tar -zxvf ziptest.tar.gz
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
[[email protected] tmp]# rm -rf ziptest
[[email protected] tmp]# tar -Jxvf ziptest.tar.xz
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 4564
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 505453 Apr 8 09:55 ziptest.tar.bz2
-rw-r--r-- 1 root root 880963 Apr 8 09:53 ziptest.tar.gz
-rw-r--r-- 1 root root 98388 Apr 8 09:54 ziptest.tar.xz
//在tar包文件中添加新的文件,使用-r选项,for Example:
[[email protected] tmp]# ls -l
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
[[email protected] tmp]# tar -cvf ziptest.tar ziptest/
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
[[email protected] tmp]# ls -l
total 12396
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 9512960 Apr 8 10:39 ziptest.tar
[[email protected] tmp]# tar -r messages testbzip2.txt -f ziptest.tar
[[email protected] tmp]# ls -l
total 15484
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 12677120 Apr 8 10:40 ziptest.tar
[[email protected] tmp]# tar -tvf ziptest.tar
drwxr-xr-x root/root 0 2015-04-07 17:22 ziptest/
-rw------- root/root 3167456 2015-04-07 11:42 ziptest/messages.2
-rw------- root/root 3167456 2015-04-07 11:42 ziptest/messages.1
-rw------- root/root 3167456 2015-04-07 11:30 ziptest/messages
-rw------- root/root 3167456 2015-04-07 11:51 messages
-rw-r--r-- root/root 989 2015-04-07 14:50 testbzip2.txt
//将归档文件解压到新的目录,使用-C选项,注意,-C 及其后的目录放在前,放在后都可以
[[email protected] tmp]# ls -l
total 15484
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 12677120 Apr 8 10:40 ziptest.tar
[[email protected] tmp]# mkdir newdir
[[email protected] tmp]# tar -C newdir -xvf ziptest.tar
ziptest/
ziptest/messages.2
ziptest/messages.1
ziptest/messages
messages
testbzip2.txt
[[email protected] tmp]# ls -l
total 15488
-rw------- 1 root root 3167456 Apr 7 11:51 messages
drwxr-xr-x 3 root root 4096 Apr 8 10:44 newdir
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest
-rw-r--r-- 1 root root 12677120 Apr 8 10:40 ziptest.tar
[[email protected] tmp]# ls -l newdir/
total 3104
-rw------- 1 root root 3167456 Apr 7 11:51 messages
-rw-r--r-- 1 root root 989 Apr 7 14:50 testbzip2.txt
drwxr-xr-x 2 root root 4096 Apr 7 17:22 ziptest