创建zip - 忽略目录结构

时间:2012-03-14 20:56:25

标签: linux zip

我需要使用命令

创建一个zip
zip /dir/to/file/newZip /data/to/zip/data.txt

这样可行,但创建的zip文件会创建一个目录结构,模仿原始文件的目录。这是我不需要的许多额外文件夹。

我粗略地浏览了这个手册或谷歌搜索,但没有找到答案。

7 个答案:

答案 0 :(得分:308)

您可以使用-j

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

答案 1 :(得分:32)

使用-j选项:

   -j     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 path).

答案 2 :(得分:24)

使用-j无法与-r选项一起使用 因此可以解决这个问题:

cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;

或在线版

cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -

如果您不希望/dev/null输出显示在屏幕上,则可以将输出定向到cd -

答案 3 :(得分:17)

有点相关 - 我正在寻找一个解决方案来为目录做同样的事情。 不幸的是-j选项对此不起作用:(

这是一个很好的解决方案,如何完成它: https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file

答案 4 :(得分:2)

或者,您可以创建一个指向您文件的临时符号链接:

ln -s /data/to/zip/data.txt data.txt
zip /dir/to/file/newZip !$
rm !$

这也适用于目录。

答案 5 :(得分:2)

保留父目录,以使unzip不会在任何地方喷出文件

在压缩目录时,将父目录保留在归档中将有助于避免在以后解压缩归档文件时乱抛当前目录

因此,为避免保留所有路径,并且由于不能同时使用-j和-r(会出现错误),因此可以改为:

cd path/to/parent/dir/;
zip -r ../my.zip ../$(basename $PWD)
cd -;

../$(basename $PWD)是保留父目录的魔力。

因此,unzip my.zip现在将提供一个包含所有文件的文件夹:

parent-directory
├── file1
├── file2
├── dir1
│   ├── file3
│   ├── file4

不是用解压缩的文件乱扔当前目录:

file1
file2
dir1
├── file3
├── file4

答案 6 :(得分:0)

只需使用-jrm选项即可删除文件和目录 结构

zip -jrm /path/to/file.zip /path/to/file
相关问题