1、linux下壓縮、解壓以.gz結尾檔案
壓縮檔案
指令格式:gzip [參數] [檔案或者目錄]
指令功能:壓縮檔案壓縮後的檔案名以".gz"為擴充名
指令參數:
-c或--stdout或--to-stdout 把壓縮後的檔案輸出到标準輸出裝置不去更動原始檔案。即保留源檔案
-l或--list 列出壓縮檔案的相關資訊。
-n或--no-name 壓縮檔案時不儲存原來的檔案名稱及時間戳記。
-N或--name 壓縮檔案時儲存原來的檔案名稱及時間戳記。
-q或--quiet 不顯示警告資訊。
-t或--test 測試壓縮檔案是否正确無誤。
-r或--recursive 遞歸處理将指定目錄下的所有檔案及子目錄一并處理。
-v或--verbose 顯示指令執行過程。
-V或--version 顯示版本資訊。
使用執行個體:
1
2
3
4
5
6
7
8
9
<code>[root@localhost </code><code>case</code><code>]</code><code># ls</code>
<code>gzip</code> <code>gzip1 gzip2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># gzip gzip</code>
<code>gzip1[root@localhost </code><code>case</code><code>]</code><code># ls</code>
<code>gzip1 gzip2 </code><code>gzip</code><code>.gz</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># gzip -c gzip1 > gzio1.gz</code>
<code>gzio1.gz gzip1 gzip2 </code><code>gzip</code><code>.gz</code>
<code>[root@localhost </code><code>case</code><code>]</code><code>#</code>
解壓檔案
指令格式:gunzip [參數] [檔案或者目錄]
指令功能:解壓檔案名以".gz"為擴充名的檔案
-c或--stdout或--to-stdout把解壓後的檔案輸出到标準輸出裝置。
-l或--list列出壓縮檔案的相關資訊。
-t或--test測試壓縮檔案是否正确無誤。
<code>gzio1.gz </code><code>gzip</code><code>.gz</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># gunzip gzio1.gz </code>
<code>[root@localhost </code><code>case</code><code>]</code><code># gunzip -c gzip.gz > gzip</code>
<code>gzio1 </code><code>gzip</code> <code>gzip</code><code>.gz</code>
2、linux下壓縮、解壓以.zip結尾檔案
指令格式:zip [參數] [壓縮後的檔案名] [壓縮檔案的路徑]
指令功能:壓縮檔案壓縮後的檔案名以".zip"為擴充名
-b<工作目錄> 指定暫時存放檔案的目錄
-d 從壓縮檔案内删除指定的檔案。
-D 壓縮檔案内不建立目錄名稱。
-F 嘗試修複已損壞的壓縮檔案。
-m 将檔案壓縮并加入壓縮檔案後删除原始檔案即把檔案移到壓縮檔案中。
-r 遞歸處理将指定目錄下的所有檔案和子目錄一并處理。
-q 安靜模式在壓縮的時候不顯示指令的執行過程
10
11
12
13
14
<code>[root@localhost </code><code>case</code><code>]</code><code># touch zip1 zip2 </code>
<code>[root@localhost </code><code>case</code><code>]</code><code># mkdir zipdir</code>
<code>zip1 zip2 zipdir</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># zip zip1.zip zip1 </code>
<code> </code><code>adding: zip1 (stored 0%)</code>
<code>zip1 zip1.zip zip2 zipdir</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># zip -r zip.zip zip2 zipdir/</code>
<code> </code><code>adding: zip2 (stored 0%)</code>
<code> </code><code>adding: zipdir/ (stored 0%)</code>
<code>zip1 zip1.zip zip2 zipdir zip.zip</code>
指令格式:unzip [參數] [檔案]
指令功能:解壓檔案名以".zip"為擴充名的檔案
-l 顯示壓縮檔案内所包含的檔案
-C 壓縮檔案中的檔案名稱區分大小寫。
-n 解壓縮時不要覆寫原有的檔案。
-o 不必先詢問使用者unzip執行後覆寫原有檔案。
<code>zip1.zip zip.zip</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># unzip zip.zip </code>
<code>Archive: zip.zip</code>
<code> </code><code>extracting: zip2 </code>
<code> </code><code>creating: zipdir/</code>
<code>zip1.zip zip2 zipdir zip.zip</code>
3、linux下壓縮、解壓以.bz2結尾檔案
指令格式:bzip2 [參數] [檔案]
指令功能:bzip2壓縮完檔案後會産生.bz2的壓縮檔案并删除原始的檔案。
-k或--keep bzip2在壓縮或解壓縮後會删除原始的檔案。若要保留原始檔案請使用此參數。
-f或--force bzip2在壓縮或解壓縮時若輸出檔案與現有檔案同名預設不會覆寫現有檔案。若要覆寫請使用此參數。
-t或--test 測試.bz2壓縮檔案的完整性。
-z或--compress 強制執行壓縮。
-d或--decompress 執行解壓縮。
-壓縮等級 壓縮時的區塊大小與 gzip 同樣的都是在計算壓縮比的參數 -9 最佳 -1 最快
-c或--stdout 将壓縮與解壓縮的結果送到标準輸出。
<code>bzip1 </code><code>bzip2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># bzip2 bzip1 </code>
<code>bzip1.bz2 </code><code>bzip2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># bzip2 -k bzip2 </code>
<code>bzip1.bz2 </code><code>bzip2</code> <code>bzip2</code><code>.bz2</code>
指令格式:bunzip2 [參數] [檔案]
指令功能:解壓檔案名以".bz2"為擴充名的檔案
-f或--force 解壓縮時若輸出的檔案與現有檔案同名時預設不會覆寫現有的檔案。若要覆寫請使用此參數。
-k或--keep 在解壓縮後預設會删除原來的壓縮檔案。若要保留壓縮檔案請使用此參數。
-s或--small 降低程式執行時記憶體的使用量。
-v或--verbose 解壓縮檔案時顯示詳細的資訊。
-l,--license,-V或--version 顯示版本資訊。
<code>bzip1.bz2 </code><code>bzip2</code><code>.bz2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># bunzip2 bzip1.bz2 </code>
<code>bzip1 </code><code>bzip2</code><code>.bz2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># bunzip2 -k bzip2.bz2 </code>
<code>bzip1 </code><code>bzip2</code> <code>bzip2</code><code>.bz2</code>
4、linux下壓縮、解壓以.tar.*結尾檔案
.tar.*結尾的檔案是先用tar打包,再調用相應的壓縮程式進行壓縮,linux中常見的有兩種,*.tar.gz和*.tar.bz2就屬于這種先打包再壓縮的檔案
打包指令tar
指令格式:tar [參數] [檔案、目錄清單]
指令功能:對檔案、目錄進行打包備份
-c :建立一個新的壓縮檔案(create 的意思)
-x :解開一個壓縮檔案
-C [目錄] :解壓檔案到某一指定目錄
-t :檢視 tarfile 裡面的檔案,特别注意,在參數的下達中, c/x/t 僅能存在一個!不可同時存在,因為不可能同時壓縮與解壓縮。
-z :是否同時具有 gzip 的屬性?即調用gzip進行壓縮
-j :是否同時具有 bzip2 的屬性?即調用bzip2進行壓縮
-v :壓縮的過程中顯示檔案!這個常用,但不建議用在背景執行過程!
-f :使用檔名,請留意,在 f 之後要立即接檔名!不要再加參數!
例如使用『 tar -zcvfP tfile sfile』就是錯誤的寫法,要寫成
『 tar -zcvPf tfile sfile』才對喔!
-p :使用原檔案的原來屬性(屬性不會依據使用者而變)
-P :可以使用絕對路徑來壓縮!
-N :比後面接的日期(yyyy/mm/dd)還要新的才會被打包進建立的檔案中!
--exclude FILE:在壓縮的過程中,不要将 FILE 打包!多個檔案寫多個exclude,最好用相對路徑
檔案路徑要寫成--exclude=queryweb/logs,不能寫成--exclude=queryweb/logs/格式,不然無法排除log目錄
--remove-files 壓縮後删除源檔案
<code>[root@localhost remote]</code><code># tar -zcvf 2016-12-29.tar.gz *.log --remove-files</code>
打包
15
16
17
18
19
<code>[root@localhost </code><code>case</code><code>]</code><code># touch file1 file2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># mkdir dir1 dir2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># cp file1 file2 dir1</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># cp file2 dir2</code>
<code>dir1 dir2 file1 file2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -cvf file1.tar file1</code>
<code>file1</code>
<code>dir1 dir2 file1 file1.</code><code>tar</code> <code>file2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -cvf dir1.tar dir1 dir2</code>
<code>dir1/</code>
<code>dir1</code><code>/file2</code>
<code>dir1</code><code>/file1</code>
<code>dir2/</code>
<code>dir2</code><code>/file2</code>
<code>dir1 dir1.</code><code>tar</code> <code>dir2 file1 file1.</code><code>tar</code> <code>file2</code>
檢視包内包含的檔案
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -tvf dir1.tar </code>
<code>drwxr-xr-x root</code><code>/root</code> <code>0 2014-09-06 05:45 dir1/</code>
<code>-rw-r--r-- root</code><code>/root</code> <code>0 2014-09-06 05:45 dir1</code><code>/file2</code>
<code>-rw-r--r-- root</code><code>/root</code> <code>0 2014-09-06 05:45 dir1</code><code>/file1</code>
<code>drwxr-xr-x root</code><code>/root</code> <code>0 2014-09-06 05:45 dir2/</code>
<code>-rw-r--r-- root</code><code>/root</code> <code>0 2014-09-06 05:45 dir2</code><code>/file2</code>
打包時排除某一檔案
<code>[root@localhost </code><code>case</code><code>]</code><code># tar --exclude=file1 -cvf dirfile2.tar dir1</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -tvf dirfile2.tar </code>
<code>[root@localhost </code><code>case</code><code>]</code><code># ll dir1</code>
<code>total 0</code>
<code>-rw-r--r-- 1 root root 0 Sep 6 05:45 file1</code>
<code>-rw-r--r-- 1 root root 0 Sep 6 05:45 file2</code>
<code>[root@localhost ~]</code><code># tar cvf ProxyServer1.tar.gz ProxyServer --exclude=ProxyServer/lib</code>
<code>[root@localhost ~]</code><code># tar cvf ProxyServer2.tar.gz ProxyServer --exclude=ProxyServer/derby.log</code>
<code>[root@localhost ~]</code><code>#</code>
解包
<code>dir1.</code><code>tar</code> <code>dirfile2.</code><code>tar</code> <code>file1.</code><code>tar</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -xvf dir1.tar </code>
<code>dir1 dir1.</code><code>tar</code> <code>dir2 dirfile2.</code><code>tar</code> <code>file1.</code><code>tar</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># mkdir dir3</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -xvf dirfile2.tar -C dir3/</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># ll dir3/</code>
<code>total 4</code>
<code>drwxr-xr-x 2 root root 4096 Sep 6 05:45 dir1</code>
打包、壓縮成.tar.gz結尾的檔案
<code>file</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -zcvf file.tar.gz file</code>
<code>file</code> <code>file</code><code>.</code><code>tar</code><code>.gz</code>
解壓.tar.gz結尾的檔案
<code>file</code><code>.</code><code>tar</code><code>.gz</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -zxvf file.tar.gz </code>
解壓tar包中某個檔案
<code>[root@localhost src]</code><code># tar zxvpf queryweb20170327.tar.gz queryweb/webapps/commodity</code>
這裡的queryweb/webapps/commodity這個路徑是壓縮包裡的路徑,可以通過tar -tf 來列出來的路徑,前面不要再加一個“/”,否則就報錯了
打包、壓縮成.tar.bz2結尾的檔案
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -jcvf file.tar.bz2 file</code>
<code>file</code> <code>file</code><code>.</code><code>tar</code><code>.bz2</code>
解壓.tar.bz2結尾的檔案
<code>file</code><code>.</code><code>tar</code><code>.bz2</code>
<code>[root@localhost </code><code>case</code><code>]</code><code># tar -jxvf file.tar.bz2 </code>
檢視檔案的大小
<code>[root@localhost </code><code>case</code><code>]</code><code># du -h file.tar.bz2 </code>
<code>4.0K </code><code>file</code><code>.</code><code>tar</code><code>.bz2</code>
本文轉自 justin_peng 51CTO部落格,原文連結:http://blog.51cto.com/ityunwei2017/1316788,如需轉載請自行聯系原作者