简体   繁体   English

用相对路径压缩文件?

[英]Zipping files with relative path?

I'm trying to create backups of my USB key.我正在尝试创建我的 USB 密钥的备份。 For that, I'd need to zip the content of my USB key ( /Volumes/<USB KEY NAME>/ ).为此,我需要压缩我的 USB 密钥( /Volumes/<USB KEY NAME>/ )的内容。 I have this code for the moment我现在有这个代码
zip -r /Volumes/<USB KEY NAME>/* That seems to work, except the fact that when I extract my archive, I get : zip -r /Volumes/<USB KEY NAME>/*这似乎有效,除了当我提取档案时,我得到:

(For simplfication purpose (and laziness), <USB KEY NAME>+(<DATE>).zip is Archive.zip ) (为了简化目的(和懒惰), <USB KEY NAME>+(<DATE>).zipArchive.zip

Archive.zip
    -> Volumes
        -> <USB KEY NAME>
            -> <USB KEY CONTENT>
                -> ...

How to get just :如何获得:

Archive.zip
    -> <USB KEY NAME>
        -> <USB KEY CONTENT>
            -> ...

I know it's something about absolute/relative paths but that's all I know.我知道这是关于绝对/相对路径的,但这就是我所知道的。 How could I do this ?我怎么能这样做?

PS : I'm on MacOS PS:我在 MacOS 上

Try using the -j option.尝试使用-j选项。

It stores just the name of the saved files (junk the path), and does not store directory names.它只存储保存文件的名称(垃圾路径),而不存储目录名称。 By default, zip will store the full path (relative to the current path).默认情况下, zip将存储完整路径(相对于当前路径)。

As man zip tell us,正如man zip告诉我们的那样,

-j --junk-paths Store just the name of a saved file (junk the path), and do not store directory names. -j --junk-paths 仅存储已保存文件的名称(垃圾路径),不存储目录名称。 By default, zip will store the full path (relative to the current directory) .默认情况下,zip 将存储完整路径(相对于当前目录)

However, -j will discard all path information, which caused all files be put in same package without any path information.但是, -j将丢弃所有路径信息,这导致所有文件都放在同一个包中而没有任何路径信息。

Method 1 Change current directory( cd )方法一改变当前目录( cd )

cd /Volumes; zip -rq $(OLDPWD)/Archive.zip <USB_Key_Name>; cd -

Method 2 Symlink will help方法 2符号链接会有所帮助

ln -s /Volumes/<USB_Key_Name> . # create symlink to avoid cd
zip -rq Archive.zip <USB_Key_Name>
rm <USB_Key_Name> # remove symlink

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM