Zipping files with relative path?
Asked Answered
S

3

15

I'm trying to create backups of my USB key. For that, I'd need to zip the content of my USB key (/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 :

(For simplfication purpose (and laziness), <USB KEY NAME>+(<DATE>).zip is Archive.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

Spiller answered 21/3, 2017 at 18:8 Comment(0)
C
13

Try using the -j option.

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

Chiliad answered 21/3, 2017 at 18:16 Comment(2)
I tried but it gives me an error, saying that I have to files with the same name, and that it is propably because I have the -j option, so what can I do?Spiller
Try changing your working directory into the USB drive prior to zipping your files. Include the original -r option but not the -j option. So: cd /Volumes/<USB KEY NAME>/ then zip -r *Chiliad
A
6

As man zip tell us,

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

However, -j will discard all path information, which caused all files be put in same package without any path information.

Method 1: Change current directory(cd)

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

Method 2: Symlink will help

ln -s /Volumes/<USB_Key_Name> . # create symlink to avoid cd
zip -rq Archive.zip <USB_Key_Name>
rm <USB_Key_Name> # remove symlink
Agamemnon answered 18/5, 2022 at 1:49 Comment(0)
R
0

just control path to the target archive

cd /Volumes/<USB KEY NAME>;
zip -r ../../archive.zip *

  adding: ./file-in-usb-key-name-dir (stored 0%)
  adding: ./subdir/file (stored 0%)
  ...
Redden answered 31/8, 2023 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.