首页·终端命令

zip

压缩与传输

用法

zip -r <archive.zip> <path>

和 tar 不同,它自己就带压缩,也是 Windows 不装任何东西就能打开的那种格式;装目录必须加 `-r`,否则只把目录这一条记进去、里面的东西一个也没有,而 `-e` 设的密码是老式 ZipCrypto,很弱。

常用选项

选项含义
-rRecurse into directories. Without it you archive the directory entry and nothing inside.
-x '*/node_modules/*'Exclude a pattern. Quote it so the shell leaves it alone.
-ePrompt for a password. This is legacy ZipCrypto, which is weak.
-1 / -9Fastest / smallest.
-jStore bare file names and drop the directory structure.
-uAdd only files that are new or changed.
-s 100mSplit the archive into 100 MB volumes.

示例

zip -r site.zip site -x '*/node_modules/*'

The archive a Windows colleague can open, without the dependency tree.

zip -j photos.zip ~/Pictures/*.jpg

One flat archive of files from several places.

打包和压缩是两件事:tar 打包,gzip 压小,.tar.gz 是两样一起。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. zip 是做什么的?

和 tar 不同,它自己就带压缩,也是 Windows 不装任何东西就能打开的那种格式;装目录必须加 `-r`,否则只把目录这一条记进去、里面的东西一个也没有,而 `-e` 设的密码是老式 ZipCrypto,很弱。

Q. 怎么写?

zip -r <archive.zip> <path> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man zip。这条命令属于压缩与传输。

相关命令

man 手册: man zip