我的十大IT傑出博客參賽主頁是:http://2010blog.51cto.com/1060257,請各位支持我,給我投上寶貴一票,謝謝!
隨著Linux開源系統平台的不斷發展,越來越多的開源軟體可以提供給Linux用戶使用,這樣,也是的更多的文件“吞噬”著硬碟空間.作為一種優秀的開源操作系統,如何高效地管理系統中的軟體是一個很重要的問題.Linux提供了多種方法,用戶可以根據實際情況方便地對軟體進行管理.比如,它提供了對文件打包的功能,用戶可以使用其將若干文件或目錄打成一個軟體包;同時,它也提供了多種文件壓縮工具,是的用戶可以對某些文件進行壓縮,以減小文件佔用的硬碟空間或方便網路傳輸.本文將通過例子來介紹如何合理地利用這些方法來管理Linux系統中的軟體.(更多全文請見:http://os.51cto.com/art/201011/235682.htm)
zip程序位於/usr/bin目錄中,可將文件壓縮成.zip文件以節省硬碟空間,而當需要的時候又可將壓縮文件解開.unzip命令用於將壓縮文件解壓.
(1)用zip命令壓縮文件或文件夾
在Linux下輸入man zip,系統顯示zip的幫助文檔.
zip命令的使用形式為:zip [選項] 壓縮後文件名 待壓縮文件或文件夾.
其中:參數選項表明要完成的操作類型,壓縮后的文件名是某個合法的文件名,其後綴為zip,待壓縮文件或文件夾指明須要進行壓縮的文件或文件夾,可以是多個文件或文件夾.zip命令的參數選項如下:
注意:使用zip命令可以將許多文件壓縮成一個文件,這與gzip是有區別的.
下面給出使用該命令壓縮文件或文件夾的例子:
//顯示當前目錄下所有文件和文件夾 #ll 總用量 9 drwx------ 1 root root 4096 10月 15 19:45 gzip drwx------ 1 root root 0 10月 12 21:25 smart -rwx------ 1 root root 26 10月 13 22:51 smart.txt -rwx------ 1 root root 226 10月 13 22:51 tar_create.txt -rwx------ 1 root root 7433 10月 13 22:51 tar.txt drwx------ 1 root root 4096 10月 13 22:13 xplns //將當前目錄下的所有文件和文件夾全部壓縮成gong.zip壓縮文件,-r表示遞歸壓縮子目錄下所有文件 # zip -r gong.zip ./* adding: gzip/ (stored 0%) adding: gzip/gong.tar.gz (stored 0%) adding: smart/ (stored 0%) adding: smart/smartsuite-2.1-2.i386.rpm (deflated 5%) adding: smart.txt (stored 0%) adding: tar_create.txt (deflated 44%) adding: tar.txt (deflated 68%) adding: xplns/ (stored 0%) adding: xplns/xplns-cat-3.3.1-1.i386.rpm.gz (deflated 0%) adding: xplns/xplns-elm-3.3.1-1.i386.rpm.gz (deflated 0%) adding: xplns/xplns-img-3.3.1-1.i386.rpm.gz (deflated 0%) //再顯示當前目錄下所有文件,可以發現壓縮文件gong.zip已經生成 #ll 總用量 1621 -rwx------1 root root 3301222 10月 15 19:49 gong.zip drwx------1 root root 4096 10月 15 19:45 gzip drwx------1 root root 0 10月 12 21:25 smart -rwx------1 root root 26 10月 13 22:51 smart.txt -rwx------1 root root 226 10月 13 22:51 tar_create.txt -rwx------1 root root 7433 10月 13 22:51 tar.txt drwx------1 root root 4096 10月 13 22:13 xplns
用帶-v參數選項可以查看zip文件的內容,這點類似於帶t參數選項的tar命令.只不過前者用於顯示壓縮的zip文件的內容,後者用於顯示tar文件的內容.與帶t參數選項的tar命令類似,帶v參數選項的zip命令實際上並不解壓縮文件.用帶-v參數選項的zip命令舉例如下:
//顯示當前目錄下所有文件,從顯示結果可以看出,當前目錄下只有一個gong.zip文件 # ll 總用量 1612 -rwx------ 1 root root 3301222 10月 15 19:49 gong.zip //查看gong.zip文件的內容 # zip -v gong.zip zip info: local extra (21 bytes) != central extra (13 bytes): gzip/ zip info: local extra (21 bytes) != central extra (13 bytes): gzip/gong.tar.gz zip info: local extra (21 bytes) != central extra (13 bytes): smart/ zip info: local extra (21 bytes) != central extra (13 bytes): smart.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt zip info: local extra (21 bytes) != central extra (13 bytes): xplns/
用帶-d參數選項的zip命令可以從zip壓縮文件中刪除某個文件,而使用帶-m的zip命令可以向zip壓縮文件添加某個文件,對帶-d和-m參數選項的zip命令舉例如下:
# zip -v gong.zip //顯示壓縮文件gong.zip的文件內容 zip info: local extra (21 bytes) != central extra (13 bytes): free.txt zip info: local extra (21 bytes) != central extra (13 bytes): smart.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt //刪除壓縮文件中smart.txt文件 # zip -d gong.zip smart.txt deleting: smart.txt //再顯示壓縮文件內容,可以發現文件刪除成功 # zip -v gong.zip zip info: local extra (21 bytes) != central extra (13 bytes): free.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt //向壓縮文件中gong.zip中添加rpm_info.txt文件 # zip -m gong.zip ./rpm_info.txt adding: rpm_info.txt (deflated 75%) //再顯示壓縮文件內容,可以發現文件添加成功 # zip -v gong.zip zip info: local extra (21 bytes) != central extra (13 bytes): free.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt zip info: local extra (21 bytes) != central extra (13 bytes): rpm_info.txt
(2)用unzip命令解壓縮文件
unzip命令用於擴展名為zip的壓縮文件的解壓縮,同時,Windows下用壓縮軟體Winzip壓縮的文件在Linux系統下也可以用unzip命令解壓縮.unzip命令的語法如下:
unzip [參數選項] 壓縮文件名.zip.
所有參數選項如下:
unzip命令的常用用法有:
簡單解壓縮文件:將壓縮文件解壓縮到當前目錄下.
//顯示當前目錄下所有文件,從顯示結果可以發現當前目錄下只有一個文件gong.zip # ll 總用量 7 -rwx------ 1 root root 13512 10月 15 20:44 gong.zip //將gong.zip解壓縮到當前目錄 # unzip gong.zip Archive: gong.zip inflating: free.txt inflating: tar_create.txt inflating: tar.txt inflating: rpm_info.txt //再顯示當前目錄下所有文件,從顯示結果可以發現,gong.zip壓縮文件中所有文件已經成功解壓縮到當前目錄 # ll 總用量 32 -rwx------ 1 root root 230 10月 15 21:03 free.txt -rwx------ 1 root root 13512 10月 15 20:44 gong.zip -rwx------ 1 root root 40833 10月 15 21:03 rpm_info.txt -rwx------ 1 root root 226 10月 15 21:03 tar_create.txt -rwx------ 1 root root 7433 10月 15 21:03 tar.txt
解壓縮文件到指定目錄:將壓縮文件解壓縮到指定的目錄下,如果已有相同的文件存在,unzip命令不覆蓋原來的文件.
//解壓縮當前目錄下的text.zip壓縮文件,-n參數選項指明不覆蓋原有文件,-d /tmp指明將文件解壓縮到/tmp目錄下 #unzip -n text.zip -d /tmp //查看壓縮文件目錄:類似於帶-v參數的zip命令,只顯示壓縮文件內容,並不實際解壓縮文件. #unzip -v text.zip //顯示當前目錄下text.zip壓縮文件內容3、bzip2命令
bzip2是一個無損壓縮軟體,具有高質量的數據壓縮能力.它能將文件壓縮到原來的10%到15%,壓縮速度和解壓縮速度都非常高,因此在Linux下得到廣泛使用.
bzip2從命令行讀入文件名和參數,每個文件都被名為“原始文件名.bz2”的壓縮文件替換.每個壓縮文件具有與原文件相同的修改時間和許可權,如果可能的話,還具有相同的屬主,因此在解壓縮時這些特性將正確地恢復.
(1)bzip2命令參數選項
- -c:將數據壓縮或解壓縮至標準輸出.
- -d:強制解壓縮.bzip2、bunzip2和bzcat實際上是同一個程序,進行何種操作將根據程序名確定.指定該選項后將不考慮這一機制,強制bzip2解壓縮.
- -z:和-d選項類似,強制進行壓縮操作,而不管輸入的是哪個程序名.
- -t:檢查指定文件的完整性,但並不對其解壓縮.實際上將對數據進行實驗性的解壓縮操作,而不輸出結果.
- -f:強制覆蓋輸出文件.通常bzip2不會覆蓋已經存在的文件.該選項還強制bzip2打破文件的硬鏈接,默認情況下bzip2不會這麼做.
- -k:在壓縮或解壓縮時保留輸入文件(不刪除這些文件).
- ? -s:在壓縮、解壓縮及檢查時減少內存用量.採用一種修正的演算法進行壓縮和測試,每個數據塊僅需要2.5個位元組.這意味著任何文件都可以在2300k的內存中進行解壓縮,儘管速度只有通常情況下的一半.在壓縮時,-s將選定200k的塊長度,內存用量也限制在200k左右,代價是壓縮率會降低.總之,如果機器的內存較少(8兆位元組或更少),可對所有操作都採用-s選項.
- -q:壓制不重要的警告信息.屬於I/O錯誤及其他嚴重事件的信息將不會被壓制.
- -v:詳盡模式--顯示每個被處理文件的壓縮率.命令行中更多的-v選項將增加詳細的程度,使bzip2顯示出許多主要用於診斷目的信息.
- -L:顯示軟體版本,許可證條款及條件.
- -1to-9:在壓縮時將塊長度設為100k、200k.、900k.對解壓縮沒有影響.
(2)用bzip2命令壓縮文件
//顯示當前目錄下所有文件 # ll 總用量 32 -rwx------ 1 root root 230 10月 15 22:06 free.txt -rwx------ 1 root root 13512 10月 15 22:06 gong.zip -rwx------ 1 root root 40833 10月 15 22:06 rpm_info.txt -rwx------ 1 root root 226 10月 15 22:06 tar_create.txt -rwx------ 1 root root 7433 10月 15 22:06 tar.txt //用bzip2命令壓縮當前目錄下所有文件,用bunzip2 -z * 將獲得相同的效果 #bzip2 * //再顯示當前目錄下所有文件,從顯示結果可以看出,所有文件都壓縮成了原文件名加bz2後綴的形式 # ll 總用量 15 -rwx------ 1 root root 157 10月 15 22:06 free.txt.bz2 -rwx------ 1 root root 13824 10月 15 22:06 gong.zip.bz2 -rwx------ 1 root root 9366 10月 15 22:06 rpm_info.txt.bz2 -rwx------ 1 root root 165 10月 15 22:06 tar_create.txt.bz2 -rwx------ 1 root root 2326 10月 15 22:06 tar.txt.bz2(3)用bunzip2命令解壓縮文件
//顯示當前目錄下所有文件 # ll 總用量 15 -rwx------ 1 root root 230 10月 15 22:17 free.txt -rwx------ 1 root root 13824 10月 15 22:17 gong.zip.bz2 -rwx------ 1 root root 9366 10月 15 22:17 rpm_info.txt.bz2 -rwx------ 1 root root 165 10月 15 22:17 tar_create.txt.bz2 -rwx------ 1 root root 2326 10月 15 22:17 tar.txt.bz2 //用bunzip2命令解壓縮當前目錄下所有文件,其中有一個文件free.txt不是以bz2為後綴 //從顯示結果可以看出,對free.txt文件解壓縮時報錯 # bunzip2 * bunzip2: Can't guess original name for free.txt -- using free.txt.out bunzip2: free.txt is not a bzip2 file. //再顯示當前目錄下所有文件,從顯示結果可以看出,所有bz2文件都解壓縮成功 # ll 總用量 32 -rwx------ 1 root root 230 10月 15 22:17 free.txt -rwx------ 1 root root 13512 10月 15 22:18 gong.zip -rwx------ 1 root root 40833 10月 15 22:18 rpm_info.txt -rwx------ 1 root root 226 10月 15 22:18 tar_create.txt -rwx------ 1 root root 7433 10月 15 22:18 tar.txt
本文出自 「卓越始於足下」 博客,謝絕轉載!
[火星人 ] 手把手教你Linux下的文件管理(二)已經有636次圍觀