文檔壓縮、解壓縮集合

前言

在windows下我們接觸最多的壓縮文件就是.rar格式的了。但在linux下這樣的格式是不能識別的,它有自己所特有的壓縮工具。但有一種文件在windows和linux下都能使用那就是.zip格式的文件了。壓縮的好處不用介紹相信你也曉得吧,它不僅能節省磁盤空間而且在傳輸的時候還能節省網絡帶寬呢。

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

.gz gzip 壓縮工具壓縮的文件

.bz2 bzip2 壓縮工具壓縮的文件

.tar tar 打包程序打包的文件(tar並沒有壓縮功能,只是把一個目錄合併成一個文件)

.tar.gz 可以理解爲先用tar打包,然後再gzip壓縮

.tar.bz2 同上,先用tar打包,然後再bzip2壓縮

gzip壓縮工具

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

“-d” : 解壓縮時使用

“-#” : 壓縮等級,1壓縮最差,9壓縮最好,6爲默認

[root@localhost ~]# [ -d test ] && rm -rf test
[root@localhost ~]# mkdir test
[root@localhost ~]# mv test.txt test
[root@localhost ~]# cd test
[root@localhost test]# ls
test.txt
[root@localhost test]# gzip test.txt
[root@localhost test]# ls
test.txt.gz

你對第一條命令也許很陌生,其實這是兩條命令,

  • 內是一個判斷,”-d test” 判斷test目錄是否存在,
  • ’&&’ 爲一個連接命令符號,當前面的命令執行成功後,後面的命令才執行。
  • gzip 後面直接跟文件名,就在當前目錄下把該文件壓縮了,而原文件也會消失。
[root@localhost test]# gzip -d test.txt.gz
[root@localhost test]# ls
test.txt

“gzip -d” 後面跟壓縮文件,會解壓壓縮文件。gzip 是不支持壓縮目錄的。

[root@localhost ~]# gzip test
gzip: test is a directory -- ignored
[root@localhost ~]# ls test
test/  test1  test2/ test3
[root@localhost ~]# ls test
test.txt

至於 “-#” 選項,平時很少用,使用默認壓縮級別足夠了。

bzip2壓縮工具

語法: bzip2 [-dz] filename

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

“-d” : 解壓縮

“-z” : 壓縮

壓縮時,可以加 “-z” 也可以不加,都可以壓縮文件,”-d” 則爲解壓的選項:

[root@localhost ~]# cd test
[root@localhost test]# bzip2 test.txt
[root@localhost test]# ls
test.txt.bz2
[root@localhost test]# bzip2 -d test.txt.bz2
[root@localhost test]# bzip2 -z test.txt
[root@localhost test]# ls
test.txt.bz2

bzip2 同樣也不可以壓縮目錄:

[root@localhost test]# cd ..
[root@localhost ~]# bzip2 test
bzip2: Input file test is a directory.

tar壓縮工具

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

語法:tar [-zjxcvfpP] filename tar 命令有多個選項

tar cvf hbs.tar /etc/mnt

​ 打包名稱 打包對象

“-z” : 同時用gzip壓縮

“-j” : 同時用bzip2壓縮

“-x” : 解包或者解壓縮

“-t” : 查看tar包裏面的文件

“-c” : 建立一個tar包或者壓縮文件包

“-v” : 可視化

“-f” : 後面跟文件名,壓縮時跟 “-f 文件名”,意思是壓縮後的文件名爲filename, 解壓時跟 “-f 文件名”,意思是解壓filename. 請注意,如果是多個參數組合的情況下帶有 “-f”,請把 “-f” 寫到最後面。

“-p” : 使用原文件的屬性,壓縮前什麼屬性壓縮後還什麼屬性。(不常用)

“-P” : 可以使用絕對路徑。(不常用)

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

[root@localhost ~]# cd test
[root@localhost test]# mkdir test111
[root@localhost test]# touch test111/test2.txt
[root@localhost test]# echo "nihao" > !$
echo "nihao" > test111/test2.txt
[root@localhost test]# ls
test111  test.txt.bz2
[root@localhost test]# tar -cvf test111.tar test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111  test111.tar  test.txt.bz2

首先在test目錄下建立test111目錄,然後在test111目錄下建立test2.txt, 並寫入 “nihao” 到test2.txt中,接着是用tar把test111打包成test111.tar. 請記住 “-f” 參數後跟的是打包後的文件名, 然後再是要打包的目錄或者文件。tar 打包後,原文件不會消失,而依舊存在。在上例中使用一個特殊的符號

  • ”!$” 它表示上一條命令的最後一個參數,比如在本例中,它表示”test111/test2.txt”. tar 不僅可以打包目錄也可以打包文件,打包的時候也可以不加 “-v” 選項表示,表示不可視化。
[root@localhost test]# rm -f test111.tar
[root@localhost test]# tar -cf test.tar test111 test.txt.bz2
[root@localhost test]# ls
test111  test.tar  test.txt.bz2

刪除原來的test111目錄,然後解包test.tar,不管是打包還是解包,原來的文件是不會刪除的,而且它會覆蓋當前已經存在的文件或者目錄。

[root@localhost test]# rm -rf test111
[root@localhost test]# ls
test.tar  test.txt.bz2
[root@localhost test]# tar -xvf test.tar
test111/
test111/test2.txt
test.txt.bz2

打包的同時使用gzip壓縮

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

[root@localhost test]# tar -czvf  test111.tar.gz test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111  test111.tar.gz   test.tar  test.txt.bz2

“-tf” 可以查看包或者壓縮包的文件列表:

[root@localhost test]# tar -tf test111.tar.gz
test111/
test111/test2.txt
[root@localhost test]# tar -tf test.tar
test111/
test111/test2.txt
test.txt.bz2

“-zxvf” 用來解壓.tar.gz的壓縮包

[root@localhost test]# rm -rf test111
[root@localhost test]# ls
test111.tar.gz  test.tar  test.txt.bz2
[root@localhost test]# tar -zxvf test111.tar.gz
test111/
test111/test2.txt
[root@localhost test]# ls
test111  test111.tar.gz  test.tar  test.txt.bz2

打包的同時使用bzip2壓縮

和gzip壓縮不同的是,這裏使用 “-cjvf” 選項來壓縮

[root@localhost test]# tar -cjvf test111.tar.bz2 test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111  test111.tar.bz2  test111.tar.gz  test.tar  test.txt.bz2

同樣可以使用 “-tf” 來查看壓縮包文件列表

[root@localhost test]# tar -tf test111.tar.bz2
test111/
test111/test2.txt

解壓.tar.bz2 的壓縮包也很簡單

[root@localhost test]# tar -jxvf test111.tar.bz2
test111/
test111/test2.txt

下面介紹一下 --exclude 這個選項的使用,因爲在日常的管理工作中你也許會用到它。

[root@localhost test]# tar -cvf test111.tar --exclude test3.txt test111
test111/
test111/test4.txt
test111/test2.txt
test111/test5/

請注意上條命令中,test111.tar 是放到了 --exclude 選項的前面。除了可以排除文件,也可以排除目錄:

[root@localhost test]# rm -f test111.tar
[root@localhost test]# tar -cvf test111.tar --exclude test5 test111
test111/
test111/test4.txt
test111/test3.txt
test111/test2.txt

總結

  • tar.gz 包

    tar -czvf newfile.tar.gz /wyh 壓縮/wyh 目錄,名字爲newfile.tar.gz

    tar zxvf etc.tar.gz 解壓

  • tar.bz2 包

    tar jcvf newfile.tar.bz2 /wyh 壓縮

    tar jxvf etc.tar.bz2 解壓

  • tar.xz 包

    tar Jcvf hbs.tar.xz 壓縮

    tar xvf hbs.tar.xz 解壓

  • zip包

    zip hsb.zip /wyh /a 壓縮文件

    zip -r hbs,zip /boot/grup 壓縮目錄

    unzip grub.zip 解壓

    unzip grup.zip -d /opt 解壓到/opt 裏面

常用的是tar.gz 、tar.bz2

常見壓縮工具:

​ gzip 、bzip2、zip、xz、tar

常見壓縮格式

.gz .bz2 xz zip
zcf jcf Jcf zip
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章