Linux命令(6)zip命令

zip命令

zip 壓縮包在 Windows 和 Linux 都比較常見的,zip命令自己即能歸檔又能壓縮。node

  • 功能說明:對一個文件或多個文件進行壓縮ide

  • 用法:zip [選項] 壓縮文件名.zip FILE…測試

    選項 做用
    -r 對目錄下的文件及子目錄進行遞歸壓縮
    -d 從壓縮文件內刪除指定文件
    -q 壓縮時不顯示命令執行過程
    -x 壓縮時排除某些文件不壓縮

示例:複製/var/log/messages到/tmp/compress目錄並對messages文件進行壓縮。遞歸

[root@node1 ~]# cd /tmp/ && mkdir compress
[root@node1 tmp]# cd compress
[root@node1 compress]# cp /var/log/messages ./
[root@node1 compress]# ll -h
total 44K
-rw------- 1 root root 42K Feb 25 14:38 messages
[root@node1 compress]# zip messages.zip messages 
  adding: messages (deflated 94%)
[root@node1 compress]# ll -h
total 48K
-rw------- 1 root root  42K Feb 25 14:38 messages
-rw-r--r-- 1 root root 2.9K Feb 25 14:39 messages.zip

說明: zip 後面先跟目標文件名,也就是壓縮後的自定義壓縮包名,而後是要壓縮的文件或者目錄。ip

示例:將/var/log下的全部目錄及文件複製到/tmp目錄,並使用zip進行歸檔。it

[root@node1 compress]# cp -r /var/log/ /tmp/
[root@node1 compress]# du -sh /tmp/log/
3.9M    /tmp/log/
[root@node1 compress]# cd ..
[root@node1 tmp]# zip -r -q log.zip log/
[root@node1 tmp]# ll -h
total 416K
drwxr-xr-x 2 root root   42 Feb 25 14:39 compress
drwxr-xr-x 8 root root 4.0K Feb 25 14:40 log
-rw-r--r-- 1 root root 411K Feb 25 14:42 log.zip

示例:生成1..10.txt的文件,壓縮時不對10.txt進行壓縮。table

[root@node1 tmp]# mkdir test
[root@node1 tmp]# cd test/
[root@node1 test]# for i in {1..10}.txt;do touch $i;done
[root@node1 test]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
[root@node1 test]# zip text.zip *.txt -x 10.txt
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
  adding: 3.txt (stored 0%)
  adding: 4.txt (stored 0%)
  adding: 5.txt (stored 0%)
  adding: 6.txt (stored 0%)
  adding: 7.txt (stored 0%)
  adding: 8.txt (stored 0%)
  adding: 9.txt (stored 0%)

unzip命令

  • 功能說明:對zip壓縮文件進行解壓縮class

  • 用法:unzip [選項] 壓縮文件名.ziptest

    選項 做用
    -l 列出壓縮文件內的文件
    -d 指定文件解壓後的目錄,默認爲當前目錄
    -q 解壓時不顯示命令執行過程
    -t 測試壓縮文件是否損壞

示例:解壓text.zip壓縮文件到當前目錄。file

[root@node1 test]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  text.zip
[root@node1 test]# rm -f *.txt
[root@node1 test]# unzip text.zip 
Archive:  text.zip
 extracting: 1.txt                   
 extracting: 2.txt                   
 extracting: 3.txt                   
 extracting: 4.txt                   
 extracting: 5.txt                   
 extracting: 6.txt                   
 extracting: 7.txt                   
 extracting: 8.txt                   
 extracting: 9.txt

示例:查看text.zip壓縮文件中的信息。

[root@node1 test]# unzip -l text.zip 
Archive:  text.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  02-25-2018 14:44   1.txt
        0  02-25-2018 14:44   2.txt
        0  02-25-2018 14:44   3.txt
        0  02-25-2018 14:44   4.txt
        0  02-25-2018 14:44   5.txt
        0  02-25-2018 14:44   6.txt
        0  02-25-2018 14:44   7.txt
        0  02-25-2018 14:44   8.txt
        0  02-25-2018 14:44   9.txt
---------                     -------
        0                     9 files

示例:解壓log.zip文件到/tmp/new目錄

[root@node1 test]# cd ..
[root@node1 tmp]# ls
compress  log  log.zip  test
[root@node1 tmp]# unzip -q log.zip -d new/
[root@node1 tmp]# ll new/
total 4
drwxr-xr-x 8 root root 4096 Feb 25 14:40 log
相關文章
相關標籤/搜索