Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zipping files with relative path?

Tags:

bash

path

macos

zip

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

like image 676
Blax Avatar asked Mar 10 '23 13:03

Blax


2 Answers

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

like image 117
mlapointe22 Avatar answered Mar 16 '23 01:03

mlapointe22


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
like image 20
DawnSong Avatar answered Mar 16 '23 01:03

DawnSong