歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(27)gzip命令

每天一個Linux命令(27)gzip命令

日期:2017/3/3 12:25:17   编辑:Linux技術

zip命令用來壓縮文件。gzip是個使用廣泛的壓縮程序,文件經它壓縮過後,其名稱後面會多處“.gz”擴展名。[b] (1)用法:[/b]

[b] 用法: gzip [選項參數][-s <壓縮字尾字符串>] [文件...][/b] 或 gzip [選項參數][-s <壓縮字尾字符串>] [目錄]

[b] (2)功能:[/b] 功能: gzip是個使用廣泛的解壓縮程序,它用於解開被gzip壓縮過的文件,這些壓縮文件預設最後的擴展名為".gz"。

事實上gzip就是gzip的硬連接,因此不論是壓縮或解壓縮,都可通過gzip指令單獨完成。[b] (3)選項參數:[/b]

1) -d --decompress --uncompress                  解開壓縮文件;

2) -v --verbose                           顯示指令執行過程;

3) -l --list                              列出壓縮文件的相關信息;

4) -r --recursive                          遞歸處理,將指定目錄下的所有文件及子目錄一並處理;

5) -A --catenate:                         新增文件到已存在的備份文件; 6) -B                               設置區塊大小

7) -c                                 把解壓後的文件輸出到標准輸出設備[b] (4)實例:[/b]

1)[root@localhost Dir]# gzip *      壓縮當前目錄下的所有文件,文件後綴名加上.gz(gzip調用時自動執行壓縮或解壓縮命令)

[root@localhost Dir]# gzip *
[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz

2)[root@localhost Dir]# gzip -d *     解壓當前目錄的所有文件

[root@localhost Dir]# gzip *                //解壓並不能像壓縮時那樣什麼參數都不帶,需要帶解壓命令-d
gzip: head_text.gz already has .gz suffix -- unchanged
gzip: less1.gz already has .gz suffix -- unchanged
gzip: less2.gz already has .gz suffix -- unchanged
[root@localhost Dir]# gzip -d *
[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 sunjimeng root      664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root      sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root      sunjimeng  57 5月   9 08:16 less2
3)[root@localhost Dir]# gzip -v *      顯示命令執行時的具體的步驟

[root@localhost Dir]# gzip -v *
head_text:     42.3% -- replaced with head_text.gz
less1:      4.4% -- replaced with less1.gz
less2:      1.8% -- replaced with less2.gz
[root@localhost Dir]# gzip -dv *
head_text.gz:     42.3% -- replaced with head_text
less1.gz:      4.4% -- replaced with less1
less2.gz:      1.8% -- replaced with less2
4)[root@localhost Dir]# gzip -l *

[root@localhost Dir]# gzip -l *
         compressed        uncompressed  ratio uncompressed_name
                411                 664  42.3% head_text
                 67                  45   4.4% less1
                 80                  57   1.8% less2
                558                 766  30.3% (totals)
[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
5)[root@localhost findDir]# tar -cvf Dir.tar Dir        先用tar命令打包

[root@localhost findDir]# tar -cvf Dir.tar Dir
Dir/
Dir/head_text.gz
Dir/less1.gz
Dir/less2.gz
[root@localhost findDir]# ll
總用量 12
dr-xr-xr-x. 2 root sunjimeng    55 5月  24 07:28 Dir
-rw-r--r--. 1 root root      10240 5月  24 07:34 Dir.tar
[root@localhost findDir]# gzip -v Dir.tar
Dir.tar:     92.1% -- replaced with Dir.tar.gz
[root@localhost findDir]# gzip -l Dir.tar.gz
         compressed        uncompressed  ratio uncompressed_name
                833               10240  92.1% Dir.tar
5)[root@localhost findDir]# tar cvf Dir1.tar -R Dir      打包的幾種方法

[root@localhost findDir]# tar cvf Dir1.tar -R Dir
塊 0:Dir/
塊 1:Dir/head_text.gz
塊 3:Dir/less1.gz
塊 5:Dir/less2.gz
[root@localhost findDir]# tar -cvf Dir2.tar Dir
Dir/
Dir/head_text.gz
Dir/less1.gz
Dir/less2.gz
[root@localhost findDir]# tar -cvf Dir3.tar -R Dir
塊 0:Dir/
塊 1:Dir/head_text.gz
塊 3:Dir/less1.gz
塊 5:Dir/less2.gz
6)[root@localhost Documents]# gzip -vr findDir      遞歸的壓縮子文件夾下的文件

[root@localhost Documents]# gzip -vr findDir
gzip: findDir/Dir/head_text.gz already has .gz suffix -- unchanged
gzip: findDir/Dir/less1.gz already has .gz suffix -- unchanged
gzip: findDir/Dir/less2.gz already has .gz suffix -- unchanged
gzip: findDir/Dir.tar.gz already has .gz suffix -- unchanged
findDir/Dir1.tar:     92.1% -- replaced with findDir/Dir1.tar.gz
findDir/Dir2.tar:     92.1% -- replaced with findDir/Dir2.tar.gz
findDir/Dir3.tar:     92.1% -- replaced with findDir/Dir3.tar.gz
[root@localhost Documents]# ls -l findDir
總用量 16
-rw-r--r--. 1 root root      833 5月  24 07:34 Dir.tar.gz
-rw-r--r--. 1 root root      834 5月  24 07:43 Dir3.tar.gz
-rw-r--r--. 1 root root      834 5月  24 07:39 Dir2.tar.gz
-rw-r--r--. 1 root root      834 5月  24 07:39 Dir1.tar.gz
dr-xr-xr-x. 2 root sunjimeng  55 5月  24 07:28 Dir
[root@localhost Documents]# ls -l findDir/Dir
總用量 12
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
7)[root@localhost findDir]# gzip -rdv Dir        遞歸的解壓目錄下的所有.gz的文件

[root@localhost findDir]# ls -l Dir
總用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
[root@localhost findDir]# gzip -rdv Dir
Dir/head_text.gz:     42.3% -- replaced with Dir/head_text
Dir/less1.gz:      4.4% -- replaced with Dir/less1
Dir/less2.gz:      1.8% -- replaced with Dir/less2
[root@localhost findDir]# ls -l Dir
總用量 12
-r-xr-xr-x. 1 sunjimeng root      664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root      sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root      sunjimeng  57 5月   9 08:16 less2
[root@localhost findDir]# gzip -r Dir
[root@localhost findDir]# ls -l Dir
總用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
[root@localhost findDir]# gzip -dv Dir
gzip: Dir is a directory -- ignored
8)[root@localhost Dir]# gzip --help           

[root@localhost Dir]# gzip --help
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]>.

Copyright © Linux教程網 All Rights Reserved