Linux command zip command

zip command

Zip compression packages are common on both Windows and Linux, and the zip command itself can be archived and compressed.

  • Capability description: Compress one or more files

  • Usage: zip [options] compressed file FILE...

    option Effect
    -r Recursive compression of files and subdirectories under a directory
    -d Delete the specified file from the compressed file
    -q Do not show command execution when compressing
    -x Exclude some files from compression without compression

Example: Copy/var/log/messages to/tmp/compress directory and compress messages files.

[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 
  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

Description: zip is followed by the target file name, which is the compressed custom compressed package name, then the file or directory to be compressed.

Example: Copy all directories and files under /var/log to the / tmp directory and use zip for archiving.

[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/
[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

Example: Generate a file of 1..10.txt without compressing 10.txt.

[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 *.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 command

  • Function description: Unzip zip compressed file

  • Usage: unzip [option] compressed file

    option Effect
    -l List files in compressed files
    -d Specifies the unzipped directory of the file, defaulting to the current directory
    -q Do not show command execution when decompressing
    -t Test for corruption of compressed files

Example: Unzip the compressed file to the current directory.

[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]# rm -f *.txt
[root@node1 test]# unzip 
 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

Example: View the information in the compressed file.

[root@node1 test]# unzip -l 
  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

Example: Unzip file to/tmp/new directory

[root@node1 test]# cd ..
[root@node1 tmp]# ls
compress  log  test
[root@node1 tmp]# unzip -q -d new/
[root@node1 tmp]# ll new/
total 4
drwxr-xr-x 8 root root 4096 Feb 25 14:40 log

Tags: Linux Windows

Posted on Sun, 17 May 2020 12:58:22 -0400 by ILMV