Unix 压缩子目录不包括父目录 [英] Unix zipping sub directory not including parent directory

查看:37
本文介绍了Unix 压缩子目录不包括父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如

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 关于这个地方,但我只是不太喜欢它.也许这是最好(唯一)的方式.

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.

推荐答案

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

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)

或者因为您使用的是 -r,您可能想要的不仅仅是 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)

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

Or use pushd/popd to jump in/out:

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

这适用于 sh/bash/csh/tcsh.

This works in sh/bash/csh/tcsh.

这篇关于Unix 压缩子目录不包括父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆