Skip to content

最常用的基础命令

压缩:基于文件或目录制作.zip包

// 文件
zip file.zip file
//目录
zip -r dir.zip dir

解压.zip文件

unzip file.zip -d path/distDir
unzip dir.zip -d path/distDir

高级命令

zip

# zip -h
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help
// zip -d命令,比如从dq.zip中删除 dq.html文件
zip -d dq.zip dq.html

// 往dq.zip压缩包中,新增文件test.sh ,使用的命令
zip -g dq.zip test.sh

// 压缩并忽略文件
zip -r test.zip /path --exclude "/path/test01/*"

// 如果test.zip压缩文件中没有t3.txt,则test.zip中会增加新文件t3.txt;如果有t3.txt,则会替换test.zip压缩文件中的旧t3.txt为新的t3.txt
zip -u test.zip t3.txt


// 用于刷新(更新)现有ZIP文件中的指定文件
zip -f test.zip t3.txt
更新test.zip压缩文件中的t3.txt文件,如果test.zip压缩文件存在该文件则会更新此文件;如果不存在则不会执行任何操作(即命令为无效命令)

// m选项:用于移动(归档)文件到一个ZIP压缩文件中,并在移动后将源文件删除
# zip -m test.zip a.txt b.log 
  adding: a.txt (stored 0%)
  adding: b.log (deflated 74%)
# ls
test01  test.zip


// -e(加密)压缩a.txt和b.log到压缩文件test.zip中,并将其保存为加密的ZIP文件
zip -e test.zip a.txt b.log
# ll
总用量 287732
-rw-r--r--. 1 root root         0 4月  15 14:02 a.txt
-rw-r--r--. 1 root root         0 4月  15 14:02 b.txt
-rw-r--r--. 1 root root 294636324 4月  15 13:51 sonarqube-9.9.4.87374.zip
# zip -e test.zip a.txt b.txt 
Enter password: 
Verify password: 
  adding: a.txt (stored 0%)
  adding: b.txt (stored 0%)
# ls
a.txt  b.txt  sonarqube-9.9.4.87374.zip  test.zip
# unzip test.zip 
Archive:  test.zip
[test.zip] a.txt password: 
password incorrect--reenter: 
replace a.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace b.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n


// -z选项:为压缩文件添加注释
// 按回车可以继续输入,直到某一行单独出行了英文的.才会结束,如上图,注释后面有.但是没结束,直到单独输入了.之后才结束的
# ls
a.txt  b.txt  test.zip
# unzip test.zip 
Archive:  test.zip
this is test add #
replace a.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: N

选项 作用
-r 递归地将一个目录及其所有子目录和文件压缩到ZIP文件中
-q 在压缩文件时启用静默模式,即不显示压缩过程的详细信息
-d 从现有的ZIP文件中删除指定的文件或目录
-u 用于更新现有的ZIP文件,将新的文件或修改后的文件添加到ZIP存档中
-f 用于刷新(更新)现有ZIP文件中的指定文件
-m 用于移动(归档)文件到一个ZIP压缩文件中,并在移动后将源文件删除
-e 用于对ZIP压缩文件进行加密
-z 为压缩文件添加注释

若命令不存在需安装

Linux-CentOS-7

// 检查
yum list installed | grep zip
// 安装
yum -y install zip unzip