天天看點

Linux gzip 壓縮/解壓 詳解

gzip 是linux中常見的壓縮/解壓工具,最常見的使用對象是*.gz格式的檔案,這裡簡單介紹下它最常見的用法,

GZIP(1) General Commands Manual GZIP(1)

NAME

     gzip, gunzip, zcat - compress or expand files

SYNOPSIS

     gzip [ -acdfhklLnNrtvV19 ] [--rsyncable] [-S suffix] [ name ... ]

     gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ... ]

     zcat [ -fhLV ] [ name ... ]

OPTIONS

     -c --stdout --to-stdout 結果寫到标準輸出,原檔案保持不變

     -d --decompress --uncompress 解壓

     -k --keep 壓縮或者解壓過程中,保留原檔案

     -r --recursive

     -t --test 檢查壓縮檔案的完整性

     -v --verbose 顯示每個檔案的名子和壓縮率

     -# --fast --best 取值從-1(最快)到-9(最好),預設是-6

示例1,壓縮檔案

原檔案名為file1.txt,壓縮後原檔案消失,壓縮後檔案名為file1.txt.gz

root@ubuntu:/tmp# ls -l file1.*

-rw-r--r-- 1 root root 12383865 Aug 21 08:08 file1.txt

root@ubuntu:/tmp# gzip file1.txt

root@ubuntu:/tmp# ls -l file1.*

-rw-r--r-- 1 root root 134416 Aug 21 08:08 file1.txt.gz

示例2,解壓檔案

root@ubuntu:/tmp# gzip -d file1.txt.gz

root@ubuntu:/tmp# ls -lh file1.*

-rw-r--r-- 1 root root 12M Aug 21 08:08 file1.txt

示例3,壓縮的時候,顯示壓縮率

root@ubuntu:/tmp# gzip -v file1.txt

file1.txt: 98.9% -- replaced with file1.txt.gz

示例4,一條指令壓縮多個檔案,壓縮之後,是各自分開的:

root@ubuntu:/tmp# gzip file1.txt file2.txt

root@ubuntu:/tmp# ls -l

total 1348

-rw-r--r-- 1 root root 134416 Aug 21 08:08 file1.txt.gz

-rw-r--r-- 1 root root 392 Aug 21 08:15 file2.txt.gz

示例5,壓縮過程中,保留原檔案

root@ubuntu:/tmp# gzip -k file1.txt

root@ubuntu:/tmp# ls file1.*

file1.txt file1.txt.gz

示例6,壓縮到标準輸出中

可以連接配接兩個檔案

root@ubuntu:/tmp# cat file1.txt file2.txt | gzip > foo.gz

或者

root@ubuntu:/tmp# gzip -c file1.txt file2.txt > foo.gz

======================================

gzip

壓縮後的格式為:*.gz

這種壓縮方式不能儲存原檔案;且不能壓縮目錄

指令舉例:

#壓縮

[root@localhost tmp]# gzip buodo

[root@localhost tmp]# ls

buodo.gz

#解壓

[root@localhost tmp]# gunzip buodo.gz 

[root@localhost tmp]# ls

buodo

tar

指令選項:

    -z(gzip)      用gzip來壓縮/解壓縮檔案

    -j(bzip2)     用bzip2來壓縮/解壓縮檔案

    -v(verbose)   詳細報告tar處理的檔案資訊

    -c(create)    建立新的檔案檔案

    -x(extract)   解壓縮檔案或目錄

    -f(file)      使用檔案檔案或裝置,這個選項通常是必選的。

指令舉例:

#壓縮

[root@localhost tmp]# tar -zvcf buodo.tar.gz buodo

[root@localhost tmp]# tar -jvcf buodo.tar.bz2 buodo 

#解壓

[root@localhost tmp]# tar -zvxf buodo.tar.gz 

[root@localhost tmp]# tar -jvxf buodo.tar.bz2

zip

與gzip相比:1)可以壓縮目錄; 2)可以保留原檔案;

選項:

上一篇: Flex移動