在命令行中压缩目录中的所有内容,但不将任何目录作为根目录包含。

46

我真的找不到答案。由于我正在为构建过程打包zip文件并以特定方式进行操作,因此我不想在生成的zip根目录中包含任何文件夹。例如,如果我的文件路径是:

MyFolder/
    A.png
    B.txt
    C.mp3

我使用以下任一命令:

zip -r -X "MyFolder.zip" MyFolder/*
或者
cd MyFolder; zip -r -X "../MyFolder.zip" *

我最终得到了一个名为MyFolder的根元素zip文件。我希望在解压时将其全部直接释放到目录中,就像这样:

A.png
B.txt
C.mp3

换句话说,我不希望"MyFolder"或其他任何文件夹成为根目录。我已经仔细阅读了整个手册,并尝试了许多选项和大量的谷歌搜索,但zip似乎只想在根目录下有一个文件夹。

谢谢!


1
第二个测试结果不符合您的预期,这让您感到惊讶。我在这里进行了测试,正如我所预期的那样,它不会在路径中包含“MyFolder”。 - jcaron
是的,在Mac OS X上,如果我解压缩它,它会解压出一个名为“MyFolder”的文件夹,其中包含所有内容,而不是像期望的那样将所有内容倒入该目录中。 - Eli
你确定没有混淆生成的zip文件吗?还要记住,对现有存档运行zip会添加更多文件,而不是创建新存档。 - jcaron
1
zipinfo看起来没问题!天哪,问题出在Mac OS X上(使用Archive Utility从Finder打开zip文件)。在命令行中使用unzip能如预期般工作。感谢对我的支持! - Eli
我差点尝试了一下,但最终没有这么做,因为我认为如果文件夹不存在的话,它可能找不到文件夹的名称。不过,我猜它会使用压缩文件的名称作为文件夹的名称吧? - jcaron
显示剩余2条评论
4个回答

57

这是归档实用程序(一个 Mac OS X 解压缩应用程序)的问题。当我使用命令行的解压缩命令时,它可以正常工作。

(cd MyFolder && zip -r -X "../MyFolder.zip" .)

4
我对你的解决方案进行了调整,这样你就不需要实际离开当前目录,只需在代码中加入 () 并将 ; 改为 && 即可,例如:(cd MyFolder && zip -r -X ../MyFolder.zip .) - Samuel Mburu
1
谢谢!看起来很不错。我会编辑我的帖子。我想你也可以: - Eli

19

我偶然看到了这个答案,但不想要在目录之间来回更改。我发现-j选项很有用,它可以将所有文件添加到ZIP根目录中。请注意,这是所有文件,子目录结构将不会被保留。

因此,对于以下文件夹结构:

MyFolder
 - MyFile1
 - MySubFolder
   - MyFile2

还有这个命令:

zip -rj MyFolder.zip MyFolder

你得到了这个:

MyFolder.zip
 - MyFile1
 - MyFile2

5
我发现使用Mac终端应用程序可以很容易地从文件夹中创建加密zip文件。以下是终端命令:
zip -j -e wishedname.zip yourfolder/*

就是这样,享受吧!

*

有关在终端应用程序中使用zip命令的更多信息

man zip

-j-e是什么?

-j
--junk-paths
Store  just  the  name  of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).

-e
--encrypt
Encrypt  the contents of the zip archive using a password which is entered on the terminal in response to a prompt (this will not be echoed; if standard error is not a tty, zip will  exit with an error).  The password prompt is repeated to save the user from typing errors.

-1

安装zip

sudo apt install zip

使用zip

zip -r foo.zip .

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

通过-x标志可以排除文件。来自man页面:

-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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接