1. 程式人生 > >Linux 文件壓縮

Linux 文件壓縮

log find 添加 compress 註意 creating 詳細 刪除 tor

壓縮工具
compress/uncompress:對應 .Z 結尾的壓縮格式文件
壓縮格式:gz、bz2、xz、zip、Z
  gzip??壓縮文件並刪除源文件(生成.gz的文件)
  gunzip?解壓縮文件(gzip -d有相同的功能)
  bzip2 壓縮文件(壓縮比例比gzip更高後綴為.bz2)
  bunzip2 解壓縮文件(bzip -d有相同的功能)

??壓縮算法不同,壓縮比也會不同
gzip
gzip /PATH/TO/SOMEFILE?壓縮完成後會刪除原文件
  -d?解壓縮
  -#?指定壓縮比(1-9),默認是6
  -c?將壓縮結果送往標準輸出,可以使用重定向將其保存為壓縮文件,從而保留原文件


例子:
??gzip -c /PATH/TO/SOMEFILE > SOMEFILE.gz
gunzip
gunzip /PATH/TO/SOMEFILE.gz??解壓完成後會刪除原文件
zcat /PATH/TO/SOMEFILE.gz??????不解壓的情況,查看文本文件的內容
z系列命令,可以在不經解壓的情況下,直接操作gzip壓縮文件
  zcat 直接顯示壓縮文件的內容
  zless 直接逐行顯示壓縮文件的內容
  zdiff 直接報告壓縮文件的差異內容
  zcmp?直接報告壓縮文件的差異處
演示:
[root@centos7 ~]# cp /var/log/messages /tmp/test/

[root@centos7 ~]# ll /tmp/test/
總用量 288
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f

-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 292504 2月 20 16:28 messages
[root@centos7 ~]# ll -h /tmp/test/messages
-rw------- 1 root root 286K 2月 20 16:28 /tmp/test/messages

# 壓縮,刪除原文件,保留壓縮後以.gz結尾的文件
[root@centos7 ~]# gzip /tmp/test/messages

[root@centos7 ~]# ll -h /tmp/test
總用量 44K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 41K 2月 20 16:28 messages.gz

# 解壓縮,原來的壓縮文件被刪除,保留解壓縮後的文件
[root@centos7 ~]# gunzip /tmp/test/messages.gz
[root@centos7 ~]# ll -h /tmp/test
總用量 288K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 286K 2月 20 16:28 messages

# zcat可以查看壓縮的文件,不建議對大文件使用zcat命令查看
[root@centos7 ~]# zcat /tmp/test/messages.gz

#=====================================================================================
# 解壓縮
[root@centos7 ~]# gzip -d /tmp/test/messages.gz
[root@centos7 ~]# ll /tmp/test/
總用量 288
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 292504 2月 20 16:28 messages

# 將壓縮或解壓縮的結果輸出至標準輸出(gzip -c FILE > /PATH/TP/SOMEFILE.gz)
[root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz
[root@centos7 ~]# ll /tmp/test/
總用量 332
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 292504 2月 20 16:28 messages
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz

# 解壓縮到標準輸出
[root@centos7 ~]# rm -f /tmp/test/messages
[root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages
[root@centos7 ~]# ll /tmp/test/
總用量 332
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 292504 2月 20 16:50 messages
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz

bzip2
比gzip有著更大壓縮比的壓縮工具,使用格式近似
bzip2 /PATH/TO/SOMEFILE
  -d?解壓縮
  -#?指定壓縮比(1-9),默認是6
  -k?壓縮解壓時保留原文件
bunzip2
bunzip2 /PATH/TO/SOMEFILE.bz2 解壓完成後會刪除原文件
bzcat /PATH/TO/SOMEFILE.bz2 不解壓的情況,查看文本文件的內容
演示:
# 壓縮
[root@centos7 ~]# bzip2 /tmp/test/messages

[root@centos7 ~]# ll -h /tmp/test/
總用量 72K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2 #壓縮後的結果
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

# 解壓縮
[root@centos7 ~]# bunzip2 /tmp/test/messages.bz2
[root@centos7 ~]# ll -h /tmp/test/
總用量 332K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 286K 2月 20 16:50 messages # 解壓縮後的結果
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

# -k 選項不用指明重定向的文件,自動保留源文件在當前文件中
[root@centos7 ~]# bzip2 -k /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/
總用量 360K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 286K 2月 20 16:50 messages
-rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

xz
xz /PATH/TO/SOMEFILE
  -d?解壓縮
  -#?指定壓縮比(1-9),默認是6
  -k?壓縮解壓時保留原文件
unxz
unxz /PATH/TO/SOMEFILE.xz 解壓完成後會刪除原文件
xzdec /PATH/TO/SOMEFILE.xz 解壓文件顯示到終端上
xzcat /PATH/TO/SOMEFILE.xz 不解壓的情況,查看文本文件的內容

註意:以上壓縮工具只能壓縮文件,不能壓縮目錄,如果指定目錄中的所有文件/PATH/TO/*會將目錄中的每個文件壓縮成一個壓縮文件,所以在壓縮目錄的時候還需要歸檔工具
演示:
[root@centos7 ~]# xz /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/
總用量 96K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21K 2月 20 16:50 messages.xz

zip 既歸檔又壓縮的工具(window下面的.zip的文件可以直接在linux下解壓)
zip FILENAME.zip FILE1 FILE2 ... 壓縮多個文件到一個目錄中,壓縮後不刪除原文件
unzip FILENAME.zip
如果要壓縮默認目錄,要通過指定目錄的所有文件/PATH/TO/*才能壓縮整個目錄下的文件,如果指定的是/PATH/TO/就只會壓縮TO這個目錄本身並不包括目錄中的文件
  zip /PATH/TO/ /PATH/TO/SOMEFILE.zip?只是壓縮了TO目錄
  zip /PATH/TO/* /PATH/TO/SOMEFILE.zip 壓縮了TO目錄中的所有文件
演示:
# 對目錄進行歸檔並壓縮
[root@centos7 test]# zip /tmp/test/tao.zip tao
adding: tao/ (stored 0%)
[root@centos7 test]# ll
總用量 188
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
-rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

[root@centos7 test]# rm -fr tao

# 解壓縮
[root@centos7 test]# unzip tao.zip
Archive: tao.zip
creating: tao/
[root@centos7 test]# ll
總用量 188
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 6 2月 20 17:43 tao
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
-rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log


tar?歸檔壓縮工具(archive歸檔本身並不意味著壓縮)?
主要參數:
  -f 指定創建或展開歸檔文件名(只要操作歸檔文件就要用-f)
  -c 創建歸檔文件
  -v 顯示創建的詳細過程
  -x 展開歸檔文件
  -r 將文件添加到已經存在的歸檔文件中(壓縮後是不能用r的,只有歸檔的時候用)
  -z 用gzip來壓縮和解壓縮文件
  -j 用bz2來壓縮和解壓縮文件
  -J 用xz來壓縮和解壓縮文件
  -t 不展開歸檔文件情況下查看文件中的內容
  -C 展開或解壓文件到指定目錄(如果沒指定該參數,默認是當前目錄)
  --xattrs?歸檔時,保留文件的擴展屬性信息

  -zcf 歸檔並調用gzip壓縮
  -zxf 調用gzip解壓縮並展開歸檔,-z選項可省略(解壓時自動判斷用什麽工具壓縮)

  -jcf 歸檔並調用bzip2壓縮
  -jxf 調用bzip2解壓縮並展開歸檔,-j選項可省略

  -Jcf 歸檔並調用xz壓縮
  -Jxf 調用xz解壓縮並展開歸檔,-J選項可省略

??例如:
??tar -cvf home.tar /home 將home目錄打包成home.tar(只是打包並沒有壓縮)
??tar -rvf home.tar /etc 將etc目錄添加到已存在的打包文件home.tar裏面
??tar -czvf home.tar.gz /home 將home目錄打包並壓縮成home.tar.gz
??tar -cvjf home.tar.bz2 /home 將home目錄打包並壓縮成.bz2的格式
??tar -xzvf home.tar.gz 將home.tar.gz這個備份文件還原並解壓縮
??tar -tvf home.tar | more 查看home.tar備份文件的類容,並以分屏方式顯示在顯示器上
??tar -cvf /dev/st0 /home/shrek 可以將st0磁帶機備份到/home目錄下面

註意:f選項一定要寫在最後一個;如果是解開壓縮歸檔文件可以不指定壓縮選項zjJ,tar會通過文件後綴判斷是什麽壓縮工具壓縮的
歸檔演示:
[root@centos7 ~]# ls /tmp/test/tao
boot.log fstab issue pacemaker.log wpa_supplicant.log Xorg.0.log yum.log

# 對所有以 .log 結尾的文件進行歸檔
[root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log
[root@centos7 ~]# ll /tmp/test/
總用量 136
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:45 mylog.tar # 歸檔後的文件
drwxr-xr-x 2 root root 121 2月 20 17:43 tao

# 展開歸檔
[root@centos7 test]# tar xf mylog.tar
[root@centos7 test]# ll
總用量 176
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

# -C 展開歸檔至指定文件中
[root@centos7 test]# mkdir /tmp/newtest
[root@centos7 test]# tar xf mylog.tar -C /tmp/newtest
[root@centos7 test]# ll /tmp/newtest
總用量 40
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

# 查看歸檔文件中的文件列表
[root@centos7 test]# tar tf mylog.tar
boot.log
pacemaker.log
wpa_supplicant.log
Xorg.0.log
yum.log

歸檔並壓縮演示:
# 對目錄進行歸檔並壓縮
[root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao
[root@centos7 test]# ll /tmp/test
總用量 184
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 原文件
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz # 歸檔壓縮後的文件
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

# 刪除原文件
[root@centos7 test]# rm -fr tao

# 展開歸檔,其中 z 可省略,tar命令會自動識別其為壓縮文件
[root@centos7 test]# tar xf tao.tar.gz
[root@centos7 test]# ll
總用量 184
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 展開後的文件
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

cpio?歸檔工具(把文件復制為歸檔文件)
cpio命令是通過重定向的方式將文件進行打包備份,還原恢復的工具,它可以解壓以“.cpio”或者“.tar”結尾的文件。
用法:
  cpio[選項] > 文件名或者設備名
  cpio[選項] < 文件名或者設備名
選項:
  -o 將文件拷貝打包成文件或者將文件輸出到設備上
  -i 解包,將打包文件解壓或將設備上的備份還原到系統
  -t 預覽,查看文件內容或者輸出到設備上的文件內容
  -v 顯示打包過程中的文件名稱
  -d 解包生成目錄,在cpio還原時,自動的建立目錄
  -c 一種較新的存儲方式
示例:
將etc目錄備份:
  find . /etc -print |cpio -ov > etc.cpio
內容預覽
  cpio -tv < etc.cpio
要解包文件
  cpio -iv < etc.cpio
  cpio -idv < etc.cpio

壓縮工具 ??compress/uncompress:對應 .Z 結尾的壓縮格式文件??壓縮格式:gz、bz2、xz、zip、Z????gzip???????壓縮文件並刪除源文件(生成.gz的文件)????gunzip????解壓縮文件(gzip -d有相同的功能)????bzip2 壓縮文件(壓縮比例比gzip更高後綴為.bz2)????bunzip2 解壓縮文件(bzip -d有相同的功能)
??壓縮算法不同,壓縮比也會不同??gzip??gzip /PATH/TO/SOMEFILE??壓縮完成後會刪除原文件????-d????解壓縮??? -#????指定壓縮比(1-9),默認是6????-c????將壓縮結果送往標準輸出,可以使用重定向將其保存為壓縮文件,從而保留原文件??例子:??gzip -c /PATH/TO/SOMEFILE > SOMEFILE.gz??gunzip??gunzip /PATH/TO/SOMEFILE.gz??解壓完成後會刪除原文件??zcat /PATH/TO/SOMEFILE.gz??????不解壓的情況,查看文本文件的內容??z系列命令,可以在不經解壓的情況下,直接操作gzip壓縮文件????zcat 直接顯示壓縮文件的內容????zless 直接逐行顯示壓縮文件的內容????zdiff 直接報告壓縮文件的差異內容????zcmp????? 直接報告壓縮文件的差異處演示: [root@centos7 ~]# cp /var/log/messages /tmp/test/ [root@centos7 ~]# ll /tmp/test/ 總用量 288 -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw------- 1 root root 292504 2月 20 16:28 messages [root@centos7 ~]# ll -h /tmp/test/messages -rw------- 1 root root 286K 2月 20 16:28 /tmp/test/messages
# 壓縮,刪除原文件,保留壓縮後以.gz結尾的文件 [root@centos7 ~]# gzip /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test 總用量 44K -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw------- 1 root root 41K 2月 20 16:28 messages.gz
# 解壓縮,原來的壓縮文件被刪除,保留解壓縮後的文件 [root@centos7 ~]# gunzip /tmp/test/messages.gz [root@centos7 ~]# ll -h /tmp/test 總用量 288K -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw------- 1 root root 286K 2月 20 16:28 messages
# zcat可以查看壓縮的文件,不建議對大文件使用zcat命令查看 [root@centos7 ~]# zcat /tmp/test/messages.gz
#===================================================================================== # 解壓縮 [root@centos7 ~]# gzip -d /tmp/test/messages.gz [root@centos7 ~]# ll /tmp/test/ 總用量 288 -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw------- 1 root root 292504 2月 20 16:28 messages
# 將壓縮或解壓縮的結果輸出至標準輸出(gzip -c FILE > /PATH/TP/SOMEFILE.gz) [root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz [root@centos7 ~]# ll /tmp/test/ 總用量 332 -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw------- 1 root root 292504 2月 20 16:28 messages -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
# 解壓縮到標準輸出 [root@centos7 ~]# rm -f /tmp/test/messages [root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages [root@centos7 ~]# ll /tmp/test/ 總用量 332 -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw-r--r-- 1 root root 292504 2月 20 16:50 messages -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
??bzip2????比gzip有著更大壓縮比的壓縮工具,使用格式近似????bzip2 /PATH/TO/SOMEFILE??????-d????解壓縮??????-#????指定壓縮比(1-9),默認是6??????-k????壓縮解壓時保留原文件??bunzip2????bunzip2 /PATH/TO/SOMEFILE.bz2 解壓完成後會刪除原文件????bzcat /PATH/TO/SOMEFILE.bz2 不解壓的情況,查看文本文件的內容演示: # 壓縮 [root@centos7 ~]# bzip2 /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/ 總用量 72K -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2 #壓縮後的結果 -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz
# 解壓縮 [root@centos7 ~]# bunzip2 /tmp/test/messages.bz2 [root@centos7 ~]# ll -h /tmp/test/ 總用量 332K -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw-r--r-- 1 root root 286K 2月 20 16:50 messages # 解壓縮後的結果 -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz
# -k 選項不用指明重定向的文件,自動保留源文件在當前文件中 [root@centos7 ~]# bzip2 -k /tmp/test/messages [root@centos7 ~]# ll -h /tmp/test/ 總用量 360K -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw-r--r-- 1 root root 286K 2月 20 16:50 messages -rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz
??xz????xz /PATH/TO/SOMEFILE??????-d????解壓縮??????-#????指定壓縮比(1-9),默認是6??????-k????壓縮解壓時保留原文件??unxz????unxz /PATH/TO/SOMEFILE.xz 解壓完成後會刪除原文件????xzdec /PATH/TO/SOMEFILE.xz 解壓文件顯示到終端上????xzcat /PATH/TO/SOMEFILE.xz 不解壓的情況,查看文本文件的內容以上壓縮工具只能壓縮文件,不能壓縮目錄,如果指定目錄中的所有文件/PATH/TO/*會將目錄中的每個文件壓縮成一個壓縮文件,所以在壓縮目錄的時候還需要歸檔工具演示: [root@centos7 ~]# xz /tmp/test/messages [root@centos7 ~]# ll -h /tmp/test/ 總用量 96K -rw-r----- 1 root root 0 2月 20 13:41 a -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger -r--r----- 1 root root 0 2月 20 13:41 c -rwxrwxr-x 1 root root 0 2月 20 13:41 d -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger -rw-r--r-- 1 root root 0 2月 20 13:41 f -rw-r--r-- 1 root root 0 2月 20 13:41 g -rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21K 2月 20 16:50 messages.xz
??zip??既歸檔又壓縮的工具(window下面的.zip的文件可以直接在linux下解壓) ????zip FILENAME.zip FILE1 FILE2 ... 壓縮多個文件到一個目錄中,壓縮後不刪除原文件????unzip FILENAME.zip????如果要壓縮默認目錄,要通過指定目錄的所有文件/PATH/TO/*才能壓縮整個目錄下的文件,如果指定的是/PATH/TO/就只會壓縮TO這個目錄本身並不包括目錄中的文件????zip /PATH/TO/????/PATH/TO/SOMEFILE.zip??只是壓縮了TO目錄????zip /PATH/TO/*??/PATH/TO/SOMEFILE.zip??壓縮了TO目錄中的所有文件演示: # 對目錄進行歸檔並壓縮 [root@centos7 test]# zip /tmp/test/tao.zip tao adding: tao/ (stored 0%) [root@centos7 test]# ll 總用量 188 -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log drwxr-xr-x 2 root root 121 2月 20 17:43 tao -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz -rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log -rw------- 1 root root 105 2月 20 17:42 yum.log
[root@centos7 test]# rm -fr tao
# 解壓縮 [root@centos7 test]# unzip tao.zip Archive: tao.zip creating: tao/ [root@centos7 test]# ll 總用量 188 -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log drwxr-xr-x 2 root root 6 2月 20 17:43 tao -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz -rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log -rw------- 1 root root 105 2月 20 17:42 yum.log

??tar??歸檔壓縮工具(?archive歸檔本身並不意味著壓縮)???主要參數:????-f 指定創建或展開歸檔文件名(只要操作歸檔文件就要用-f)????-c 創建歸檔文件????-v 顯示創建的詳細過程????-x 展開歸檔文件????-r 將文件添加到已經存在的歸檔文件中(壓縮後是不能用r的,只有歸檔的時候用)????-z 用gzip來壓縮和解壓縮文件??? -j 用bz2來壓縮和解壓縮文件????-J???????? 用xz來壓縮和解壓縮文件????-t 不展開歸檔文件情況下查看文件中的內容????-C 展開或解壓文件到指定目錄(如果沒指定該參數,默認是當前目錄) --xattrs??歸檔時,保留文件的擴展屬性信息
????-zcf 歸檔並調用gzip壓縮????-zxf 調用gzip解壓縮並展開歸檔,-z選項可省略(解壓時自動判斷用什麽工具壓縮)
????-jcf 歸檔並調用bzip2壓縮????-jxf 調用bzip2解壓縮並展開歸檔,-j選項可省略
????-Jcf 歸檔並調用xz壓縮????-Jxf 調用xz解壓縮並展開歸檔,-J選項可省略
??例如:??tar -cvf home.tar /home 將home目錄打包成home.tar(只是打包並沒有壓縮)??tar -rvf home.tar /etc 將etc目錄添加到已存在的打包文件home.tar裏面??tar -czvf home.tar.gz /home 將home目錄打包並壓縮成home.tar.gz??tar -cvjf home.tar.bz2 /home 將home目錄打包並壓縮成.bz2的格式??tar -xzvf home.tar.gz 將home.tar.gz這個備份文件還原並解壓縮??tar -tvf home.tar | more 查看home.tar備份文件的類容,並以分屏方式顯示在顯示器上??tar -cvf /dev/st0 /home/shrek 可以將st0磁帶機備份到/home目錄下面
註意:f選項一定要寫在最後一個;如果是解開壓縮歸檔文件可以不指定壓縮選項zjJ,tar會通過文件後綴判斷是什麽壓縮工具壓縮的歸檔演示: [root@centos7 ~]# ls /tmp/test/tao boot.log fstab issue pacemaker.log wpa_supplicant.log Xorg.0.log yum.log
# 對所有以 .log 結尾的文件進行歸檔 [root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log [root@centos7 ~]# ll /tmp/test/ 總用量 136 -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz -rw-r--r-- 1 root root 40960 2月 20 17:45 mylog.tar # 歸檔後的文件 drwxr-xr-x 2 root root 121 2月 20 17:43 tao
# 展開歸檔 [root@centos7 test]# tar xf mylog.tar [root@centos7 test]# ll 總用量 176 -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log drwxr-xr-x 2 root root 121 2月 20 17:43 tao -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log -rw------- 1 root root 105 2月 20 17:42 yum.log
# -C 展開歸檔至指定文件中 [root@centos7 test]# mkdir /tmp/newtest [root@centos7 test]# tar xf mylog.tar -C /tmp/newtest [root@centos7 test]# ll /tmp/newtest 總用量 40 -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log -rw------- 1 root root 105 2月 20 17:42 yum.log
# 查看歸檔文件中的文件列表 [root@centos7 test]# tar tf mylog.tar boot.log pacemaker.log wpa_supplicant.log Xorg.0.log yum.log
歸檔並壓縮演示: # 對目錄進行歸檔並壓縮 [root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao [root@centos7 test]# ll /tmp/test 總用量 184 -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 原文件 -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz # 歸檔壓縮後的文件 -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log -rw------- 1 root root 105 2月 20 17:42 yum.log
# 刪除原文件 [root@centos7 test]# rm -fr tao
# 展開歸檔,其中 z 可省略,tar命令會自動識別其為壓縮文件 [root@centos7 test]# tar xf tao.tar.gz [root@centos7 test]# ll 總用量 184 -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2 -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 展開後的文件 -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log -rw------- 1 root root 105 2月 20 17:42 yum.log
cpio??歸檔工具(把文件復制為歸檔文件)cpio命令是通過重定向的方式將文件進行打包備份,還原恢復的工具,它可以解壓以“.cpio”或者“.tar”結尾的文件。 用法: cpio[選項] > 文件名或者設備名 cpio[選項] < 文件名或者設備名 選項: -o:將文件拷貝打包成文件或者將文件輸出到設備上 -i:解包,將打包文件解壓或將設備上的備份還原到系統 -t:預覽,查看文件內容或者輸出到設備上的文件內容 -v:顯示打包過程中的文件名稱 -d:解包生成目錄,在cpio還原時,自動的建立目錄 -c:一種較新的存儲方式 示例: 將etc目錄備份: find . /etc -print |cpio -ov > etc.cpio 內容預覽 cpio -tv < etc.cpio 要解包文件 cpio -iv < etc.cpio cpio -idv < etc.cpio

Linux 文件壓縮