在Windows系統中,咱們能夠將目錄和文件壓縮,從而下降磁盤的使用量和網絡資源的使用量。一樣的在Linux系統中咱們同樣能夠把文件和目錄壓縮達到咱們下降網絡資源和磁盤資源的使用量。在Linux系統中經常使用的壓縮工具備:網絡
gzip壓縮工具能夠壓縮文件,格式爲gzip filename ,gzip有1-9九個壓縮級別,默認級別是6。爲了試驗效果咱們先在/tmp下生成一個比較大的文件,可使用工具
find /etc/ -type f -name "*conf" -exec cat {} >> /tmp/1.txt ;ui
這條命令的意思是,找到/etc/下名字以conf結尾的文件 並把內容輸出到/tmp/1.txt中。this
[root@localhost tmp]# find /etc/ -type f -name "*conf" -exec cat {} >> /tmp/1.txt \; [root@localhost tmp]# du -h 1.txt 15M 1.txt
而後使用gzip 壓縮文件,而後查看它壓縮先後的對比插件
[root@localhost tmp]# gzip 1.txt [root@localhost tmp]# du -h 1.txt.gz 2.0M 1.txt.gz
發現壓縮前是15M,壓縮以後變成了2M,文件名變爲了1.txt.gz ,這個後綴名是gzip工具附加給文件的,雖然沒有實際意義,可是爲了方便查找和管理你們都在遵循這個規則。當文件被壓縮成壓縮文件以後咱們沒辦法直接使用cat、more等命令查看它的內容,可是咱們可使用zcat 、zmore 等命令查看它的內容,還可使用wc -l查看文件有多少行,以及file命令來查看壓縮文件的信息等。code
[root@localhost tmp]# wc 1.txt.gz 7235 41587 2009225 1.txt.gz [root@localhost tmp]# wc -l 1.txt.gz wc -l命令能夠查看文件有多少行 7235 1.txt.gz [root@localhost tmp]# zmore 1.txt.gz zmore命令能夠查看文件內容 ------> 1.txt.gz <------ # This is an example configuration file for the LVM2 system. # It contains the default settings that would be used if there was no # /etc/lvm/lvm.conf file. # # Refer to 'man lvm.conf' for further information including the file layout. # # Refer to 'man lvm.conf' for information about how settings configured in # this file are combined with built-in values and command line options to [root@localhost tmp]# file 1.txt.gz file命令能夠查看文件信息 1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jun 23 00:28:55 2018
當咱們直接使用gzip filename 時,會把文件壓縮而且不會保存原文件,若是想要保存原文件而後生成一個新的壓縮文件時,可使用 gzip -c 命令orm
[root@localhost tmp]# ls 1.txt 222 2.txt form154853.pdf [root@localhost tmp]# gzip -c 1.txt > 1.txt.gz 使用這個參數後面必須跟壓縮後文件的文件名,不然會報錯 [root@localhost tmp]# ls 1.txt 1.txt.gz 222 2.txt form154853.pdf [root@localhost tmp]# gzip -c 1.txt > 222/1.txt.gz 不但能夠保留原文件,還能夠給新生成的壓縮文件制定目錄 [root@localhost tmp]# tree /tmp /tmp ├── 1.txt ├── 1.txt.gz ├── 222 │ └── 1.txt.gz ├── 2.txt └── form154853.pdf 1 directory, 5 files
能壓縮確定能解壓縮,gzip壓縮的文件須要使用gunzip或gzip -d來解壓縮 gunzip的使用方法跟gzip同樣,能夠不保留壓縮文件,加-c選項能夠保留壓縮文件ip
[root@localhost tmp]# gzip -d -c 1.txt.gz > 3.txt [root@localhost tmp]# ls 1.txt 1.txt.gz 222 2.txt 3.txt form154853.pdf [root@localhost tmp]# wc -l 3.txt 189156 3.txt [root@localhost tmp]# wc -l 1.txt 189156 1.txt [root@localhost tmp]# gunzip -c 1.txt.gz > 222/11.txt [root@localhost tmp]# tree /tmp /tmp ├── 1.txt.gz ├── 222 │ ├── 11.txt │ ├── 1.txt │ └── 1.txt.gz ├── 2.txt ├── 3.txt └── form154853.pdf [root@localhost tmp]# gzip 222 gzip: 222 is a directory -- ignored
在這裏咱們也能夠看到,gzip是不支持壓縮目錄的。資源
bzip2 命令的格式爲bzip2 [-d / -z]_ filename_,它只有壓縮-d和解壓-z兩個經常使用選項在壓縮是能夠不加- z 選項。壓縮級別一樣是1~9 ,默認的壓縮級別是9,bzip2 命令也一樣不能壓縮目錄,在壓縮目錄是會報錯。若是系統沒有安裝這個工具可使用 yum install -y bzip2來安裝it
[root@localhost tmp]# yum install -y bzip2 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn 軟件包 bzip2-1.0.6-13.el7.x86_64 已安裝而且是最新版本 無須任何處理 [root@localhost tmp]# bzip2 1.txt 使用bzip2來壓縮1.txt [root@localhost tmp]# ls 能夠看到源文件1.txt也消失了 1.txt.bz2 222 2.txt 3.txt form154853.pdf [root@localhost tmp]# du -h 1.txt.bz2 bzip2壓縮的比較緊密,發現文件畢1.txt.gz更小 660K 1.txt.bz2 [root@localhost tmp]# bzip2 -d 1.txt.bz2 解壓縮 [root@localhost tmp]# ls 1.txt 222 2.txt 3.txt form154853.pdf [root@localhost tmp]# du -h 1.txt 7.2M 1.txt [root@localhost tmp]# bzip2 -c 1.txt > 1.txt.bz2 bzip2也可使用-c選項來保留源文件 [root@localhost tmp]# ls 1.txt 1.txt.bz2 222 2.txt 3.txt form154853.pdf [root@localhost tmp]# bzmore 1.txt.bz2 ------> 1.txt.bz2 <------ # This is an example configuration file for the LVM2 system. # It contains the default settings that would be used if there was no # /etc/lvm/lvm.conf file. # # Refer to 'man lvm.conf' for further information including the file layout. # # Refer to 'man lvm.conf' for information about how settings configured in # this file are combined with built-in values and command line options to # arrive at the final values used by LVM. # # Refer to 'man lvmconfig' for information about displaying the built-in # and configured values used by LVM. # # If a default value is set in this file (not commented out), then a # new version of LVM using this file will continue using that value, # even if the new version of LVM changes the built-in default value.
查看文件內容可使用bzmore ,bzcat等命令。
xz命令跟bzip2用法幾乎同樣,它壓縮文件更加的緊密,佔用CPU資源也更多
[root@localhost tmp]# ls 1.txt 1.txt.bz2 222 2.txt 3.txt form154853.pdf [root@localhost tmp]# xz 1.txt [root@localhost tmp]# ls 1.txt.bz2 1.txt.xz 222 2.txt 3.txt form154853.pdf [root@localhost tmp]# du -h 1.txt.xz 56K 1.txt.xz 能夠看到壓縮文件只有56K
使用xz -d 命令或unxz 來解壓縮,一樣不能壓縮目錄。 它也可使用-c的參數來保留原文件
[root@localhost tmp]# unxz 1.txt.xz > 44.txt [root@localhost tmp]# ls 1.txt 1.txt.bz2 222 2.txt 3.txt 44.txt form154853.pdf [root@localhost tmp]# xz -c 44.txt > 222/333.txt 使用-c壓縮44.txt會保留源文件 [root@localhost tmp]# tree /tmp /tmp ├── 1.txt ├── 1.txt.bz2 ├── 222 │ ├── 11.txt │ ├── 1.txt │ ├── 1.txt.gz │ └── 333.txt ├── 2.txt ├── 3.txt ├── 44.txt └── form154853.pdf 1 directory, 10 files [root@localhost tmp]# xz 222 xz: 222: Is a directory, skipping 壓縮目錄會報錯