创建zip文件并忽略目录结构

问题描述 投票:206回答:6

我需要使用此命令创建一个zip文件:

zip /dir/to/file/newZip /data/to/zip/data.txt

这有效,但是创建的zip文件创建了将目录模仿为原始文件的目录结构。我不需要很多额外的文件夹。

我在手册页或Google搜索中没有粗略地找到答案。

linux zip
6个回答
333
投票

您可以使用-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).

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

29
投票

使用-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 -


18
投票

有点相关-我正在寻找一种解决方案,以对目录执行相同的操作。不幸的是-j选项不适用于此:(

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


2
投票

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

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

这也适用于目录。


0
投票

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

zip -jrm /path/to/file.zip /path/to/file
© www.soinside.com 2019 - 2024. All rights reserved.