本文使用centos
爲何要壓縮?文件通過壓縮後,其大小會縮小,能夠節約服務器帶寬資源、內存資源,節約運維成本!服務器
Linux平臺的常見壓縮文件格式有如下幾種運維
雖然在Linux中文件後綴並不重要,可是爲了便於區分和管理,壓縮文件仍是最好加上上述的一些後綴名。工具
gzip不能壓縮目錄!!centos7
# 建立一個大文件 [root@localhost tmp]# find /etc/ -type f -exec cat {} >> 1.txt \; [root@localhost tmp]# ls -lh 總用量 25M -rw-r--r--. 1 root root 25M 11月 3 18:53 1.txt #壓縮文件:gzip file [root@localhost tmp]# gzip 1.txt [root@localhost tmp]# ls -lh 總用量 9.3M -rw-r--r--. 1 root root 9.3M 11月 3 18:53 1.txt.gz #解壓縮文件: gzip -d file.gz # gzip -d 等價於 gunzip [root@localhost tmp]# gzip -d 1.txt.gz [root@localhost tmp]# ls -lh 總用量 25M -rw-r--r--. 1 root root 25M 11月 3 18:53 1.txt
#以1級別進行壓縮,默認壓縮級別爲9; # 壓縮級別1-9,壓縮級別越高,佔用CPU資源越高 # gzip -[1-9] file [root@localhost tmp]# gzip -1 1.txt [root@localhost tmp]# ls -lh 總用量 9.9M -rw-r--r--. 1 root root 9.9M 11月 3 18:53 1.txt.gz
[root@localhost tmp]# gzip -c 1.txt > /test/tmp/1.txt.gz [root@localhost tmp]# ls -lh 總用量 34M -rw-r--r--. 1 root root 25M 11月 3 18:53 1.txt -rw-r--r--. 1 root root 9.3M 11月 3 19:04 1.txt.gz
# 注意最好使用絕對路徑表示文件,清楚明白,固然相對路徑表示也能夠。 [[root@localhost tmp]# gzip -d -c /test/tmp/1.txt.gz > /test/tmp/2.txt [root@localhost tmp]# ls -l 總用量 34684 -rw-r--r--. 1 root root 9649736 11月 3 19:04 1.txt.gz -rw-r--r--. 1 root root 25865478 11月 3 19:22 2.txt
# 因爲內容太多,這裏只顯示行數 [root@localhost tmp]# zcat 1.txt.gz | wc -l 176350
可使用file命令查看壓縮文件的具體信息!code
[root@localhost tmp]# file 1.txt.gz 1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Nov 3 18:53:55 2017, max speed
有些壓縮文件被命名爲不帶壓縮文件類型後綴的,這種文件沒法解壓,可使用file來查看文件類型,而後修改後綴,更好區分!!ip
[root@centos7 test]# mv 1.txt.bz2 1.txt [root@centos7 test]# file 1.txt 1.txt: bzip2 compressed data, block size = 900k [root@centos7 test]# mv 1.txt 1.txt.bz2
壓縮率比gzip更高內存
未安裝使用yum install -y bzip2資源
經常使用使用跟gzip命令相似,且一樣不支持壓縮目錄ast
# 壓縮前 [root@localhost tmp]# ls -lh 總用量 25M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt # bzip2壓縮 [root@localhost tmp]# bzip2 1.txt # 壓縮後,比gzip壓縮率高 [root@localhost tmp]# ls -lh 總用量 8.2M -rw-r--r--. 1 root root 8.1M 11月 3 19:22 1.txt.bz2 # file命令查看文件格式 [root@localhost tmp]# file 1.txt.bz2 1.txt.bz2: bzip2 compressed data, block size = 900k # bzip2解壓縮 [root@localhost tmp]# bzip2 -d 1.txt.bz2 [root@localhost tmp]# ls -lh 總用量 25M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
[root@localhost tmp]# bzip2 -c 1.txt > 1.txt.bz2 [root@localhost tmp]# ls -lh 總用量 33M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt -rw-r--r--. 1 root root 8.1M 11月 3 20:04 1.txt.bz2
[root@localhost tmp]# bzip2 -d -c 1.txt.bz2 > 3.txt [root@localhost tmp]# ls -l 總用量 58816 -rw-r--r--. 1 root root 25865478 11月 3 19:22 1.txt -rw-r--r--. 1 root root 8491810 11月 3 20:04 1.txt.bz2 -rw-r--r--. 1 root root 25865478 11月 3 20:06 3.txt
bzip2一樣能夠指定壓縮級別:1-9,默認級別爲6 [root@localhost tmp]# bzip2 -1 1.txt [root@localhost tmp]# ls -lh 總用量 8.8M -rw-r--r--. 1 root root 8.8M 11月 3 19:22 1.txt.bz2
[root@localhost tmp]# bzcat 1.txt.bz2 | wc -l 176350
級別使用方法等同與上述的gzip和bzip2命令,一樣也不能壓縮目錄!!
通常狀況下,上述3種壓縮方式:xz > bz2 > gzip
具體壓縮要看文件內容,也不必定xz的壓縮包最小
# 壓縮 [root@localhost tmp]# xz 1.txt [root@localhost tmp]# ls -lh 總用量 7.0M -rw-r--r--. 1 root root 7.0M 11月 3 19:22 1.txt.xz # 解壓縮 xz -d等價於unxz [root@localhost tmp]# xz -d 1.txt.xz [root@localhost tmp]# ls -lh 總用量 25M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
[root@localhost tmp]# xz -c 1.txt > 1.txt.xz [root@localhost tmp]# ls -lh 總用量 32M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt -rw-r--r--. 1 root root 7.0M 11月 3 21:02 1.txt.xz
[root@localhost tmp]# xz -d -c 1.txt.xz > ./2.txt [root@localhost tmp]# ls -lh 總用量 57M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt -rw-r--r--. 1 root root 7.0M 11月 3 21:02 1.txt.xz -rw-r--r--. 1 root root 25M 11月 3 21:02 2.txt
[root@localhost tmp]# xzcat 1.txt.xz | wc -l 176350
[root@localhost tmp]# xz -1 2.txt [root@localhost tmp]# ls -lh 總用量 40M -rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt -rw-r--r--. 1 root root 7.0M 11月 3 21:02 1.txt.xz -rw-r--r--. 1 root root 7.5M 11月 3 21:02 2.txt.xz