文件的壓縮與打包

2021-08-20 21:39:26 字數 3658 閱讀 3123

linux下的壓縮檔案:

在linux下最常見的壓縮檔案通常都是以.tar.gz 為結尾的,除此之外還有.tar, .gz, .bz2, .zip等等。linux系統中的字尾名其實要不要無所謂,但是對於壓縮檔案來講必須要帶上。這是為了判斷壓縮檔案是由哪種壓縮工具所壓縮,而後才能去正確的解壓縮這個檔案。以下介紹常見的字尾名所對應的壓縮工具。

語法:gzip [-d#] filename 其中#為1-9的數字

「-d」 : 解壓縮時使用

「-#」 : 壓縮等級,1壓縮最差,9壓縮最好,6為預設

gzip 後面直接跟檔名,就在當前目錄下把該檔案壓縮了,而原檔案也會消失。

「gzip -d」 後面跟壓縮檔案,會解壓壓縮檔案。gzip 是不支援壓縮目錄的。

gzip其他具體用法如下:

圖:gzip具體用法

[root@localhost ~]# mkdir dir

[root@localhost ~]# ls

anaconda-ks.cfg dir downloads pictures templates

desktop documents music public videos

[root@localhost ~]# cd dir

[root@localhost dir]# ls

[root@localhost dir]# touch file.txt

[root@localhost dir]# ls

file.txt

[root@localhost dir]# gzip file.txt

[root@localhost dir]# ls

file.txt.gz

[root@localhost dir]# cd ../

[root@localhost ~]# gzip dir

gzip: dir is a directory -- ignored

[root@localhost ~]# cd dir

[root@localhost dir]# ls

file.txt.gz

[root@localhost dir]# gzip -d file.txt.gz

[root@localhost dir]# ls

file.txt

語法:bzip2 [-dz] filename

bzip2 只有兩個選項需要掌握。

「-d」 : 解壓縮

「-z」 : 壓縮。壓縮時,可以加 「-z」 也可以不加,都可以壓縮檔案,」-d」 則為解壓的選項:

bzip2 同樣也不可以壓縮目錄

bzip2其他具體用法如下:

圖:bzip2具體用法

tar 本身為乙個打包工具,可以把目錄打包成乙個檔案,它的好處是它把所有檔案整合成乙個大檔案整體,方便拷貝或者移動。

語法:tar [-zjxcvfpp] filename

「-z」 : 同時用gzip壓縮

「-j」 : 同時用bzip2壓縮

「-x」(extract) (提取) :解包或者解壓縮

「-t」(list) : 檢視tar包裡面的檔案,列出歸檔內容。

「-c」 (create): 建立乙個tar包或者壓縮檔案包

「-f」 : 後面跟檔名,壓縮時跟 「-f 檔名」,意思是壓縮後的檔名為filename, 解壓時跟 「-f 檔名」,意思是解壓filename. 請注意,如果是多個引數組合的情況下帶有 「-f」,請把 「-f」 寫到最後面。

「-p」 : 使用原檔案的屬性,壓縮前什麼屬性壓縮後還什麼屬性。(不常用)

「-p」 : 可以使用絕對路徑。(不常用)

--exclude filename: 在打包或者壓縮時,不要將filename檔案包括在內。(不常用)

其他用法可使用tar –help來檢視。

tar,打包還是解包,原來的檔案是不會刪除的,而且它會覆蓋當前已經存在的檔案或者目錄。

打包的同時使用gzip壓縮(常用):

tar命令非常好用的乙個功能就是可以在打包的時候直接壓縮,它支援gzip壓縮和bzip2壓縮。

「-cvzf」:可以在打包的同時使用gzip壓縮。

「-tf」 可以檢視包或者壓縮包的檔案列表.

「-zxvf」 用來解壓.tar.gz的壓縮包.

具體操作示例如下:

圖:打包的同時使用gzip壓縮和解壓縮

打包的同時使用bzip2壓縮:

和gzip壓縮不同的是,這裡使用 「-cjvf」 選項來壓縮。同樣可以使用 「-tf」 來檢視壓縮包檔案列表。

圖:使用bzip2來壓縮解壓檔案

–exclude的使用方法:

語法:tar -cvf dir.tar --exclude file.txt dir

請注意,dir.tar是放到了 –exclude 選項的前面。該選項除了可以排除檔案,也可以排除目錄。

[root@localhost ~]# ls

anaconda-ks.cfg dir downloads pictures templates

desktop documents music public videos

[root@localhost ~]# cd dir

[root@localhost dir]# ls

file.txt newdir

[root@localhost dir]# cd ../

[root@localhost ~]# tar -cvf dir.tar --exclude file.txt dir

dir/

dir/newdir/

[root@localhost ~]# ls

anaconda-ks.cfg dir documents music public videos

desktop dir.tar downloads pictures templates

[root@localhost ~]# tar -tf dir.tar

dir/

dir/newdir/

[root@localhost ~]#

文件的壓縮與打包

概述 跟阿銘學linux gz gzip 壓縮工具壓縮的檔案 bz2 bzip2 壓縮工具壓縮的檔案 tar tar 打包程式打包的檔案 tar.gz 可以理解為先用tar打包,然後再gzip壓縮 tar.bz2 先用tar打包,然後再bzip2壓縮 語法 gzip d 檔名 d 解壓縮時使用 壓縮...

linux文件壓縮與打包

ar zcvf home xahot.tar.gz xahot tar zcvf 打包後生成的檔名全路徑 要打包的目錄 例子 把 xahot資料夾打包後生成乙個 home xahot.tar.gz的檔案。tar xf all.tar 這條命令是解出all.tar包中所有檔案,x是解開的意思 zip ...

Linux文件壓縮與打包

常見的字尾名所對應的壓縮工具 linux裡常見的壓縮檔案通常是tar.gz格式。除此之外還有.tar gz bz2 zip等格式。gzip壓縮工具 gzip後面直接跟檔名,表示在當前目錄下壓縮該檔案,而原檔案也會消失。命令gzip d後面跟壓縮檔案表示解壓壓縮檔案。gzip不支援壓縮目錄,壓縮目錄時...