Learn a Linux command every day (41): zip

Time:2021-10-16

Command introduction

The zip command can be used to compress and package files.

#Debian / Ubuntu installation
apt-get install zip
#CentOS installation
[[email protected] testdir]# zip
-bash: zip: command not found
[[email protected] testdir]# yum install zip -y

The zip command can also be used to decompress files. Zip is also a common compression and decompression application. After the files are compressed, a new compressed package file with. Zip as the extension will be generated.

Syntax format

zip [ OPTIONS ] [ NAME ..]

Option description

-A   # Adjust the executable automatic decompression file
-B < Working Directory >   # Specify the storage directory of compressed files
-c   # Annotate each compressed file
-d   # To delete the specified file from the compressed file, you can also use the -- delete parameter
-D   # No directory name is created in the compressed file
-f   # Update the existing file and add other files to the compressed file
-F   # Repair damaged compressed files
-g   # Compress the specified file into an existing compressed file without creating a new file
-h   # print the help information
-I < template style >   # Compress only files that match the specified criteria
-m   # Directly delete the original file after compressing and packaging the specified file
-o   # Change the attribute information of the compressed file to be consistent with the attributes of the latest file in the compressed file
-q   # Quiet mode
-r   # Recursive processing
-T < date time >   # Set the date of the compressed file to the specified date
-T   # Check whether each file in the backup file is correct
-u   # Update newer files into compressed files
-v   # Print command execution process information or version information
-X < template style >   # Exclude eligible files when compressing
-z   # Annotate the compressed file
-< compression efficiency >   # Specify compression efficiency (1-9 digits)

Application examples

Create compressed files in the current directory (automatically create files in. Zip)

[[email protected] testdir]# zip mingongge *
  adding: cest.txt.gz (stored 0%)
  adding: cuttest.txt.gz (stored 0%)
  adding: dir/ (stored 0%)
  adding: file (stored 0%)
  adding: mingongge1.txt.gz (stored 0%)
  adding: mingongge2.txt.gz (stored 0%)
  adding: mingongge.txt.md5.gz (stored 0%)
  adding: sort.cut.txt.gz (stored 0%)

Split a large file

[[email protected] ~]# ls -lh
total 22M
-rw-------.  1 root root 1.3K Aug 20 10:39 anaconda-ks.cfg
-rw-r--r--   1 root root  140 Jan 16 11:36 dos_test.txt
drwxr-xr-x   3 root root   39 Aug 30 03:48 goinception
-rw-r--r--   1 root root  13M Aug 30 03:42 goInception-linux-amd64-v1.2.3.tar.gz
drwxr-sr-x  11 root   40 4.0K Dec 24 22:35 httpd-2.4.46
-rw-r--r--   1 root root 9.0M Aug  5 07:32 httpd-2.4.46.tar.gz
-rw-r--r--   1 root root    0 Jan 16 11:32 mingongge.file
drwxr-xr-x   3 root root  192 Jan 16 16:19 testdir
-rw-r--r--   1 root root  140 Jan 16 11:32 test.txt
[[email protected] ~]# zip -s 4M -r mingongge.zip httpd-2.4.46.tar.gz
  adding: httpd-2.4.46.tar.gz (deflated 0%)
[[email protected] ~]# ls -lh
total 31M
-rw-------.  1 root root 1.3K Aug 20 10:39 anaconda-ks.cfg
-rw-r--r--   1 root root  140 Jan 16 11:36 dos_test.txt
drwxr-xr-x   3 root root   39 Aug 30 03:48 goinception
-rw-r--r--   1 root root  13M Aug 30 03:42 goInception-linux-amd64-v1.2.3.tar.gz
drwxr-sr-x  11 root   40 4.0K Dec 24 22:35 httpd-2.4.46
-rw-r--r--   1 root root 9.0M Aug  5 07:32 httpd-2.4.46.tar.gz
-rw-r--r--   1 root root    0 Jan 16 11:32 mingongge.file
-rw-r--r--   1 root root 4.0M Jan 16 16:24 mingongge.z01
-rw-r--r--   1 root root 4.0M Jan 16 16:24 mingongge.z02
-rw-r--r--   1 root root 943K Jan 16 16:24 mingongge.zip
drwxr-xr-x   3 root root  192 Jan 16 16:19 testdir
#It can be seen from the results that it will be divided into three files: 4m mingongge.z01, 4m mingongge.z02 and 943K mingongge.zip.

Learn a Linux command every day (39): Tar

Learn a Linux command every day (40): gzip