Linux Zip 说明绝对路径,但只保留其中的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11249624/
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 07:07:39  来源:igfitidea点击:

Zip stating absolute paths, but only keeping part of them

linuxzip

提问by Hermann Ingjaldsson

zip -r 1.zip /home/username/the_folder

At here, when i unzip 1.zip, it will create /home/username/the_folder, from whichever folder i am unzipping from.

在这里,当我解压缩时1.zip,它将/home/username/the_folder从我解压缩的任何文件夹中创建。

How do I zip, stating the full absolute paths, but make the zip only contain the folder structure starting at, in this case for instance, /home/username?

我如何压缩,说明完整的绝对路径,但使 zip 只包含开始于的文件夹结构,例如,在这种情况下,/home/username

That way I could be at whatever path i wanted, unzip and it would just create the_folder, and not /home/username/the_folder.

这样我就可以在我想要的任何路径上,解压缩它只会创建the_folder,而不是/home/username/the_folder.

采纳答案by Hermann Ingjaldsson

Use this command:

使用这个命令:

cd path_under_folder_to_zip && \
zip -r 1.zip folder_to_zip >/dev/null && \
mv 1.zip my_current_path

回答by Tri

Use relative path when specifying the file to zip.

指定要压缩的文件时使用相对路径。

cd /home/username
zip -r 1.zip ./the_folder

Then when you unzip, it'll be a relative path starting at whichever folder you're in when unzipping.

然后当您解压缩时,它将是一个相对路径,从解压缩时所在的文件夹开始。

回答by Niraj Nawanit

  1. List item
  1. 项目清单

How about this:

这个怎么样:

function zipExtraFolder {
    if [ $# -lt 2 ]; then
        echo "provide at least two arguments"
        return
    fi
    folder=
    mkdir del
    echo cp -r `dirname $folder` del
    cd del
    echo zip -r ../ .
    cd -
    rm -rf del
}

Define above as a shell function in your .bashrc and you should be able to use it whenever you want. The usage will be like below.

将上面定义为 .bashrc 中的 shell 函数,您应该可以随时使用它。用法如下。

zipExtraFolder 1.zip /home/username/the_folder

回答by Hymanjr300

Just use the -joption, works on OSX, I don't know about linux.

只需使用该-j选项,适用于OSX,我不知道 linux。

zip -j -r 1.zip /home/username/the_folder

zip -j -r 1.zip /home/username/the_folder