layout | title | category | tags | ||
---|---|---|---|---|---|
post |
Gzip and Bzip2 on Linux |
Linux |
|
gzip 可以说是应用度最广的压缩命令了!目前 gzip 可以解开 compress, zip 与 gzip 等软件所压缩的文件。
[root@server Workspace]# 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
--rsyncable Make rsync-friendly archive
With no FILE, or when FILE is -, read standard input.
Report bugs to <bug-gzip@gnu.org>.
压缩
[test@server ~]$ gzip -v netstat.log
netstat.log: 93.0% -- replaced with netstat.log.gz
[test@server ~]$ ll
total 4
-rw-rw-r-- 1 test test 1868 Mar 11 17:11 netstat.log.gz
继续压缩
[test@server ~]$ gzip -v netstat.log.gz
gzip: netstat.log.gz already has .gz suffix -- unchanged
[test@server ~]$ gzip -v -S .gz netstat.log.gz -c > netstat.log.gz.gz
netstat.log.gz: 0.2%
[test@server ~]$ ll
total 8
-rw-rw-r-- 1 test test 1799 Mar 11 16:12 netstat.log.gz
-rw-rw-r-- 1 test test 1837 Mar 11 16:17 netstat.log.gz.gz
解压缩
[test@server ~]$ gzip -v -d netstat.log.gz
netstat.log.gz: 93.0% -- replaced with netstat.log
测试完整性
[test@server ~]$ gzip -v -t netstat.log.gz
netstat.log.gz: OK
[test@server ~]$ touch dummy.gz;gzip -v -t dummy.gz
gzip: dummy.gz: unexpected end of file
列表文件
[test@server ~]$ gzip -v -l netstat.log.gz
method crc date time compressed uncompressed ratio uncompressed_name
defla 73d6b845 Mar 11 16:12 1799 25013 93.0% netstat.log
1.仅能对单个文件(即输入一个文件,输出一个文件)
[test@server ~]$ mkdir -p foo/bar
[test@server ~]$ free > foo/free.log
[test@server ~]$ netstat > foo/bar/netstat.log
[test@server ~]$ tree .
.
└── foo
├── bar
│ └── netstat.log
└── free.log
2 directories, 2 files
[test@server ~]$ gzip foo/
gzip: foo/ is a directory -- ignored
[test@server ~]$ gzip foo/*
gzip: foo/bar is a directory -- ignored
2.通过递归,支持对文件夹的压缩(gzip -r
),但解压缩需单独一一进行
[test@server ~]$ gzip -v -r foo/
foo//bar/netstat.log: 92.5% -- replaced with foo//bar/netstat.log.gz
foo//free.log: 44.8% -- replaced with foo//free.log.gz
3.默认情况下,gzip压缩后的文件后缀名为.gz(gzip -S ".your_suffix"
)
[test@server ~]$ gzip -v -S ".gzip" passwd
passwd: 61.4% -- replaced with passwd.gzip
gzip默认后缀为.gz,当更改后缀名后,解压时,需要指定后缀名
[test@server ~]$ gzip -v -d passwd.gzip
gzip: passwd.gzip: unknown suffix -- ignored
[test@server ~]$ gzip -v -d -S ".gzip" passwd.gzip
passwd.gzip: 61.4% -- replaced with passwd
4.压缩、解压缩时,保留原有文件权限
压缩前,文件权限
[root@server ~]# ll
total 4
-rw-r--r-- 1 root root 5 Mar 11 16:49 whoami.log
更改权限为777
[root@server ~]# chmod 777 whoami.log
[root@server ~]# chown test:test whoami.log
压缩后,文件权限
[root@server ~]# gzip -v whoami.log
whoami.log: 120.0% -- replaced with whoami.log.gz
[root@server ~]# ll
total 4
-rwxrwxrwx 1 test test 36 Mar 11 16:49 whoami.log.gz
解压后,文件权限
[root@server ~]# gzip -v -d whoami.log.gz
whoami.log.gz: 120.0% -- replaced with whoami.log
[root@server ~]# ll
total 4
-rwxrwxrwx 1 test test 5 Mar 11 16:49 whoami.log
注:在使用 gzip -c 配合 > 重定向时,相当于创建文件,此时文件权限为于当前用户设定的权限。
5.压缩时,默认状态下,原文件会被压缩后的文件替换;解压时类似
此时,可以使用gzip -c > your_file_name
来重定向输出文件,同时保存原有文件。不指定文件时,默认会输出到标准输出stdout上。
压缩
[test@server ~]$ gzip -v -S .gz netstat.log.gz -c > netstat.log.gz.gz
netstat.log.gz: 0.2%
[test@server ~]$ ll
total 8
-rw-rw-r-- 1 test test 1799 Mar 11 16:12 netstat.log.gz
-rw-rw-r-- 1 test test 1837 Mar 11 16:17 netstat.log.gz.gz
解压缩
[root@server ~]# gzip -vdc whoami.log.gz > whoami
whoami.log.gz: 120.0%
[root@server Workspace]# ll
total 8
-rw-r--r-- 1 root root 5 Mar 11 17:08 whoami
-rw-r--r-- 1 test test 36 Mar 11 17:04 whoami.log.gz
6.压缩后的文本文件可以直接使用zcat读出(zcat your_file_name.gz)
[test@server ~]$ zcat netstat.log.gz
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
... ...
若说 gzip 是为了取代 compress 并提供更好的压缩比而成立的,那么 bzip2 则是为了取代 gzip 并提供更佳的压缩比而来的。 bzip2 真是很不错用的东西~这玩意的压缩比竟然比 gzip 还要好~至於 bzip2 的用法几乎与 gzip 相同!
[root@server svn]# bzip2 --help
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
If invoked as `bzip2', default action is to compress.
as `bunzip2', default action is to decompress.
as `bzcat', default action is to decompress to stdout.
If no file names are given, bzip2 compresses or decompresses
from standard input to standard output. You can combine
short flags, so `-v -4' means the same as -v4 or -4v, &c.
gzip的替代工具bzip2,压缩比较gzip高,用法与gzip类似,不再赘述。
1.压缩后的文本文件可以直接使用bzcat读出
[root@server ~]# bzcat netstat.log.bz2
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
... ...