1. 程式人生 > >Linux 基礎教程 32-解壓縮命令

Linux 基礎教程 32-解壓縮命令

tree llb 文件夾 重要 最快 ext gem mod -a

? ? 將文件壓縮後對提升數據傳輸效率,降低傳輸帶寬,管理備份數據都有非常重要的功能,因此文件壓縮解壓技能就成為必備技能。相對於Windows中的文件解壓縮工具百花爭艷,在Linux中的解壓縮工具則要少很多,常用的解壓縮命令主要為gzipbzip2tarzip等等。

gzip命令

gzip只能針對普通文件進行壓縮和解壓,對於文件夾,符號鏈接等是不支持的。基本語法如下所示:

gzip [選項] [文件]

常用參數如下所示:

參數 說明
-d , --decompress 解壓文件
-f , --force 壓縮/解壓文件強制覆蓋已經存在的文件
-l , --list 顯示壓縮包的信息,如壓縮前大小、壓縮後大小、壓縮比例等
-r , --recursive 遞歸壓縮指定目錄中的所有文件和子目錄中的文件,將每個文件都壓縮為一個gz文件
-t , --test 檢查壓縮文件的完整性
-v , --verbose 顯示詳細過程
-V , --version 顯示版本信息

壓縮速度和壓縮強度

? ? 在壓縮文件時,可以根據實際需要采用不同的壓縮速度和壓縮強度,來調整壓縮的時間和壓縮比例。在gzip中共提供了9種壓縮級別。

  • -1:代表壓縮速度最快,但壓縮強度不高
  • -9:代表壓縮強度最高,但壓縮速度較慢
  • -6:gzip默認值

gzip示例如下所示:

  • 1、添加壓縮文件
[root@localhost Python-3.7.0]# du -sh Python-3.7.0
189M    Python-3.7.0
[root@localhost Python-3.7.0]# gzip -r Python-3.7.0
[root@localhost Python-3.7.0]# du -sh Python-3.7.0
64M     Python-3.7.0
[root@localhost Python-3.7.0]# ll
總用量 10856
-rw-rw-r--  1 root     root    3731 6月  27 11:07 aclocal.m4.gz
drwxr-xr-x  5 root     root      82 7月  20 12:43 build
-rw-rw-r--  1 root     root   13214 6月  27 11:07 config.guess.gz
-rw-r--r--  1 root     root   38624 7月  20 12:48 config.log.gz
  • 2、壓縮tar包
[root@localhost Python-3.7.0]# gzip Python-3.7.0.tar
[root@localhost Python-3.7.0]# ll -h
總用量 52M
-rw-r--r-- 1 root root 52M 7月  24 14:13 Python-3.7.0.tar.gz
  • 3、解壓壓縮包
gzip -d Python-3.7.0.tar.gz
  • 4、顯示壓縮包信息
[root@localhost Python-3.7.0]# gzip -l Python-3.7.0.tar.gz
         compressed        uncompressed  ratio uncompressed_name
           53670458           190464000  71.8% Python-3.7.0.tar

bzip2

bzip2與gzip非常類似,區別在於gzip壓縮和解壓均是同一個命令,而bzip2壓縮命令為bzip2,解壓縮命令為bunzip2

tar命令

在Linux中tar命令算是用得最多的命令了,基基本語法如下所示:

tar [選項] [文件]

其常用參數如下所示:

選項 說明
-A , --catenate 向壓縮包中添加壓縮包
-c, --create 新建壓縮包
-C, --directory=DIR 指定解壓縮目錄
-d, --diff 對比壓縮包與文件系統的差異
--delete 從壓縮包刪除指定的文件
-r, --append 添加文件末尾追加文件
-t, --list 顯示壓縮包中的目錄結構
-u, --update 僅向壓縮包中添加較新的文件
-x, --extract 解壓壓縮包
-f, --file=ARCHIVE 指定壓縮文件
-v, --verbose 顯示詳細過程
-j, --bzip2 支持bzip2
-z, --gzip 支持gzip
--overwrite 解壓時如果文件已經存在,則進行替換

使用tar需要註意的事項如下所示:

  • 1、常用的主選項參數如下:
-c -x -t -r -u
以上這5個參數同時只能出現一個,不能同時出現多個
  • 2、輔助選項
-f :一般情況需要將該參數放置在最後位置,後面緊跟文件名

tar 示例如下所示:

  • 1、添加壓縮文件到當前目錄
[root@localhost ~]# tar -cf Alltxt.tar *.txt
  • 2、查看壓縮包內容
[root@localhost ~]# tar -tf Alltxt.tar
in.txt
out.txt
packstack-answers-20180710-091950.txt
packstack-answers-20180710-092155.txt
packstack-answers-20180710-100538.txt
  • 3、向壓縮包中添加壓縮包
[root@localhost ~]# tar -cf Alltxt.tar *.txt
[root@localhost ~]# tar -cf Allbak.tar *.bak
[root@localhost ~]# tar -Af Alltxt.tar Allbak.tar
[root@localhost ~]# tar -tf Alltxt.tar
in.txt
out.txt
packstack-answers-20180710-091950.txt
packstack-answers-20180710-092155.txt
packstack-answers-20180710-100538.txt
append.txt.bak
  • 4、向壓縮包中添加文件
[root@localhost ~]# tar -rf Alltxt.tar out
[root@localhost ~]# tar -tf Alltxt.tar
in.txt
out.txt
packstack-answers-20180710-091950.txt
packstack-answers-20180710-092155.txt
packstack-answers-20180710-100538.txt
append.txt.bak
out/
out/out.txt
out/eip.sh
  • 5、更新壓縮包中的文件
[root@localhost ~]# tar -uf Alltxt.tar append.txt.bak
  • 6、解壓文件到指定目錄
[root@localhost ~]# tar -xf Python-3.7.0.tar  -C TarTest/
  • 7、解壓壓縮包中的指定文件
[root@localhost Python-3.7.0]# tar -xf Python-3.7.0.tar Python-3.7.0/pyconfig.h
[root@localhost Python-3.7.0]# tree Python-3.7.0
Python-3.7.0
└── pyconfig.h
0 directories, 1 file

zip命令

跟bzip2類似,zip用於壓縮文件,而unzip用於解壓縮文件。其基本語法如下所示:

zip [選項] [指定文件名] [壓縮文件或路徑]

uzip [選項] [壓縮包名稱]
  • zip常用參數如下所示:
選項 說明
-c , --entry-comments 給壓縮文件添加註釋
-d , --delete 從壓縮包刪除指定文件
-D 壓縮包不創建目錄名稱
-f , --freshen 與參數 -u 類似,不僅更新已有文件,而且也添加壓縮包沒有的文件
-i files/ --include files 僅向壓縮包添加指定的文件
-m , --move 將原始文件添加到壓縮包刪除原文件
-O output-file
-q , --quiet 靜默模式
-r , --recurse-paths 遞歸處理指定目錄和子目錄
-T , --test 檢查壓縮包的完整性
-u , --update 將較新的文件更新替換到壓縮包中
-v , --verbose 顯示詳細過程
-x files/--exclude files 壓縮文件時添加排除項
-# (-0~-9) 設置壓縮級別,-0:不壓縮文件,-1:最快壓縮速度,-9:最好壓縮強度,默認為-6
  • unzip常用參數
選項 說明
-l 顯示壓縮包中的內容
-t 檢查壓縮包的完整性
-o 強制覆蓋已存在的文件而不提示
-j 不處理壓縮文件中的原有目錄路徑
-d exdir 指定解壓目錄

zip/unzip示例如下所示:

  • 1、添加壓縮包
[root@localhost Python-3.7.0]# zip -r Python-3.7.0.zip Python-3.7.0
  • 2、解壓壓縮包
unzip -d /tmp/ Python-3.7.0.zip
  • 3、檢查壓縮包完整性
[root@localhost Python-3.7.0]# unzip -t Python-3.7.0.zip
Archive:  Python-3.7.0.zip
    testing: Python-3.7.0/            OK
    testing: Python-3.7.0/install-sh   OK
    ...
    testing: Python-3.7.0/pybuilddir.txt   OK
No errors detected in compressed data of Python-3.7.0.zip.
  • 4、顯示壓縮包內容
[root@localhost Python-3.7.0]# unzip -l Python-3.7.0.zip
Archive:  Python-3.7.0.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  07-20-2018 12:50   Python-3.7.0/
     7122  06-27-2018 11:07   Python-3.7.0/install-sh
   101855  06-27-2018 11:07   Python-3.7.0/setup.py
        0  07-20-2018 12:37   Python-3.7.0/.vsts/
 13965984  07-20-2018 12:50   Python-3.7.0/python
       26  07-20-2018 12:50   Python-3.7.0/pybuilddir.txt
---------                     -------
186791269                     4771 files
  • 5、刪除壓縮包中指定的文件
[root@localhost Python-3.7.0]# zip Python-3.7.0.zip -d Python-3.7.0/*.o
deleting: Python-3.7.0/Modules/config.o
deleting: Python-3.7.0/Modules/getpath.o
deleting: Python-3.7.0/Modules/main.o

本文同步在微信訂閱號上發布,如各位小夥伴們喜歡我的文章,也可以關註我的微信訂閱號:woaitest,或掃描下面的二維碼添加關註:
技術分享圖片

Linux 基礎教程 32-解壓縮命令