Linux gzip壓縮/解壓 *.gz文件詳解


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)可以保留原文件;

選項:

    -r(recursive)    遞歸壓縮目錄內的所有文件和目錄

命令舉例:
#壓縮和解壓文件
[root@localhost tmp]# zip boduo.zip boduo
[root@localhost tmp]# unzip boduo.zip

#壓縮和解壓目錄
[root@localhost tmp]# zip -r Demo.zip Demo
  adding: Demo/ (stored 0%)
  adding: Demo/Test2/ (stored 0%)
  adding: Demo/Test1/ (stored 0%)
  adding: Demo/Test1/test4 (stored 0%)
  adding: Demo/test3 (stored 0%)
[root@localhost tmp]# unzip Demo.zip 
Archive:  Demo.zip
   creating: Demo/
   creating: Demo/Test2/
   creating: Demo/Test1/
 extracting: Demo/Test1/test4        
 extracting: Demo/test3  

bzip2
壓縮后的格式:.bz2
參數
    -k    產生壓縮文件后保留原文件

命令舉例
#壓縮
[root@localhost tmp]# bzip2 boduo
[root@localhost tmp]# bzip2 -k boduo

#解壓
[root@localhost tmp]# bunzip2 boduo.bz2 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM