【CentOS 7筆記26】,打包和壓縮工具的使用#171108

hellopasswdlinux


常見的壓縮文件

  • windows系統中,常見的壓縮文件有 .rar,.zip,.7z
  • linux系統中,常見的壓縮文件有 .zip,.gz, .bz2,.xz,.tar.gz,.tar.bz2,.tar.xz

使用壓縮工具壓縮文件有兩個好處,一是能夠減小存儲空間,二是減小傳輸時間windows

gzip壓縮工具

經常使用

-f/--force	#強制壓縮。不理會是否存在符號連接
-l/--list	#列出壓縮文件的相關信息
-n/--no-name	#壓縮文件時,不保存原來文件名稱和時間
-N/--name	#壓縮文件時,保存原來文件名稱和時間
-q/--quiet	#不顯示警告信息
-r/--recursive	#遞歸
-v/--verbose	#顯示執行過程
-數字	#壓縮效率介於1~9的數值,預設值爲6,指定值越大,壓縮效率越高

示例

[root@localhost ~]# mkdir 111
[root@localhost ~]# cd 111
[root@localhost 111]# du -sh 1.txt 
	224K	1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;	#查找/etc/下*.conf後綴的文件內容追加到當前的1.txt文件裏
[root@localhost 111]# ls
	1.txt
[root@localhost 111]# du -sh 1.txt 
	448K	1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt 
	704K	1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt 
	1.2M	1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt		#空隙
	1.2M	1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt 
	2.2M	1.txt

這是在寫數據的時候,若是數據小於4K,當再次寫入數據的時候,不是從這個塊裏面寫的,而是另起一個塊,大於4K就寫兩個塊以上。因此,就會出現容量空隙。其實就是塊空間沒有寫滿網絡

[root@localhost 111]# gzip 1.txt 
[root@localhost 111]# ls
	1.txt.gz
[root@localhost 111]# du -sh 1.txt.gz 
	356K	1.txt.gz
[root@localhost 111]# gzip -d 1.txt.gz 
[root@localhost 111]# ls
	1.txt
[root@localhost 111]# gzip 1.txt
[root@localhost 111]# gunzip 1.txt.gz 
[root@localhost 111]# ls
	1.txt

[root@localhost 111]# du -sh 1.txt.gz	#默認壓縮級別爲6
	356K	1.txt.gz
[root@localhost 111]# file 1.txt.gz
	1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Wed Nov  8 11:36:58 2017
[root@localhost 111]# gzip -d 1.txt.gz 

[root@localhost 111]# gzip -1 1.txt 
[root@localhost 111]# du -sh 1.txt.gz	#使用壓縮級別1對比默認壓縮
	420K	1.txt.gz
[root@localhost 111]# file 1.txt.gz 
	1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Wed Nov  8 11:36:58 2017, max speed
[root@localhost 111]# gzip -d 1.txt.gz 

[root@localhost 111]# gzip -9 1.txt	#最高壓縮級別爲9,當壓縮限度超過上限後,級別再高也不會發生變化
[root@localhost 111]# du -sh 1.txt.gz 
	356K	1.txt.gz
[root@localhost 111]# file 1.txt.gz 
	1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Wed Nov  8 11:36:58 2017, max compression
[root@localhost 111]# gzip -d 1.txt.gz 


[root@localhost 111]# zip -c 1.txt > 2.txt.gz	#使用-c能夠使壓縮或解壓的結果傳送到標準輸出
[root@localhost 111]# ls
	1.txt  2.txt.gz
[root@localhost 111]# zip -d -c 2.txt.gz > 2.txt
[root@localhost 111]# ls
	1.txt  2.txt  2.txt.gz

[root@localhost 111]# zcat 1.txt.gz	#使用zcat能夠在不解壓狀況下,查看文件內容
	#內容

3. 其餘

bzip2壓縮工具

安裝bzip2

[root@localhost 111]# yum install -y bzip2
	Loaded plugins: fastestmirror
	base                                                                        | 3.6 kB  00:00:00     
	extras                                                                      | 3.4 kB  00:00:00     
	updates                                                                     | 3.4 kB  00:00:00     
	Loading mirror speeds from cached hostfile
	 * base: mirrors.btte.net
	 * extras: mirrors.btte.net
	 * updates: mirrors.btte.net
	Resolving Dependencies
	--> Running transaction check
	---> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed
	--> Processing Dependency: bzip2-libs = 1.0.6-13.el7 for package: bzip2-1.0.6-13.el7.x86_64
	--> Running transaction check
	---> Package bzip2-libs.x86_64 0:1.0.6-12.el7 will be updated
	---> Package bzip2-libs.x86_64 0:1.0.6-13.el7 will be an update
	--> Finished Dependency Resolution
	
	Dependencies Resolved
	
	===================================================================================================
	 Package                  Arch                 Version                    Repository          Size
	===================================================================================================
	Installing:
	 bzip2                    x86_64               1.0.6-13.el7               base                52 k
	Updating for dependencies:
	 bzip2-libs               x86_64               1.0.6-13.el7               base                40 k
	
	Transaction Summary
	===================================================================================================
	Install  1 Package
	Upgrade             ( 1 Dependent package)

	Total download size: 92 k
	Downloading packages:
	Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
	(1/2): bzip2-libs-1.0.6-13.el7.x86_64.rpm                                   |  40 kB  00:00:00     
	(2/2): bzip2-1.0.6-13.el7.x86_64.rpm                                        |  52 kB  00:00:01     
	---------------------------------------------------------------------------------------------------
	Total                                                               65 kB/s |  92 kB  00:00:01     
	Running transaction check
	Running transaction test
	Transaction test succeeded
	Running transaction
	  Updating   : bzip2-libs-1.0.6-13.el7.x86_64                                                  1/3 
	  Installing : bzip2-1.0.6-13.el7.x86_64                                                       2/3 
	  Cleanup    : bzip2-libs-1.0.6-12.el7.x86_64                                                  3/3 
	  Verifying  : bzip2-libs-1.0.6-13.el7.x86_64                                                  1/3 
	  Verifying  : bzip2-1.0.6-13.el7.x86_64                                                       2/3 
	  Verifying  : bzip2-libs-1.0.6-12.el7.x86_64                                                  3/3 

	Installed:
	  bzip2.x86_64 0:1.0.6-13.el7                                                                      

	Dependency Updated:
	  bzip2-libs.x86_64 0:1.0.6-13.el7                                                                 

	Complete!

經常使用

-f/--force	#強制壓縮。不理會是否存在符號連接
-l/--list	#列出壓縮文件的相關信息
-n/--no-name	#壓縮文件時,不保存原來文件名稱和時間
-N/--name	#壓縮文件時,保存原來文件名稱和時間
-q/--quiet	#不顯示警告信息
-r/--recursive	#遞歸
-v/--verbose	#顯示執行過程
-數字	#壓縮效率介於1~9的數值,預設值爲6,指定值越大,壓縮效率越高

示例

[root@localhost 111]# bzip2 2.txt 
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2
[root@localhost 111]# du -sh 2.txt.bz2 
	132K	2.txt.bz2
[root@localhost 111]# file 2.txt.bz2 	#bzip2默認壓縮級別爲9
	2.txt.bz2: bzip2 compressed data, block size = 900k

[root@localhost 111]# bzip2 -d -c 2.txt.bz2 > 3.txt
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt
[root@localhost 111]# bunzip2 2.txt.bz2 
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt  3.txt

[root@localhost 111]# bzip2 -1 2.txt	#使用壓縮級別1與默認的壓縮級別對比
[root@localhost 111]# du -sh 2.txt.bz2 
	284K	2.txt.bz2
[root@localhost 111]# file 2.txt.bz2
	2.txt.bz2: bzip2 compressed data, block size = 100k

[root@localhost 111]# bzcat 2.txt.bz2	#bzip2在不壓縮的狀況下查看文件內容
	#內容

[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt
[root@localhost 111]# du -sh 1.txt.gz 
	300K	1.txt.gz
[root@localhost 111]# du -sh 2.txt.bz2 
	284K	2.txt.bz2

xz壓縮工具

經常使用

-f/--force	#強制壓縮。不理會是否存在符號連接
-l/--list	#列出壓縮文件的相關信息
-n/--no-name	#壓縮文件時,不保存原來文件名稱和時間
-N/--name	#壓縮文件時,保存原來文件名稱和時間
-q/--quiet	#不顯示警告信息
-r/--recursive	#遞歸
-v/--verbose	#顯示執行過程
-數字	#壓縮效率介於1~9的數值,預設值爲6,指定值越大,壓縮效率越高

示例

[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt
[root@localhost 111]# xz 3.txt		#使用xz壓縮比大,但耗時也大,默認壓縮級別爲6
[root@localhost 111]# du 3.txt.xz 
	56K	3.txt.xz
[root@localhost 111]# xz -d 3.txt.xz 

[root@localhost 111]# xz -c 3.txt > 3.txt.xz
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt  3.txt.xz
[root@localhost 111]# unxz -c 3.txt.xz > 4.txt	#使用unxz跟xz -d是同樣的
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt  3.txt.xz  4.txt

[root@localhost 111]# xzcat 3.txt.xz	#xz的顯示內容方式
	#內容

[root@localhost 111]# du -sh 1.txt.gz 
	300K	1.txt.gz
[root@localhost 111]# du -sh 2.txt.bz2 
	284K	2.txt.bz2
[root@localhost 111]# du -sh 3.txt.xz 
	56K	3.txt.xz

zip壓縮工具

使用zip支持壓縮目錄app

安裝zip壓縮工具

[root@localhost ~]# yum install -y zip
	Loaded plugins: fastestmirror
	Loading mirror speeds from cached hostfile
	 * base: mirrors.btte.net
	 * extras: mirrors.btte.net
	 * updates: mirrors.btte.net
	Resolving Dependencies
	--> Running transaction check
	---> Package zip.x86_64 0:3.0-11.el7 will be installed
	--> Finished Dependency Resolution

	Dependencies Resolved

	===================================================================================================
	 Package             Arch                   Version                     Repository            Size
	===================================================================================================
	Installing:
	 zip                 x86_64                 3.0-11.el7                  base                 260 k
	
	Transaction Summary
	===================================================================================================
	Install  1 Package

	Total download size: 260 k
	Installed size: 796 k
	Downloading packages:
	zip-3.0-11.el7.x86_64.rpm                                                   | 260 kB  00:00:04     
	Running transaction check
	Running transaction test
	Transaction test succeeded
	Running transaction
	  Installing : zip-3.0-11.el7.x86_64                                                           1/1 
	  Verifying  : zip-3.0-11.el7.x86_64                                                           1/1 

	Installed:
	  zip.x86_64 0:3.0-11.el7                                                                          

	Complete!

[root@localhost ~]# zip 1.txt.zip 1.txt
[root@localhost ~]# zip -d 1.txt 1.txt.zip

[root@localhost ~]# zip -r 111.txt.zip 1.txt 111

安裝zip解壓工具

[root@localhost ~]# yum install -y unzip
	Loaded plugins: fastestmirror
	Loading mirror speeds from cached hostfile
	 * base: mirrors.btte.net
	 * extras: mirrors.aliyun.com
	 * updates: mirrors.btte.net
	Resolving Dependencies
	--> Running transaction check
	---> Package unzip.x86_64 0:6.0-16.el7 will be installed
	--> Finished Dependency Resolution
	
	Dependencies Resolved
	
	===================================================================================================
	 Package              Arch                  Version                      Repository           Size
	===================================================================================================
	Installing:
	 unzip                x86_64                6.0-16.el7                   base                169 k
	
	Transaction Summary
	===================================================================================================
	Install  1 Package
	
	Total download size: 169 k
	Installed size: 365 k
	Downloading packages:
	unzip-6.0-16.el7.x86_64.rpm                                                 | 169 kB  00:00:01     
	Running transaction check
	Running transaction test
	Transaction test succeeded
	Running transaction
	  Installing : unzip-6.0-16.el7.x86_64                                                         1/1 
	  Verifying  : unzip-6.0-16.el7.x86_64                                                         1/1 
	
	Installed:
	  unzip.x86_64 0:6.0-16.el7                                                                        
	
	Complete!

示例

  1. 使用zip壓縮文件
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt  3.txt.xz  4.txt
[root@localhost 111]# zip 4.txt.zip 4.txt 
	  adding: 4.txt (deflated 73%)
[root@localhost 111]# du -sh 4.txt.zip 
	300K	4.txt.zip
  1. 解壓zip文件
[root@localhost 111]# ls
	1.txt  1.txt.gz  2.txt.bz2  3.txt  3.txt.xz  4.txt  4.txt.zip
[root@localhost 111]# unzip 4.txt.zip	  #使用zip工具壓縮解壓時,不會自動刪除源文件
	Archive:  4.txt.zip
	replace 4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A 
	  inflating: 4.txt
  1. 使用zip壓縮目錄
[root@localhost ~]# cd ..
[root@localhost ~]# du -sh 111
	4.2M	111
[root@localhost ~]# zip -r 111.zip 111
	  adding: 111/ (stored 0%)
	  adding: 111/1.txt (deflated 73%)
	  adding: 111/1.txt.gz (deflated 0%)
	  adding: 111/2.txt.bz2 (deflated 0%)
	  adding: 111/3.txt (deflated 73%)
	  adding: 111/3.txt.xz (deflated 0%)
	  adding: 111/4.txt (deflated 73%)
	  adding: 111/4.txt.zip (stored 0%)
[root@localhost ~]# du -sh 111.zip 
	1.8M	111.zip

[root@localhost ~]# unzip 111.zip 
	Archive:  111.zip
	replace 111/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A  
	  inflating: 111/1.txt               
	  inflating: 111/1.txt.gz            
	  inflating: 111/2.txt.bz2           
	  inflating: 111/3.txt               
	  inflating: 111/3.txt.xz            
	  inflating: 111/4.txt               
	 extracting: 111/4.txt.zip

能夠經過-P指定加密或解密ssh

tar打包工具

經常使用

-c	#創建新的備份文件
-C	#指定路徑
-f	#指定備份文件
-v	#顯示執行過程
-t	#列出備份文件內容
-x	#解包
-r	#添加文件到已壓縮的文件中
-m	#保留原有文件不被覆蓋
-N 日期	#只將指定日期更新的文件保存到備份文件裏
--exclude 文件	#過濾

-z	#處理gzip備份文件
-j	#處理bzip2備份文件
-J	#處理xz備份文件

示例

  1. 打包和解包
[root@localhost 111]# tar -cvf 111.tar *	#將全部爲念打包備份並輸出執行過程
	1.txt
	1.txt.gz
	2.txt.bz2
	3.txt
	3.txt.xz
	4.txt
	4.txt.zip
[root@localhost 111]# ls
111.tar  1.txt  1.txt.gz  2.txt.bz2  3.txt  3.txt.xz  4.txt  4.txt.zip

[root@localhost 111]# tar -cvf 222.tar --exclude *	#使用--exclude能夠過濾指定的文件
	1.txt
	1.txt.gz
	2.txt.bz2
	3.txt
	3.txt.xz
	4.txt
	4.txt.zip
[root@localhost 111]# ls
	111.tar  1.txt  1.txt.gz  222.tar  2.txt.bz2  3.txt  3.txt.xz  4.txt  4.txt.zip

[root@localhost 111]# tar -cvf 333.tar *.txt	#將後綴爲txt的文件打包備份並輸出執行過程
	1.txt
	3.txt
	4.txt
[root@localhost 111]# ls
	111.tar  1.txt  1.txt.gz  222.tar  2.txt.bz2  333.tar  3.txt  3.txt.xz  4.txt  4.txt.zip

[root@localhost 111]# tar -xvf 111.tar
	1.txt
	1.txt.gz
	2.txt.bz2
	3.txt
	3.txt.xz
	4.txt
	4.txt.zip
[root@localhost 111]# ls
	111.tar  1.txt  1.txt.gz  222.tar  2.txt.bz2  333.tar  3.txt  3.txt.xz  4.txt  4.txt.zip


[root@localhost 111]# tar -tf 111.tar	#列出tar包的全部文件
	1.txt
	1.txt.gz
	2.txt.bz2
	3.txt
	3.txt.xz
	4.txt
	4.txt.zip
[root@localhost ~]# tar -xvf 111.tar -C . 
	111/
	111/1.txt
	111/1.txt.gz
	111/2.txt.bz2
	111/3.txt
	111/3.txt.xz
	111/4.txt
	111/4.txt.zip
  1. 打包壓縮

tar的真正做用是在以上壓縮工具除了zip能夠壓縮目錄外,用tar打包目錄文件後後方便壓縮工具

[root@localhost 111]# ls -lh 111.tar 
	-rw-r--r--. 1 root root 4.2M Nov  8 13:41 111.tar	#能夠清楚的看出,tar包其實也是個文件

[root@localhost 111]# tar -czvf 444.tar.gz *.txt.*	#打包並使用gzip壓縮
	1.txt.gz
	2.txt.bz2
	3.txt.xz
	4.txt.zip
[root@localhost 111]# ls
	111.tar  1.txt.gz  2.txt.bz2  3.txt     444.tar.gz  4.txt.zip
	1.txt    222.tar   333.tar    3.txt.xz  4.txt

[root@localhost 111]# tar -cjvf 555.tar.bz2 111.tar 222.tar 333.tar	#打包並使用bzip2壓縮
	111.tar
	222.tar
	333.tar
[root@localhost 111]# ls
	111.tar  1.txt.gz  2.txt.bz2  3.txt     444.tar.gz  4.txt.zip
	1.txt    222.tar   333.tar    3.txt.xz  4.txt       555.tar.bz2

[root@localhost 111]# tar -cJvf 666.tar.xz *	#打包並使用xz壓縮
	111.tar
	1.txt
	1.txt.gz
	222.tar
	2.txt.bz2
	333.tar
	3.txt
	3.txt.xz
	444.tar.gz
	4.txt
	4.txt.zip
	555.tar.bz2
[root@localhost 111]# ls
	111.tar  1.txt.gz  2.txt.bz2  3.txt     444.tar.gz  4.txt.zip    666.tar.xz
	1.txt    222.tar   333.tar    3.txt.xz  4.txt       555.tar.bz2

[root@localhost 111]# tar -xzvf 444.tar.gz	#解包並解壓gzip
	1.txt.gz
	2.txt.bz2
	3.txt.xz
	4.txt.zip
[root@localhost 111]# tar -xjvf 555.tar.bz2	#解包並解壓bzip2
	111.tar
	222.tar
	333.tar
[root@localhost 111]# tar -xJvf 666.tar.xz	#解包並解壓xz
	111.tar
	1.txt
	1.txt.gz
	222.tar
	2.txt.bz2
	333.tar
	3.txt
	3.txt.xz
	444.tar.gz
	4.txt
	4.txt.zip
	555.tar.bz2
[root@localhost 111]# ls
	111.tar  1.txt.gz  2.txt.bz2  3.txt     444.tar.gz  4.txt.zip    666.tar.xz
	1.txt    222.tar   333.tar    3.txt.xz  4.txt       555.tar.bz2

其餘

  1. 壓縮工具對比:
  • 壓縮耗時:xz >bzip2>7z>gzip
  • 壓縮比:xz>=7z>bzip2 >gzip(壓縮比=壓縮後文件大小/源文件大小)
  1. 利用tar經過網絡拷貝數據
[root@localhost ~]# ls 111/
	hello.txt
[root@localhost ~]# tar -cvf - 111 | ssh 192.168.9.133 "cd /home/ ; tar xvf -"
	test/
	test/text.txt
	The authenticity of host '192.168.9.133 (192.168.9.133)' can't be established.
	ECDSA key fingerprint is 52:0e:48:ee:1f:78:8e:04:eb:67:2c:48:b2:13:e7:63.
	Are you sure you want to continue connecting (yes/no)? y
	Please type 'yes' or 'no': yes
	Warning: Permanently added '192.168.9.133' (ECDSA) to the list of known hosts.
	root@192.168.9.133's password: 
	test/
	test/text.txt

[root@localhost ~]# tar -cvf - 222 | ssh 192.168.9.133 "cd /home/ ; tar xvf -"
	222/
	222/1.txt
	root@192.168.9.133's password: 
	222/
	222/1.txt

[root@localhost2 ~] ls /home/
	test

將要拷貝的目錄test打包,-表明標準輸出,而後再ssh到目標主機192.168.9.133,運行相應的命令,tar xvf -將前面的標準輸出內容做爲解包對象ui


修改於 171108加密

相關文章
相關標籤/搜索