Common Linux commands (1): zip/unzip command (compressed file/unzip file)

Preface

This article mainly explains how to compress files into zip packages and decompress zip packages on Linux.

The zip command can be used to decompress files or package files. Zip is a widely used compression program. After the file is compressed by it, a compressed file with a ".zip" extension will be generated.
The zip command can be used to package files into a compressed package in .zip format, which will contain the file name, path, creation time, last modification time and other information, similar to the tar command.

1. Linux installation zip file

1. Check whether the compression/decompression zip package is installed

yum list installed | grep zip

Insert image description here
2. Enter the following command to install the zip/unzip software:

Choose one of the following two methods

方式一:
sudo apt-get install zip unzip

方式二:
yum -y install zip unzip

3. Extended commands to install and uninstall updates

命令1:yum -y install [关键词]
命令2:yum -y remove [关键词]
命令3:yum -y update [关键词]

2. zip syntax

The syntax of this command is as follows:

zip [选项] 压缩包名 源文件或源目录列表

Common command options

-a, --ascii将文件转换为 ASCII 格式
-A, --adjust-sfx调整自解压可执行归档文件。通过将 SFX 存根添加到现有存档中首部,可以创建自解压缩的可执行存档
-b, --temp-path PATH指定暂时 zip 文件的目录
-c, --entry-comments为每一个被压缩的文件加上一行注释说明
-d, --delete删除压缩文件内指定的文件。例如 zip -d foo foo/tom/junk foo/harry/\* \*.o,将移除文件 foo/tom/junk以及所有以 foo/harry/ 开头的文件以及后缀为 .o 的文件
-db, --display-bytes显示已处理的字节和剩余的字节
-dc, --display-counts显示已压缩的条目数和剩余条目数
-dd, --display-dots压缩每个条目时显示进度条。进度条使用点表示,默认每个点表示压缩了 10MB
-dg, --display-globaldots显示整体压缩的进度条,而不是每个文件的进度条。默认每个点表示压缩了 10MB
-ds, --dot-size SIZE设置进度条中每个点表示已处理的文件大小,默认为 10MB。如果设置为 0 表示关闭进度条
-du, --display-usize显示每个条目未压缩的大小
-dv, --display-volume显示每个条目的卷(磁盘)号
-D, --no-dir-entries忽略目录
-DF, --difference-archive增量压缩,即压缩新增或更新的文件到旧的压缩归档中。应该从运行原始 zip 命令的相同目录运行,因为存储在 zip 归档中的文件路径信息会被保留
-e, --encrypt压缩时进行加密,输入的密码不会被打印出来
-f, --freshen增量压缩,只压缩更新的文件。应该从运行原始 zip 命令的相同目录运行,因为存储在 zip 归档中的文件路径信息会被保留
-F, --fix, -FF, --fixfix修复已损失的压缩文件。如果压缩文件的某些部分丢失,则可以使用 -F 选项来修复,但需要一个相当完整的中心目录。如果压缩文件损坏严重或者被截断,则需要 -FF 来修复
-FS, --filesync将存档的内容与操作系统上的文件进行同步。如果归档中的条目与操作系统上的文件不匹配,则删除该条目
-g, --grow向已存在的 zip 归档文件追加内容。如果此操作失败,zip 将尝试还原存档文件到其原始状态。如果恢复失败,则存档文件可能会被损坏
-h, -?, --help相识帮助信息并退出。当 zip 无参数运行时,等同于使用该选项,显示帮助信息
-h2, --more-help显示扩展的帮助信息,包括更多关于命令行格式、模式匹配和更多少用的选项
-i, --include FILES指定被压缩的文件
-j, --junk-paths不保留被压缩的文件的目录信息,只保留文件名
-J, --junk-sfx从存档中删除所有前置追加的数据(例如 SFX 存根)
-k, --DOS-names尝试转换名称和路径以符合 MSDOS,只存储 MSDOS 属性将条目标记为 MSDOS 下的条目
-l, --to-crlf将 Unix 的行尾字符 LF 转换成 MSDOS 约定的 CR LF。此选项不应用于二进制文件
la, --log-append附加到现有日志文件。默认是覆盖
-lf, --logfile-path LOGFILEPATH打开指定的日志文件
-li, --log-info在日志中包含更多信息,例如被压缩的文件名。默认情况下只包含命令行、警告和错误以及最终状态
-ll, --from-crlf作用于 -l 选项相反。将 MSDOS 的结束行 CR LF 转换为 Unix 的 LF。此选项不应用于二进制文件
-L, --license显示 zip 许可证
-m, --move将指定的文件移动到 zip 归档文件中。目标文件会被删除,如果目标文件所在的目录变成空目录,也会被删除
-MM, --must-match所有输入的模式至少匹配一个文件,且所有输入的文件必须可读,否则 zip 将返回 OPEN 错误并退出
-n, --suffixes SUFFIXES不要尝试压缩指定后缀的文件。这些文件只是简单地存储在输出 zip(0%压缩)
-nw, --no-wild不执行内部通配符的处理。注意 Shell 仍然会执行通配符的处理,除非对通配符使用了转义
-o, --latest-time将 zip 文件的最后修改时间更改为其所有条目中最近修改时间中最近的时间
-O, --output-file OUTPUT-FILE不更改现有 zip 文件,指定新输出的 zip 文件
-p, --paths在 zip 文件中存储文件的路径信息。该选项为默认选项,可以使用 -j 选项只存储文件名
-P, --password PASSWORD使用指定密码加密 zip 中的条目。注意,该操作不安全,因为密码可能会被其他用户通过查看历史命令窥探到,安全的做法是通过交互式输入不可打印的密码完成加密
-q, --quiet静默模式。消除各种消息和提示
-r, --recurse-paths递归压缩指定目录
-R, --recurse-patterns递归遍历当前目录
-s, --split-size SPLITSIZE指定 zip 归档文件被拆分的大小
-sb, --split-bellzip 暂停分隔归档文件时响铃提示
-sc, --show-command在 zip 启动和结束时,显示命令行
-sf, --show-files显示将要操作的文件,然后退出。例如,如果创建一个新的存档,它将列出将要添加的文件。如果该选项后跟 -,即 -sf-,仅输出到打开的日志文件。对于大列表,不建议使用屏幕显示
-so, --show-options显示 zip 所有可用的选项
-sp, --split-pause如果使用 -s 启用分割,则启用分割暂停模式
-su, --show-unicode类似于选项 -sf,但是如果存在 Unicode 版本的路径则显示
-sU, --show-just-unicode如果存在,只显示 Unicode 版本的路径,否则显示该路径的标准版本
-sv, --split-verbose输出拆分时的各种详细消息,显示如何进行拆分
-t, --from-date MMDDYYYY不对最后修改时间早于指定日期的文件进行操作。其中 MM 表示月(00-12),DD 表示每个月的日(01-31),YYYY 表示年
-T, --test检查新 zip 文件的完整性,如果检查失败,那么旧的 zip 文件不会被改变,且输入的文件不会被删除(如果使用 -m 选项)
-TT, --unzip-command CMD当使用选项 -T 时,使用指定的命令对 zip 归档文件进行检测,而不是使用默认的命令 unzip -tqq
-u, --update更新 zip 归档文件中被更新的条目
-U, --copy-entries将条目从一个 zip 存档复制到另一个存档。需要 --out 选项来指定与输入存档不同的输出文件
-UN, --unicode V确定 zip 如何处理 Unicode 文件名。V 可取值有:q 如果路径不匹配,则退出;w 警告,继续使用标准路径;i 忽略 继续使用标准路径;n 不使用 Unicode 路径
-v, --verbose详细模式或打印诊断与版本信息
-ws, --wild-stop-dirs通配符 * 只用于替换文件而不包含目录。如 /foo/bar/* 只会匹配 /foo/bar/file2.c,不会匹配 /foo/bar/dir/file1.c
-x, --exclude FILES显式排除指定的文件,如 zip -r foo foo -x \*.o,将排除目录 foo 下所有以 .o 结尾的文件。注意,星号需要转义,以免被 Shell 替换
-X, --no-extra不保存额外的文件属性
-y, --symlinks压缩存储符号链接本身而不是目标文件
-z, --archive-comment提示为整个 zip 归档文件输入多行注释。注释以仅包含一个点号的行结束或者输入 ^D。也可以从文件中获取注释,如 zip -z foo < foowhat
-Z, --compression-method CM设置默认的压缩算法。目前 zip 支持的主要算法是 store、deflate 和 bzip2
-#(-0, -1, -2, -3, -4, -5, -6, -7, -8, -9)使用指定的数字 # 调节压缩速度,其中 -0 表 示没有压缩,仅打包所有文件;-1 表示最快的压缩速度,压缩率较低;-9表示最慢的压缩速度,最佳的压缩效果;默认压缩级别为 -6
-@, --names-stdin从标准输入获取输入文件列表,每行一个文件

2.1. Examples of common options

Use examples to explain the following commonly used options:

Options effect
-r Recursively compress a directory and all its subdirectories and files into a ZIP file
-q Enable silent mode when compressing files, i.e. do not display details of the compression process
-d Remove specified files or directories from an existing ZIP file
-u Used to update existing ZIP files, add new or modified files to the ZIP archive
-f Used to refresh (update) the specified file in an existing ZIP file.
-m Used to move (archive) files into a ZIP compressed file and delete the source files after moving.
-e Used to encrypt ZIP compressed files.
-z Add comments to compressed files

2.1.1, -r (compress folder, solve 80% of scenarios)

注意:There are many common command options listed above. If you just want one command to compress, then zip -r 压缩包名.zip 文件1 文件2 文件nthis command can meet your needs.

For example: I want to compress the files with the following hierarchical structure into a zip package

/home/test
    	└─a.txt
    	└─b.log
    	└─test01
        	└─c.txt

Scenario 1: Compress multiple files (the most commonly used command, solving 80% of scenarios)

If you want to package a.txt and b.log into a zip file, enter the /home/test directory and execute zip -r test.zip a.txt b.log.压缩包内默认不包含当前目录

[root@centos test]# pwd
/home/test
[root@centos test]# ll
总用量 8
-rw-r--r--. 1 root root 11 911 23:32 a.txt
-rw-r--r--. 1 root root 23 911 23:56 b.log
drwxr-xr-x. 2 root root 19 914 15:55 test01
[root@centos test]# zip -r test.zip a.txt b.log
  adding: a.txt (stored 0%)
  adding: b.log (deflated 74%)
[root@centos test]# 

输出中 deflated 74% 表示压缩率为 74%,即压缩后的文件大小是原文件的 26%。Insert image description here

Scenario 2: Compress all files and retain the hierarchical structure.
Compress the files in the /home/test directory into a zip package, store the zip package in the /home directory, and retain the directory hierarchical structure in the zip package.

Execute the command zip -r /home/test.zip /home/test

[root@centos test]# zip -r /home/test.zip /home/test
  adding: home/test/ (stored 0%)
  adding: home/test/a.txt (stored 0%)
  adding: home/test/b.log (deflated 74%)
  adding: home/test/test01/ (stored 0%)
  adding: home/test/test01/c.txt (deflated 80%)

Insert image description here

Scenario 3: Compress all files, the hierarchical structure starts from test

After entering the home directory, then execute the command zip -r test.zip test

[root@centos home]# pwd
/home
[root@centos home]# ll
总用量 8
drwxr-xr-x.  3 root  root    46 914 15:38 test

[root@centos home]# zip -r test.zip test
  adding: test/ (stored 0%)
  adding: test/a.txt (stored 0%)
  adding: test/b.log (deflated 74%)
  adding: test/test01/ (stored 0%)
  adding: test/test01/c.txt (deflated 80%)
[root@centos home]# 

Insert image description here

Scenario 4: Compress files of specified type.
Compress all files ending with .txt in the current directory.

[root@centos test]# ll
总用量 8
-rw-r--r--. 1 root root 11 911 23:32 a.txt
-rw-r--r--. 1 root root 23 911 23:56 b.log
drwxr-xr-x. 2 root root 19 914 15:55 test01
[root@centos test]# zip -r test.zip  *.txt
  adding: a.txt (stored 0%)
[root@centos test]# 

Insert image description here

Scenario 5: Compress files and exclude specified files.
Compress files in the test directory and exclude the test01 directory. -x specifies the excluded directory.注意没有双引号将不起作用。

[root@centos test]# zip -r test.zip /home/test -x "/home/test/test01/*"
  adding: home/test/ (stored 0%)
  adding: home/test/a.txt (stored 0%)
  adding: home/test/b.log (deflated 74%)
[root@centos test]# zip -r test.zip /home/test -x /home/test/test01/*
  adding: home/test/ (stored 0%)
  adding: home/test/a.txt (stored 0%)
  adding: home/test/test01/ (stored 0%)
  adding: home/test/b.log (deflated 74%)
[root@centos test]# 

注意:-x排除目录需要加上双引号,若不加双引号,会把空文件夹打包进来,如上面的日志

Scenario 6: Compress files and ignore specified files.
Similar to the -x parameter above.

[root@centos test]# zip -r test.zip /home/test --exclude "/home/test/test01/*"
updating: home/test/ (stored 0%)
updating: home/test/a.txt (stored 0%)
updating: home/test/b.log (deflated 74%)

2.1.2、-q

-q option: Do not display detailed information about the compression process when compressing files.

[root@centos test]# zip -r test.zip a.txt b.log  test01
  adding: a.txt (stored 0%)
  adding: b.log (deflated 74%)
  adding: test01/ (stored 0%)
  adding: test01/c.txt (deflated 80%)
[root@centos test]# zip -q -r test.zip a.txt b.log  test01
[root@centos test]# 

As you can see, when the -q option is added, the compression log will not be printed.

2.1.3, -d (delete the specified file from the compressed file)

-d option: (d is the abbreviation of delete) deletes the specified files or directories from the existing ZIP file. Unnecessary files or directories can be deleted from the ZIP archive.

[root@centos test]# zip -d test.zip /home/test/a.txt
deleting: home/test/a.txt
[root@centos test]# 

2.1.4, -u (update file)

-u option: used to update existing ZIP files, add new or modified files to the ZIP archive.

语法:zip -u  <压缩文件名>  <目录/文件>
示例:zip -u test.zip t3.txt

解释:如果test.zip压缩文件中没有t3.txt,则test.zip中会增加新文件t3.txt;如果有t3.txt,则会替换test.zip压缩文件中的旧t3.txt为新的t3.txt。

2.1.5、-f

-f option: used to refresh (update) the specified file in an existing ZIP file.

语法:zip -f <压缩文件名> <要刷新(更新)的文件>
示例:zip -f test.zip t3.txt。

解释:更新test.zip压缩文件中的t3.txt文件,如果test.zip压缩文件存在该文件则会更新此文件;如果不存在则不会执行任何操作(即命令为无效命令)。

It should be noted that using the -f option only updates the specified files in the ZIP file, without adding new files or deleting files. If the file to be refreshed (updated) does not exist in the ZIP file, this option does nothing.

Here we are going to distinguish between zip -f and zip -u. Both are used to update existing ZIP files, but their behavior is slightly different. The differences are as follows:

zip -f:只更新已存在于ZIP文件中的文件,不会添加新文件。不会添加、不会添加、不会添加(重要的事情说三遍)。
zip -u:可以添加新文件并更新已存在的文件。

2.1.6、-m

-m option: used to move (archive) files into a ZIP compressed file and delete the source files after moving.

[root@centos test]# ls
a.txt  b.log  test01
[root@centos test]# zip -m test.zip a.txt b.log 
  adding: a.txt (stored 0%)
  adding: b.log (deflated 74%)
[root@centos test]# ls
test01  test.zip
[root@centos test]# 

2.1.7, -e (encryption)

-e option: used to encrypt ZIP files.
Example: zip -e test.zip a.txt b.log
Explanation: Compress a.txt and b.log into the compressed file test.zip and save it as an encrypted ZIP file.
Insert image description here

2.1.8, -z (add comments)

-z option: add comments to compressed files.
Command format: zip -z <Compressed file with added comments>

Insert image description here
注意:按回车可以继续输入,直到某一行单独出行了英文的.才会结束,如上图,注释后面有.但是没结束,直到单独输入了.之后才结束的

3. Unzip the file

The syntax of this command is as follows:

unzip [选项] 压缩包名 源文件或源目录列表

Common command options

参 数:
-c 将解压缩的结果显示到屏幕上,并对字符做适当的转换。
-f 更新现有的文件。
-l 显示压缩文件内所包含的文件。
-p 与-c参数类似,会将解压缩的结果显示到屏幕上,但不会执行任何的转换。
-t 检查压缩文件是否正确。
-u 与-f参数类似,但是除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中。
-v 执行是时显示详细的信息。
-z 仅显示压缩文件的备注文字。
-a 对文本文件进行必要的字符转换。
-b 不要对文本文件进行字符转换。
-C 压缩文件中的文件名称区分大小写。
-j 不处理压缩文件中原有的目录路径。
-L 将压缩文件中的全部文件名改为小写。
-M 将输出结果送到more程序处理。
-n 解压缩时不要覆盖原有的文件。
-o 不必先询问用户,unzip执行后覆盖原有文件。
-P<密码> 使用zip的密码选项。
-q 执行时不显示任何信息。
-s 将文件名中的空白字符转换为底线字符。
-V 保留VMS的文件版本信息。
-X 解压缩时同时回存文件原来的UID/GID。
-d<目录> 指定文件解压缩后所要存储的目录。
-x<文件> 指定不要处理.zip压缩文件中的哪些文件。
-Z unzip -Z等于执行zipinfo指令
unzip *.zip -d /目录

3.1. Decompress file example

Scenario 1: Unzip to the specified directory
. Unzip test.zip under /home to the /home/demo directory.

unzip /home/test.zip -d /home/demo

Scenario 2: View the files in the compressed package
unzip -l 命令只查看不解压。
Insert image description here

[root@centos test]# unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  09-14-2023 17:15   home/test/
       11  09-11-2023 23:32   home/test/a.txt
        0  09-14-2023 15:55   home/test/test01/
       30  09-11-2023 23:57   home/test/test01/c.txt
       23  09-11-2023 23:56   home/test/b.log
---------                     -------
       64                     5 files
[root@centos test]# 

4. Frequently Asked Questions

4.1. The command cannot be found in CentOS7

-Bash: Unzip: Command Not Found

Solution

# 安装zip /unzip
yum install -y unzip zip

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/132812358