创建zip - 忽略目录结构

Jak*_*ake 206 linux zip

我需要使用该命令创建一个zip

zip /dir/to/file/newZip /data/to/zip/data.txt
Run Code Online (Sandbox Code Playgroud)

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

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

Lar*_*off 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).
Run Code Online (Sandbox Code Playgroud)

  • 如果`-j`不适用于你的目录(连同`-r`)checkout [this answer](http://superuser.com/a/119661/45285) (18认同)

Dan*_* D. 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).
Run Code Online (Sandbox Code Playgroud)


Vik*_*iya 29

使用-j不会与-r选项一起使用.
所以它的解决方法可以是:

cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;
Run Code Online (Sandbox Code Playgroud)

或者是在线版本

cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -
Run Code Online (Sandbox Code Playgroud)

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


fla*_*aky 18

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

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


And*_*ewD 8

保留父目录,以免unzip文件到处乱放

\n

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

\n

因此,为了避免保留所有路径,并且由于您不能一起使用 -j 和 -r (您会收到错误),您可以这样做:

\n
cd path/to/parent/dir/;\nzip -r ../my.zip "../$(basename "$PWD")"\ncd -;\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

现在unzip my.zip将给出一个包含所有文件的文件夹:

\n
parent-directory\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file2\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir1\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file3\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file4\n
Run Code Online (Sandbox Code Playgroud)\n

不要将解压缩的文件散布在当前目录中:

\n
file1\nfile2\ndir1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file3\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file4\n
Run Code Online (Sandbox Code Playgroud)\n

  • 然而,在 ZIP 存档中会创建一个额外不需要的父目录“/../”,例如“/../parent/sub/” (2认同)

Mir*_*chi 5

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

ln -s /data/to/zip/data.txt data.txt
zip /dir/to/file/newZip !$
rm !$
Run Code Online (Sandbox Code Playgroud)

这也适用于目录。