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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9710141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:11:46  来源:igfitidea点击:

Create zip file and ignore directory structure

linuxzip

提问by Jake

I need to create a zip file using this command:

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

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

This works, but the created zip file creates a directory structure mimicking the directory to the raw file. It is a lot of extra folders that I don't need.

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

I didn't find an answer in a cursory glance over the man page or a Google hunt.

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

采纳答案by Lars Kotthoff

You can use -j.

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

回答by Dan D.

Use the -joption:

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

回答by flaky

Somewhat related - I was looking for a solution to do the same for directories. Unfortunately the -joption does not work for this :(

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

Here is a good solution on how to get it done: https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file

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

回答by MirkoBanchi

Alternatively, you could create a temporary symbolic link to your file:

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

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

This works also for a directory.

这也适用于目录。

回答by Vikas Tawniya

Using -jwon't work along with the -roption.
So the work-around for it can be this:

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

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

Or in-line version

或者内联版本

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

you can direct the output to /dev/nullif you don't want the cd -output to appear on screen

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

回答by Qader Dorosti

Just use the -jrmoption to remove the file and directory structures

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

zip -jrm /path/to/file.zip /path/to/file

回答by AndrewD

Retain the parent directory so unzipdoesn't spew files everywhere

保留父目录,以免unzip文件随处可见

When zipping directories, keeping the parent directory in the archive will help to avoid littering your current directory when you later unzip the archive file

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

So to avoid retaining all paths, and since you can't use -j and -r together ( you'll get an error ), you can do this instead:

因此,为了避免保留所有路径,并且由于您不能同时使用 -j 和 -r(您会收到错误),您可以改为执行以下操作:

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

The ../$(basename $PWD)is the magic that retains the parent directory.

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

So now unzip my.zipwill give a folder containing all your files:

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

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

Instead of littering the current directory with the unzipped files:

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

file1
file2
dir1
├── file3
├── file4