Unix 压缩子目录不包括父目录

发布于 2024-12-16 12:11:05 字数 375 浏览 0 评论 0原文

我想压缩一个子目录并将其发送到其他地方,我遇到的问题是当我使用 zip 命令时,它还包含我不想要的所有不需要的目录层。

例如

zip -r /new.zip /Unwanted1/Unwanted2/wanted1/wanted2/file.txt

###unzipped produces the following
/Unwanted1/Unwanted2/wanted1/wanted2/file.txt

###I want
/wanted1/wanted2/file.txt

更新:我想要压缩的目录有很多嵌套目录,因此 -r。另外,处理绝对路径也很棒,旧的解决方案用于 cd 有关该位置的信息,但我不太喜欢它。也许这是最好的(唯一的)方法。

I want to zip a sub directory and send it elsewhere, the problem I have is when I use the zip command it also includes all the unwanted layers of directories I do not want.

e.g.

zip -r /new.zip /Unwanted1/Unwanted2/wanted1/wanted2/file.txt

###unzipped produces the following
/Unwanted1/Unwanted2/wanted1/wanted2/file.txt

###I want
/wanted1/wanted2/file.txt

update: The directory I want to zip has lots of nested directories, hence the -r. Also it would be great to deal with absolute paths, the old solution used to cd about the place but I just didn't really like it. Perhaps that is the best (only) way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少钕鈤記 2024-12-23 12:11:05

您必须将目录更改为您想要开始的位置,或者可能有一个“压缩”选项来为您执行此操作。如果您像我一样懒得阅读手册页中的无数选项,那么只需先“cd”,即使在脚本中也是如此。或者,您可以将整个内容括在括号中,而不会影响您的 shell 或脚本的工作目录:

(cd Unwanted1/Unwanted2; zip -r ~/new.zip ./wanted/file.txt)

或者,由于您使用的是 -r,您可能想要的不仅仅是 file.txt,所以:

(cd Unwanted1/Unwanted2; zip -r ~/new.zip wanted)

或者使用 Pushd/popd 跳入/跳出:

pushd Unwanted1/Unwanted2
zip -r ~/new.zip wanted
popd

这在 sh/bash/csh/tcsh 中工作。

You have to change directories to wherever you want to start, or maybe there's an option to 'zip' to do that for you. If you're too lazy to read through the gazillion options in the man page, like I am, just 'cd' first, even in a script. Or you can enclose the whole thing in parenthesis without affecting your shell or script's working directory:

(cd Unwanted1/Unwanted2; zip -r ~/new.zip ./wanted/file.txt)

Or since you're using -r you presumably want more than just file.txt, so:

(cd Unwanted1/Unwanted2; zip -r ~/new.zip wanted)

Or use pushd/popd to jump in/out:

pushd Unwanted1/Unwanted2
zip -r ~/new.zip wanted
popd

This works in sh/bash/csh/tcsh.

抱猫软卧 2024-12-23 12:11:05

最简单、惯用的方式:

(cd ./Unwanted1/Unwanted2 && zip -r ../../new.zip ./wanted/file.txt)

现在 zip、tar、rar、cpio 和其他工具都有选项来管理相同的内容,但它们可能因版本而异,当然也因存档实用程序而异。我建议您学习使用 shell 来完成工作,并且您可以使用您想要的任何存档格式

另一种方式:

(cd ./Unwanted1/Unwanted2 && find . -name "*.txt" -print) | zip new.zip -@

Simplest, idiomatic way:

(cd ./Unwanted1/Unwanted2 && zip -r ../../new.zip ./wanted/file.txt)

Now zip, tar, rar, cpio and friends all have options to manage the same, but they may differ from version to version and certainly from archive utility to archive utility. I suggest you learn to work with the shell to do the work, and you can use any archive format you desire

Another way:

(cd ./Unwanted1/Unwanted2 && find . -name "*.txt" -print) | zip new.zip -@
嘦怹 2024-12-23 12:11:05

好吧,一种简单的方法可能是

cd ./Unwanted1/Unwanted2
zip -r ../../new.zip ./wanted/file.txt

但我不知道你的情况是什么。也许您需要从基本目录运行?当您想要递归遍历目录时,还可以使用“-r”选项 - 您提供的示例使用一个文件。

Well one simple way around might be to do

cd ./Unwanted1/Unwanted2
zip -r ../../new.zip ./wanted/file.txt

But I don't know what your situation is. Perhaps you're required to run from the base directory? Also the "-r" option is used when you want to recursively traverse directories - the example you provided is using one file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文