1. 程式人生 > >Gzip and Bzip2 on Linux

Gzip and Bzip2 on Linux

gzip 可以說是應用度最廣的壓縮命令了!目前 gzip 可以解開 compress, zip 與 gzip 等軟體所壓縮的檔案。

gzip help

[[email protected] 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 <[email protected]
>.

用法

壓縮

[[email protected] ~]$ gzip -v netstat.log 
netstat.log:	 93.0% -- replaced with netstat.log.gz
[[email protected] ~]$ ll
total 4
-rw-rw-r-- 1 test test 1868 Mar 11 17:11 netstat.log.gz

繼續壓縮

[[email protected] ~]$ gzip -v netstat.log.gz 
gzip: netstat.log.gz already has .gz suffix -- unchanged
[[email protected]
~]$ gzip -v -S .gz netstat.log.gz -c > netstat.log.gz.gz netstat.log.gz: 0.2% [[email protected] ~]$ 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

解壓縮

[[email protected] ~]$ gzip -v -d netstat.log.gz 
netstat.log.gz:	 93.0% -- replaced with netstat.log

測試完整性

[[email protected] ~]$ gzip -v -t netstat.log.gz 
netstat.log.gz:	 OK

[[email protected] ~]$ touch dummy.gz;gzip -v -t dummy.gz

gzip: dummy.gz: unexpected end of file

列表檔案

[[email protected] ~]$ 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.僅能對單個檔案(即輸入一個檔案,輸出一個檔案)

[[email protected] ~]$ mkdir -p foo/bar
[[email protected] ~]$ free > foo/free.log
[[email protected] ~]$ netstat > foo/bar/netstat.log
[[email protected] ~]$ tree .
.
└── foo
    ├── bar
    │   └── netstat.log
    └── free.log

2 directories, 2 files
[[email protected] ~]$ gzip foo/
gzip: foo/ is a directory -- ignored
[[email protected] ~]$ gzip foo/*
gzip: foo/bar is a directory -- ignored

2.通過遞迴,支援對資料夾的壓縮(gzip -r),但解壓縮需單獨一一進行

[[email protected] ~]$ 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")

[[email protected] ~]$ gzip -v -S ".gzip" passwd 
passwd:	 61.4% -- replaced with passwd.gzip

gzip預設字尾為.gz,當更改字尾名後,解壓時,需要指定字尾名

[[email protected] ~]$ gzip -v -d passwd.gzip 
gzip: passwd.gzip: unknown suffix -- ignored
[[email protected] ~]$ gzip -v -d -S ".gzip" passwd.gzip 
passwd.gzip:	 61.4% -- replaced with passwd

4.壓縮、解壓縮時,保留原有檔案許可權

壓縮前,檔案許可權

[[email protected] ~]# ll
total 4
-rw-r--r-- 1 root root   5 Mar 11 16:49 whoami.log

更改許可權為777

[[email protected] ~]# chmod 777 whoami.log 
[[email protected] ~]# chown test:test whoami.log 

壓縮後,檔案許可權

[[email protected] ~]# gzip -v whoami.log 
whoami.log:	120.0% -- replaced with whoami.log.gz
[[email protected] ~]# ll
total 4
-rwxrwxrwx 1 test test  36 Mar 11 16:49 whoami.log.gz

解壓後,檔案許可權

[[email protected] ~]# gzip -v -d whoami.log.gz 
whoami.log.gz:	120.0% -- replaced with whoami.log
[[email protected] ~]# ll
total 4
-rwxrwxrwx 1 test test   5 Mar 11 16:49 whoami.log

注:在使用 gzip -c 配合 > 重定向時,相當於建立檔案,此時檔案許可權為於當前使用者設定的許可權。

5.壓縮時,預設狀態下,原檔案會被壓縮後的檔案替換;解壓時類似

此時,可以使用gzip -c > your_file_name來重定向輸出檔案,同時儲存原有檔案。不指定檔案時,預設會輸出到標準輸出stdout上。

壓縮

[[email protected] ~]$ gzip -v -S .gz netstat.log.gz -c > netstat.log.gz.gz
netstat.log.gz:	  0.2%
[[email protected] ~]$ 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

解壓縮

[[email protected] ~]# gzip -vdc whoami.log.gz > whoami
whoami.log.gz:	120.0%
[[email protected] 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)

[[email protected] ~]$ zcat netstat.log.gz 
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State  
... ...

bzip2

若說 gzip 是為了取代 compress 並提供更好的壓縮比而成立的,那麼 bzip2 則是為了取代 gzip 並提供更佳的壓縮比而來的。 bzip2 真是很不錯用的東西~這玩意的壓縮比竟然比 gzip 還要好~至於 bzip2 的用法幾乎與 gzip 相同!

bzip2 help

[[email protected] 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讀出

[[email protected] ~]# bzcat netstat.log.bz2 
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State  
... ...

參考

blog comments powered by Disqus