1.壓縮與解壓縮:
壓縮與解壓縮常用的方式有gz, bz2, xz, zip,下面分別介紹一下這幾種方式:
(1)gzip:
該命令可以將文件壓縮爲 gz 格式:
[root@localhost~]# gzip file.log
壓縮後,當前目錄下的 file.log文件會消失,壓縮後的文件名爲file.log.gz 。
壓縮時可以手動指定壓縮級別,壓縮級別從 1 到 9,數字越大壓縮比例越高,默認的壓縮級別爲 6 ,一般使用默認壓縮級別即可,使用如下方式指定壓縮級別:
[root@localhost~]# gzip -1 file.log
使用 -d 參數可以將 gz 格式的壓縮文件解壓縮:
[root@localhost~]# gzip -d file.log.gz
也可以使用 gunzip命令解壓縮,效果相同:
[root@localhost~]# gunzip file.log.gz
解壓縮後,當前目錄下的 file.log.gz 文件會消失,解壓縮後的文件名爲 file.log 。
如果想指定解壓縮後文件的存放路徑和文件名,需要使用 -c 參數,結合輸出重定向來完成:
[root@localhost~]# gzip -d file.log.gz -c > /tmp/newfile.log
通過這種方式解壓縮,不僅可以指定解壓縮後文件的存放路徑和解壓縮後的文件名,同時不會影響file.log.gz 文件,即解壓完成後原壓縮文件依然存在,
可以在一條命令中對多個文件進行壓縮和解壓縮:
[root@localhost~]# gzip file1.log file2.log //壓縮
[root@localhost~]# gzip -d file1.log.gz file2.log.gz //壓縮
這裏的壓縮不是將兩個文件壓縮爲一個文件,而是分別壓縮,即壓縮後爲兩個文件 file1.log.gz 和 file2.log.gz
如果想查看一個 gz 壓縮文件中的內容,使用zcat 命令:
[root@localhost~]# zcat file.log.gz
注:gzip 壓縮並不能將目錄中的所有文件壓縮爲一個,只能使用 -r 參數對目錄中的文件分別壓縮:
[root@localhost~]# gzip -r newfolder/
這樣在newfolder/ 目錄下的所有文件都會被單獨壓縮成 gz 格式。
使用 -l 參數,可以查看 gz 壓縮文件的壓縮比例等情況:
[root@localhost~]# gzip -l file.gz
compressed uncompressed ratio uncompressed_name
47 19 -10.5% file
(2)bzip2:
和 gzip 用法幾乎相同,對應的解壓縮命令爲bunzip2 。區別是壓縮的比例略有不同,相應的參數也少了,沒有-l 、 -r 參數,但依然可以使用 -c 參數。
如果想查看一個 bz2壓縮文件中的內容,使用bzcat 命令:
[root@localhost~]# bzcat file.log.bz2
bzip2 有一個獨有的參數 -k ,使用該參數也能夠在壓縮後保留原文件:
[root@localhost~]# bzip2 -k file.log
[root@localhost~]# ls
[root@localhost~]# …… file.log …… file.log.bz2 ……
(3)xz:
和 gzip 用法幾乎相同,沒有對應的解壓縮命令,解壓縮時使用-d 參數即可。區別是壓縮的比例略有不同,相應的參數也少了,沒有 -r 、-c 參數,-l 參數基本不使用。
(4)zip:
[root@localhost~]# zip /tmp/learn/file.log.zip file.log
相對於前三種壓縮方式來說, zip 方式有些特殊。首先,在 minimal 安裝時不會安裝zip 和 unzip 命令,需要通過yum 命令安裝:
[root@localhost~]# yum install –y zip unzip
其次,zip 命令可以用來壓縮目錄,需要使用–r 參數:
[root@localhost~]# zip –r etc.zip /etc/
再次,zip 命令可以直接指定壓縮後文件的存放目錄和名稱,不必藉助其他參數,且壓縮 / 解壓縮後原文件依然存在,不受影響(不必像 gzip 和 bzip2 命令一樣,想保留原文件還需要使用 -c 結合輸出重定向)。
最後,zip 命令可以將多個文件壓縮爲一個文件:
[root@localhost~]# zip twofile.zip file1 file2
2.打包與解包:
打包不像壓縮一樣會明顯減小文件所佔用的空間,打包的意義是方便文件管理和傳輸。使用 tar 命令進行打包與解包。
將 file1file2 file3 三個文件打包爲 file.tar 文件:
[root@localhost~]# tar -cvf file.tar file1 file2 file3
如果在打包時想排除一些文件和目錄,需要使用 --exclude 參數:
[root@localhost ~]# tar -cvf lucy.tar --exclude “.sh” --exclude“.i586” --exclude “init3.d” /etc
注:此處 --exclude後直接跟要排除的文件和目錄,不用使用等號形式,即 --exclude=”.sh” 。
將 file.tar 文件解包,默認會解包到當前目錄,如果想指定解包後文件的目錄,使用-C 參數:
[root@localhost~]# tar -xvf file.tar //解包到當前目錄
[root@localhost ~]#tar -xvf file.tar -C /tmp/ //目錄必須存在,不會自動創建目錄
查看打包文件中都包含哪些文件:
[root@localhost ~]# tar -tf test.tar
test1.txt
test2.txt
如果想查看打包文件中所包含文件的詳細信息,需要多使用一個 -v 參數:
[root@localhost ~]# tar –tvf test.tar
-rw-r--r-- root/root 1982015-03-28 10:36 test1.txt
-rw-r--r-- root/root 1660322015-03-28 09:08 test2.txt
刪除打包文件中的已有文件:
[root@localhost~]# tar --delete test1.txt -f test.tar
更新打包文件中的文件:
[root@localhost~]# tar -uvf test.tar test1.txt
執行上條命令後,無論此時 test.tar 文件中是否包含 test1.txt 文件,都會將其添加進 test.tar 文件中,如果已含有 test1.tar ,查看 test.tar:
[root@localhost~]# tar -tf test.tar
test1.txt
test1.txt
test2.txt
不難看出,更新後打包文件中出現了兩個 test1.tar ,但在解壓時,新更新進去的文件會覆蓋掉原有的同名文件。
3.打包與壓縮結合使用:
這是在實際應用中最常用的一種方式。可以與 tar 結合使用的壓縮格式有 gz 、 bz2 、 xz ,差別不大,具體來看一下。
2.1 tar 與 gz 結合:
(1)打包 + 壓縮:
[root@localhost ~]# tar -zcvf test.tar.gz test1.txt test2.txt
(2)解包 + 解壓縮:
[root@localhost ~]# tar -zxvf test.tar.gz //可以使用 -C 參數
2.2 tar 與 bz2 結合:
(1)打包 + 壓縮:
[root@localhost ~]# tar -jcvf test.tar.gz test1.txt test2.txt
(2)解包 + 解壓縮:
[root@localhost ~]# tar -jxvf test.tar.gz //可以使用 -C 參數
2.3 tar 與 xz 結合:
(1)打包 + 壓縮:
[root@localhost ~]# tar -Jcvf test.tar.gz test1.txt test2.txt
(2)解包 + 解壓縮:
[root@localhost ~]# tar -Jxvf test.tar.gz //可以使用 -C 參數
打包和壓縮的內容可以結合之前的另一篇博客——《權當開篇——Linux學習之路(二)》——一塊學習,下面給出博客鏈接:
http://xitongjiagoushi.blog.51cto.com/9975742/1620232
本文出自 “細桶假狗屎” 博客,請務必保留此出處http://xitongjiagoushi.blog.51cto.com/9975742/1627128