Linux壓縮打包
壓縮是一種通過特定的算法來減小計算機文件大小的機制。有利于文件在網絡上的傳輸,節約帶寬。在Linux中,壓縮是以后綴名區分文件的。(Linux中很少見)壓縮文件都會呈現醒目的紅色。
注意!以下壓縮軟件只能壓縮文件而不能壓縮目錄。不能對目錄直接壓縮!
compress命令
compress這個命令是非常老舊的一款命令,現在基本已經不用了 而且bzip已經可以解開compress壓縮格式。一般來說使用compress需要實現安裝ncompress包。這里就不做具體詳述了
compress [-dfvcVr] [-b maxbits] [file …]
-d: 解壓縮,相當于
-c: 結果輸出至標準輸出,不刪除原文件
-v: 顯示詳情
? uncompress 解壓縮
? zcat file.Z >file
gzip命令
gzip/gunzip/zcat是應用最廣的命令。目前gzip可以解開compress和gzip等軟件壓縮的文件,后綴名為FILE.gz。 gunzip為gzip的解壓命令,zcat是查看壓縮好的帶.gz后綴的壓縮包。cat查詢為亂碼!
語法格式
gzip [OPTION]… FILE …
常用參數
-d: 解壓縮,相當于gunzip
-c: 將壓縮或解壓縮的結果輸出至標準輸出
-#:1-9,指定壓縮比,值越大壓縮比越大。默認為6,數值越高壓縮比越高,但不推薦
舉例
[root@localhost tmp]# ll #原始文件 total 8 -rw-r--r-- 1 root root 679 Aug 24 10:14 fstab -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd [root@localhost tmp]# gzip fstab #壓縮fstab [root@localhost tmp]# ll total 8 -rw-r--r-- 1 root root 376 Aug 24 10:14 fstab.gz #原文件壓縮加后綴,大小改變 -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd [root@localhost tmp]# gzip -dc fstab.gz >file #解壓且保留壓縮文件,默認不保留 [root@localhost tmp]# ll #顯示解壓文杰和保留壓縮包 total 12 -rw-r--r-- 1 root root 679 Aug 24 10:14 file -rw-r--r-- 1 root root 376 Aug 24 10:14 fstab.gz -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd [root@localhost tmp]# compress file #用compress壓縮file文件 [root@localhost tmp]# ll total 12 -rw-r--r-- 1 root root 468 Aug 24 10:14 file.Z #compress壓縮文件,后綴為.Z -rw-r--r-- 1 root root 376 Aug 24 10:14 fstab.gz -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd [root@localhost tmp]# gunzip file.Z #使用gzip解壓compress壓縮文件 [root@localhost tmp]# ll total 12 -rw-r--r-- 1 root root 679 Aug 24 10:14 file #解壓成功 -rw-r--r-- 1 root root 376 Aug 24 10:14 fstab.gz -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd
bzip2命令
bzip/bunzip2/bzcat是為了取代gzip并提供更高的壓縮比而來的。它的用法和gzip幾乎完全相同。后綴名為.bz2
語法格式
bzip2 [OPTION]… FILE …
常用選項
-k: keep, 保留原文件
-d:解壓縮
-#:1-9,壓縮比,默認為6,
bzcat:不顯式解壓縮的前提下查看文本文件內容;
示例
[root@localhost tmp]# ls #源文件 file fstab passwd [root@localhost tmp]# bzip2 passwd #壓縮passwd文件后會在原文件加.bz2完成壓縮 [root@localhost tmp]# ls #顯示 file fstab passwd.bz2 [root@localhost tmp]# bzip2 -k fstab #壓縮并保留源文件 [root@localhost tmp]# ls file fstab fstab.bz2 passwd.bz2 [root@localhost tmp]# ll total 16 -rw-r--r-- 1 root root 679 Aug 24 10:14 file -rw-r--r-- 1 root root 679 Aug 24 10:14 fstab -rw-r--r-- 1 root root 410 Aug 24 10:14 fstab.bz2 #bzip2壓縮文件 -rw-r--r-- 1 root root 995 Aug 24 10:05 passwd.bz2#bzip2壓縮文件 [root@localhost tmp]# bzip2 -d passwd.bz2 #解壓文件 [root@localhost tmp]# ll total 16 -rw-r--r-- 1 root root 679 Aug 24 10:14 file -rw-r--r-- 1 root root 679 Aug 24 10:14 fstab -rw-r--r-- 1 root root 410 Aug 24 10:14 fstab.bz2 -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd #解壓成普通文件
xz命令
xz/unxz/zcat是比較新的壓縮軟件。但它比bzip2的壓縮比更好。后綴名為.xz。
語法
xz [OPTION]…FILE…
常用選項
-d;解壓縮
-#;指定壓縮比;默認是6;數字越大壓縮比越大(1-9);
-k; 保留原文件
示例
[root@localhost tmp]# ls #測試文件 file fstab fstab.bz2 fstab.gz passwd [root@localhost tmp]# xz fstab 使用xz壓縮fstab文件 [root@localhost tmp]# ll total 20 -rw-r--r-- 1 root root 679 Aug 24 10:14 file -rw-r--r-- 1 root root 436 Aug 24 10:14 fstab.xz #xz壓縮文件 -rw-r--r-- 1 root root 2535 Aug 24 10:05 passwd
zip命令
zip壓縮是使用最廣的,不論是windows還是like-unxi系統都有使用它。后綴名為.zip,用法和以上壓縮軟件都是類型我就不多做介紹了。
tar命令
tar是一個打包命令,可以將多個文件打包成一個文件,同時它也支持gzip/bzip2/xz壓縮等。
語法
tar [OPTION…] [FILE]…
常用選項
-j:支持bzip2解壓文件;
-v:顯示操作過程;
-t或–list:列出備份文件的內容;
-z或–gzip或–ungzip:通過gzip指令處理備份文件;
-Z或–compress或–uncompress:通過compress指令處理備份文件;
-f<備份文件>或–file=<備份文件>:指定備份文件;
-v或–verbose:顯示指令執行過程;
-C <目錄>:這個選項用在解壓縮,若要在特定目錄解壓縮,可以使用這個選項。
-J, –xz :支持xz解壓文件
-x或–extract或–get:從備份文件中還原文件;
-x或–extract或–get:從備份文件中還原文件;
特別注意
#在參數 f 之后的文件檔名是自己取的,我們習慣上都用 .tar 來作為辨識。
# 如果加 z 參數,則以 .tar.gz 或 .tar.gz 來代表 gzip 壓縮過的 tar file ~
# 如果加 j 參數,則以 .tar.bz2 來作為附檔名啊~
常用組合選項
tar -cf /PATH/TO/SOMEFILE.tar FILE…. #創建歸檔,不壓縮
tar -tvf /PATH/TO/SOMEFILE.tar FILE…. #查看歸檔中的文件列表
tar -xf /PATH/FROM/SOMEFILE.tar#解包,.tar包扔存在
tar -xf /PATH/FROM/SOMEFILE.tar -C /PATH/TO/SOMEDIR #解包至指定目錄下。
示例
[root@localhost tmp]# ll #原文件 total 15004 -rw-r--r-- 1 root root 595 Aug 24 11:30 fstab -rw------- 1 root root 15353251 Aug 24 11:30 messages -rw-r--r-- 1 root root 2535 Aug 24 11:30 passwd [root@localhost tmp]# tar cf fstab.tar fstab#創建歸檔文件fstab.tar(注意.tar需要自己手動加后綴好方便識別) [root@localhost tmp]# tar cf all.tar fstab messages passwd#把三個文件打包為all.tar [root@localhost tmp]# ll total 30016 -rw-r--r-- 1 root root 15360000 Aug 24 11:33 all.tar -rw-r--r-- 1 root root 595 Aug 24 11:30 fstab -rw-r--r-- 1 root root 10240 Aug 24 11:33 fstab.tar -rw------- 1 root root 15353251 Aug 24 11:30 messages -rw-r--r-- 1 root root 2535 Aug 24 11:30 passwd [root@localhost tmp]# tar tvf all.tar #查看all.tar文件列表 -rw-r--r-- root/root 595 2016-08-24 11:30 fstab -rw------- root/root 15353251 2016-08-24 11:30 messages -rw-r--r-- root/root 2535 2016-08-24 11:30 passwd [root@localhost tmp]# tar ztvf passwd.tar.gz #查看包內文件列表 -rw-r--r-- root/root 2535 2016-08-24 11:30 passwd [root@localhost tmp]# tar zxvf passwd.tar.gz #用z解壓.gz后綴的文件 passwd [root@localhost tmp]# ll #顯示結果 total 30020 -rw-r--r-- 1 root root 15360000 Aug 24 11:33 all.tar -rw-r--r-- 1 root root 595 Aug 24 11:30 fstab -rw-r--r-- 1 root root 10240 Aug 24 11:33 fstab.tar -rw------- 1 root root 15353251 Aug 24 11:30 messages -rw-r--r-- 1 root root 2535 Aug 24 11:30 passwd -rw-r--r-- 1 root root 1073 Aug 24 11:37 passwd.tar.gz [root@localhost tmp]# tar -zxvf passwd.tar.gz -C /testdir/ #指定壓縮的文件路徑 passwd [root@localhost tmp]# ll /testdir/passwd #查看結果 -rw-r--r-- 1 root root 2535 Aug 24 11:30 /testdir/passwd
cpio命令
cpio命令是通過重定向的方式將文件進行打包備份,還原恢復的工具,它可以解壓以“.cpio”或者“.tar”結尾的文件。
語法
? cpio [選項] > 文件名或者設備名
? cpio [選項] < 文件名或者設備名
常用選項
-o 將文件拷貝打包成文件或者將文件輸出到設備上
-i 解包,將打包文件解壓或將設備上的備份還原到系統
-t 預覽,查看文件內容或者輸出到設備上的文件內容
-v 顯示打包過程中的文件名稱。
-d 解包生成目錄,在cpio還原時,自動的建立目錄
-c 一種較新的存儲方式
示例
? 將etc目錄備份:
find /etc -print |cpio -ov >etc.cpio
[root@localhost tmp]# ll total 49768 -rw-r--r-- 1 root root 15360000 Aug 24 11:33 all.tar -rw-r--r-- 1 root root 20218368 Aug 24 13:21 etc.cpio #etc備份包 -rw-r--r-- 1 root root 595 Aug 24 11:30 fstab -rw-r--r-- 1 root root 10240 Aug 24 11:33 fstab.tar -rw------- 1 root root 15353251 Aug 24 11:30 messages -rw-r--r-- 1 root root 2535 Aug 24 11:30 passwd -rw-r--r-- 1 root root 1073 Aug 24 11:37 passwd.tar.gz
? 內容預覽
cpio –tv < etc.cpio
? 要解包文件
cpio –iv <etc.cpio
cpio –idv < etc.cpio
原創文章,作者:ladsdm,如若轉載,請注明出處:http://www.www58058.com/39476