压缩当前目录中的所有内容


84

我想将所有文件(包括当前目录中的文件和文件夹)压缩并打包到Ubuntu上的单个ZIP文件中。

什么是最方便的命令(如果需要的话,还有需要安装的工具名称)?

编辑:如果我需要排除一个文件夹或多个文件怎么办?

Answers:


103

安装zip和使用

zip -r foo.zip .

您可以使用-0(无)至-9(最佳)标志来更改压缩率

可以通过该-x标志排除文件。从手册页:

-x files
--exclude files
          Explicitly exclude the specified files, as in:

                 zip -r foo foo -x \*.o

          which  will  include the contents of foo in foo.zip while excluding all the files that end in .o.  The backslash avoids the shell filename substitution, so that the name matching
          is performed by zip at all directory levels.

          Also possible:

                 zip -r foo foo -x@exclude.lst

          which will include the contents of foo in foo.zip while excluding all the files that match the patterns in the file exclude.lst.

          The long option forms of the above are

                 zip -r foo foo --exclude \*.o

          and

                 zip -r foo foo --exclude @exclude.lst

          Multiple patterns can be specified, as in:

                 zip -r foo foo -x \*.o \*.c

          If there is no space between -x and the pattern, just one value is assumed (no list):

                 zip -r foo foo -x\*.o

          See -i for more on include and exclude.

+1如果我想排除某些文件夹或文件怎么办?
Terry Li

1
@TerryLiYifeng我编辑了有关此信息的答案
SkaveRat 2011年

18

我猜想许多通过Google提出此问题的人在写“ zip”时都表示“存档和压缩”。zip格式的替代方法是tar

tar -czf copy.tar.gz whatever/

压缩的存档文件将是copy.tar.gz,内容将是文件夹中的所有内容。

 -c, --create
       create a new archive
 -z, --gzip, --gunzip --ungzip
 -f, --file ARCHIVE
       use archive file or device ARCHIVE

1
对于xz,选项为--J or --xz
Anwar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.