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