… _linux-beginner-zip:
Linux zip
命令的功能是用于压缩文件,解压命令为unzip
。
通过zip
命令可以将很多文件打包成.zip格式的压缩包,里面会包含文件的名称、路径、创建时间、上次修改时间、权限等信息,与tar
命令相似。
对于类似文本文件而言,压缩比基本可以达到2:1到3:1.
官方的定义为:
zip - package and compress (archive) files
zip的参数超级的多,各种设置也是很巧妙,不过常用的就几个,其他的放在后面再说。
$ zip [-aABcdDeEfFghjklLmoqrRSTuvVwXyz!@$] [--longoption ...] [-b path] [-n suffixes] [-t date] [-tt date] [zipfile [file ...]] [-xi list]
-r
或 --recurse-paths
:遍历整个目录 for example:-j
或 --junk-paths
: 仅保存文件名,而不保存路径。默认情况下zip将保存整个路径将指定目录及其内全部文件都打包成zip格式压缩包文件:
$ zip -r backuptest.zip /etc
adding: etc/resolv.conf (stored 0%)
adding: etc/fonts/ (stored 0%)
adding: etc/fonts/conf.d/ (stored 0%)
adding: etc/fonts/conf.d/65-0-wqy-zenhei-sharp.conf (deflated 88%)
adding: etc/fonts/conf.d/59-liberation-mono.conf (deflated 57%)
adding: etc/fonts/conf.d/57-paratype-pt-sans.conf (deflated 66%)
adding: etc/fonts/conf.d/59-liberation-sans.conf (deflated 74%)
adding: etc/fonts/conf.d/65-0-ttf-arphic-uming.conf (deflated 87%)
adding: etc/fonts/conf.d/65-1-vlgothic-gothic.conf (deflated 74%)
adding: etc/fonts/conf.d/65-0-lohit-nepali.conf (deflated 57%)
adding: etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf (deflated 43%)
adding: etc/fonts/conf.d/61-urw-d050000l.conf (deflated 71%)
adding: etc/fonts/conf.d/57-dejavu-sans.conf (deflated 77%)
...
zip
还支持通配符的表达,比如将当前工作目录内所有以.jpg为后缀的文件打包:
$ zip -r backuptest.zip *.jpg
adding: test0.jpg (deflated 56%)
adding: test10.jpg (deflated 56%)
adding: test12.jpg (deflated 56%)
adding: test14.jpg (deflated 56%)
adding: test16.jpg (deflated 56%)
adding: test18.jpg (deflated 56%)
adding: test20.jpg (deflated 56%)
adding: test2.jpg (deflated 56%)
adding: test22.jpg (deflated 56%)
adding: test24.jpg (deflated 56%)
adding: test26.jpg (deflated 56%)
adding: test28.jpg (deflated 56%)
zip
默认情况下会保存路径信息,如果加上-j将不保存路径信息,而仅仅保存文件。
这个情况可以在希望把同一类文件放在一起时比较有用。
$ zip -j foo foo/*