1. 程式人生 > >四周第四次課(1月5日) 6.1 壓縮打包介紹 6.2 gzip壓縮工具 6.3 bzip2壓縮工具 6.4 xz壓縮工具

四周第四次課(1月5日) 6.1 壓縮打包介紹 6.2 gzip壓縮工具 6.3 bzip2壓縮工具 6.4 xz壓縮工具

時間 force med 時間戳 dev iyu 文本文 是個 cups

四周第四次課(1月5日)
6.1 壓縮打包介紹
6.2 gzip壓縮工具
6.3 bzip2壓縮工具
6.4 xz壓縮工具


====================================================================================================================================================================================================================================================================================================================================================================================================================

=================================================


gzip命令:
用來壓縮文件。
gzip是個使用廣泛的壓縮程序,文件經它壓縮過後,其名稱後面會多處“.gz”擴展名。

gzip是在Linux系統中經常使用的一個對文件進行壓縮和解壓縮的命令,既方便又好用。gzip不僅可以用來壓縮大的、較少使用的文件以節省磁盤空間,還可以和tar命令一起構成Linux操作系統中比較流行的壓縮文件格式。據統計,gzip命令對文本文件有60%~70%的壓縮率。減少文件大小有兩個明顯的好處,一是可以減少存儲空間,二是通過網絡傳輸文件時,可以減少傳輸的時間。

語法
gzip(選項)(參數)
選項
-a或——ascii:使用ASCII文字模式;
-d或--decompress或----uncompress:解開壓縮文件;
-f或——force:強行壓縮文件。不理會文件名稱或硬連接是否存在以及該文件是否為符號連接;
-h或——help:在線幫助;
-l或——list:列出壓縮文件的相關信息;
-L或——license:顯示版本與版權信息;
-n或--no-name:壓縮文件時,不保存原來的文件名稱及時間戳記;
-N或——name:壓縮文件時,保存原來的文件名稱及時間戳記;
-q或——quiet:不顯示警告信息;
-r或——recursive:遞歸處理,將指定目錄下的所有文件及子目錄一並處理;


-S或<壓縮字尾字符串>或----suffix<壓縮字尾字符串>:更改壓縮字尾字符串;
-t或——test:測試壓縮文件是否正確無誤;
-v或——verbose:顯示指令執行過程;
-V或——version:顯示版本信息;
-<壓縮效率>:壓縮效率是一個介於1~9的數值,預設值為“6”,指定愈大的數值,壓縮效率就會愈高;
--best:此參數的效果和指定“-9”參數相同;
--fast:此參數的效果和指定“-1”參數相同。
參數
文件列表:指定要壓縮的文件列表。

實例
把test6目錄下的每個文件壓縮成.gz文件

gzip *
把上例中每個壓縮的文件解壓,並列出詳細的信息

gzip -dv *
詳細顯示例1中每個壓縮的文件的信息,並不解壓

gzip -l *
壓縮一個tar備份文件,此時壓縮文件的擴展名為.tar.gz

gzip -r log.tar
遞歸的壓縮目錄

gzip -rv test6
這樣,所有test下面的文件都變成了*.gz,目錄依然存在只是目錄裏面的文件相應變成了*.gz.這就是壓縮,和打包不同。因為是對目錄操作,所以需要加上-r選項,這樣也可以對子目錄進行遞歸了。

遞歸地解壓目錄

gzip -dr test6

=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================
bzip2命令:
用於創建和管理(包括解壓縮)“.bz2”格式的壓縮包。
我們遇見Linux壓縮打包方法有很多種,以下講解了Linux壓縮打包方法中的Linux bzip2命令的多種範例供大家查看,相信大家看完後會有很多收獲。

語法
bzip2(選項)(參數)
選項
-c或——stdout:將壓縮與解壓縮的結果送到標準輸出;
-d或——decompress:執行解壓縮;
-f或-force:bzip2在壓縮或解壓縮時,若輸出文件與現有文件同名,預設不會覆蓋現有文件。若要覆蓋。請使用此參數;
-h或——help:在線幫助;
-k或——keep:bzip2在壓縮或解壓縮後,會刪除原始文件。若要保留原始文件,請使用此參數;
-s或——small:降低程序執行時內存的使用量;
-t或——test:測試.bz2壓縮文件的完整性;
-v或——verbose:壓縮或解壓縮文件時,顯示詳細的信息;
-z或——compress:強制執行壓縮;
-V或——version:顯示版本信息;
--repetitive-best:若文件中有重復出現的資料時,可利用此參數提高壓縮效果;
--repetitive-fast:若文件中有重復出現的資料時,可利用此參數加快執行效果。
參數
文件:指定要壓縮的文件。

實例
壓縮指定文件filename:

bzip2 filename

bzip2 -z filename
這裏,壓縮的時候不會輸出,會將原來的文件filename給刪除,替換成filename.bz2.如果以前有filename.bz2則不會替換並提示錯誤(如果想要替換則指定-f選項,例如bzip2 -f filename;如果filename是目錄則也提醒錯誤不做任何操作;如果filename已經是壓過的了有bz2後綴就提醒一下,不再壓縮,沒有bz2後綴會再次壓縮。

解壓指定的文件filename.bz2:

bzip2 -d filename.bz2

bunzip2 filename.bz2
這裏,解壓的時候沒標準輸出,會將原來的文件filename.bz2給替換成filename。如果以前有filename則不會替換並提示錯誤(如果想要替換則指定-f選項,例如bzip2 -df filename.bz2。

壓縮解壓的時候將結果也輸出:

$bzip2 -v filename
輸入之後,輸出如下:

filename: 0.119:1, 67.200 bits/byte, -740.00% saved, 5 in, 42 out.
這裏,加上-v選項就會輸出了,只用壓縮舉例了,解壓的時候同理bzip2 -dv filename.bz2不再舉例了。

模擬解壓實際並不解壓:

bzip2 -tv filename.bz2
輸入之後,輸出如下:

filename.bz2: ok
這裏,-t指定要進行模擬解壓,不實際生成結果,也就是說類似檢查文件,當然就算目錄下面有filename也不會有什麽錯誤輸出了,因為它根本不會真的解壓文件。為了在屏幕上輸出,這裏加上-v選項了,如果是真的解壓bzip2 -dv filename.bz2則輸出的是把"ok"替換成了"done"。

壓縮解壓的時候,除了生成結果文件,將原來的文件也保存:

bzip2 -k filename
這裏,加上-k就保存原始的文件了,否則原始文件會被結果文件替代。只用壓縮舉例了,解壓的時候同理$bzip2 -dk filename.bz2不再舉例了。

解壓到標準輸出:

bzip2 -dc filename.bz2
輸入之後,輸出如下:

hahahhaahahha
這裏,使用-c指定到標準輸出,輸出的是文件filename的內容,不會將filename.bz2刪除。

壓縮到標準輸出:

bzip2 -c filename
bzip2: I won‘t write compressed data to a terminal.
bzip2: For help, type: `bzip2 --help‘.
這裏,使用-c指定壓縮到標準輸出不刪除原有文件,不同的是,壓縮後的文件無法輸出到標準輸出。

使用bzip2的時候將所有後面的看作文件(即使文件名以‘-‘開頭):

bzip2 -- -myfilename
這裏主要是為了防止文件名中-產生以為是選項的歧義。

=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================

KEYMAP="cn"
FONT="latarcyrheb-sun16"
LANG="zh_CN.UTF-8"
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"
[root@localhost d6z]# gzip -d 1.txt.gz
[root@localhost d6z]# gzip -c 1.txt > /tmp/11.txt.gz
[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# ls /tmp/11.txt.gz
/tmp/11.txt.gz
[root@localhost d6z]# file !$
file /tmp/11.txt.gz
/tmp/11.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Jan 5 21:32:55 2018
[root@localhost d6z]# gzip -d -c 1.txt > /tmp/11.txt.gz /tmp/d6z/2.txt

gzip: 1.txt: not in gzip format
gzip: /tmp/d6z/2.txt.gz: No such file or directory
[root@localhost d6z]# gzip -d -c 1.txt > /tmp/11.txt.gz > /tmp/d6z/2.txt

gzip: 1.txt: not in gzip format
[root@localhost d6z]# lsd
-bash: lsd: 未找到命令
[root@localhost d6z]# ls
1.txt 2.txt
[root@localhost d6z]# wc -l 1.txt 2.txt
123139 1.txt
0 2.txt
123139 總用量
[root@localhost d6z]# gzip -d -c /tmp/11.txt.gz > /tmp/d6z/2.txt

gzip: /tmp/11.txt.gz: unexpected end of file
[root@localhost d6z]# ls /tmp/11.txt.gz
/tmp/11.txt.gz
[root@localhost d6z]# gzip -d -c /tmp/11.txt.gz > /tmp/d6z/3.txt

gzip: /tmp/11.txt.gz: unexpected end of file
[root@localhost d6z]# ls
1.txt 2.txt 3.txt
[root@localhost d6z]# wc -l 1.txt 2.txt 3.txt
123139 1.txt
0 2.txt
0 3.txt
123139 總用量
[root@localhost d6z]# gzip -d -c /tmp/11.txt.gz > /tmp/d6z/3.txt

gzip: /tmp/11.txt.gz: unexpected end of file
[root@localhost d6z]# gzip -dc /tmp/11.txt.gz > /tmp/d6z/3.txt

gzip: /tmp/11.txt.gz: unexpected end of file
[root@localhost d6z]# ls /tmp/
11.txt.gz fstab
123 passwd.sh
12.txt systemd-private-7534822b871f401a97d8ffc1abd00603-vmtoolsd.service-28fmWV
1.txt systemd-private-fc86786af1c94a90a7ee58458ca44c06-vmtoolsd.service-NvU9XZ
d6z xxx.sh
[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz
[root@localhost d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/4.txt
[root@localhost d6z]# wc -l 1.txt 2.txt 3.txt 4.txt
123139 1.txt
0 2.txt
0 3.txt
123139 4.txt
246278 總用量
[root@localhost d6z]# ls /tmp/
11.txt.gz fstab
123 passwd.sh
12.txt systemd-private-7534822b871f401a97d8ffc1abd00603-vmtoolsd.service-28fmWV
1.txt systemd-private-fc86786af1c94a90a7ee58458ca44c06-vmtoolsd.service-NvU9XZ
1.txt.gz xxx.sh
d6z
[root@localhost d6z]# du -sh *.txt
4.7M 1.txt
0 2.txt
0 3.txt
4.7M 4.txt
[root@localhost d6z]# file !$
file *.txt
1.txt: C source, UTF-8 Unicode text, with very long lines
2.txt: empty
3.txt: empty
4.txt: C source, UTF-8 Unicode text, with very long lines
[root@localhost d6z]# file /tmp/11.txt.gz 1.txt.gz
/tmp/11.txt.gz: empty
1.txt.gz: cannot open (No such file or directory)
[root@localhost d6z]# file /tmp/11.txt.gz
/tmp/11.txt.gz: empty
[root@localhost d6z]# file /tmp/1.txt.gz
/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Jan 5 21:32:55 2018
[root@localhost d6z]# file /tmp/11.txt.gz
/tmp/11.txt.gz: empty
[root@localhost d6z]# ls
1.txt 2.txt 3.txt 4.txt
[root@localhost d6z]# yum install -y bzip2
已加載插件:fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 5.2 MB 00:00:03
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.aliyun.com
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 bzip2.x86_64.0.1.0.6-13.el7 將被 安裝
--> 解決依賴關系完成

依賴關系解決

============================================================================================
Package 架構 版本 源 大小
============================================================================================
正在安裝:
bzip2 x86_64 1.0.6-13.el7 base 52 k

事務概要
============================================================================================
安裝 1 軟件包

總下載量:52 k
安裝大小:82 k
Downloading packages:
bzip2-1.0.6-13.el7.x86_64.rpm | 52 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : bzip2-1.0.6-13.el7.x86_64 1/1
驗證中 : bzip2-1.0.6-13.el7.x86_64 1/1

已安裝:
bzip2.x86_64 0:1.0.6-13.el7

完畢!
[root@localhost d6z]# bzip2 1.txt
[root@localhost d6z]# ls
1.txt.bz2 2.txt 3.txt 4.txt
[root@localhost d6z]# du -sh 1.txt.bz2
496K 1.txt.bz2
[root@localhost d6z]# du -sh 4.txt
4.7M 4.txt
[root@localhost d6z]# bzip2 -d 1.txt.bz2
[root@localhost d6z]# bzip2 1.txt
[root@localhost d6z]# bunzip2 1.txt.bz2
[root@localhost d6z]# ls
1.txt 2.txt 3.txt 4.txt
[root@localhost d6z]# bzip2 -c 1.txt > /tmp/1.txt.bz2
[root@localhost d6z]# du -sh /tmp/1.txt.bz2
496K /tmp/1.txt.bz2
[root@localhost d6z]# bzip2 -d -c /tmp/1.txt.bz2 > 3.txt
[root@localhost d6z]# ls
1.txt 2.txt 3.txt 4.txt
[root@localhost d6z]# du -sh 3.txt
4.7M 3.txt
[root@localhost d6z]# bzip2 -9 1.txt
[root@localhost d6z]# du -sh 1.txt.bz2
496K 1.txt.bz2
[root@localhost d6z]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
[root@localhost d6z]# ls
1.txt.bz2 2.txt 3.txt 4.txt
[root@localhost d6z]# mv 1.txt.bz2 1.txt
[root@localhost d6z]# ls
1.txt 2.txt 3.txt 4.txt
[root@localhost d6z]# less 1.txt
"1.txt" may be a binary file. See it anyway?
[root@localhost d6z]# file 1.txt
1.txt: bzip2 compressed data, block size = 900k
[root@localhost d6z]# file 3.txt
3.txt: C source, UTF-8 Unicode text, with very long lines
[root@localhost d6z]# less 3.txt ^C
[root@localhost d6z]# mv 1.txt 1.txt.bz2
[root@localhost d6z]# ls
1.txt.bz2 2.txt 3.txt 4.txt
[root@localhost d6z]# bzcat
[root@localhost d6z]# xz 3.txt
[root@localhost d6z]# ls
1.txt.bz2 2.txt 3.txt.xz 4.txt
[root@localhost d6z]# du -sh 3.txt.xz
60K 3.txt.xz
[root@localhost d6z]# du -sh 4.txt
4.7M 4.txt
[root@localhost d6z]# xz -d 3.txt.xz
[root@localhost d6z]# ls
1.txt.bz2 2.txt 3.txt 4.txt
[root@localhost d6z]# xz 3.txt
[root@localhost d6z]# unxz 3.txt.xz
[root@localhost d6z]# ls
1.txt.bz2 2.txt 3.txt 4.txt
[root@localhost d6z]# xz -c 3.txt > /tmp/2.txt.xz
[root@localhost d6z]# xz -d -c /tmp/2.txt.xz > ./2.txt
[root@localhost d6z]# xzcat /tmp/2.txt.xz

=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================

技術分享圖片技術分享圖片技術分享圖片技術分享圖片技術分享圖片技術分享圖片技術分享圖片

=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================

四周第四次課(1月5日) 6.1 壓縮打包介紹 6.2 gzip壓縮工具 6.3 bzip2壓縮工具 6.4 xz壓縮工具