40

I'm calling the zip command from a script where I cannot change directory. I need to make a zip file of the stuff in data/kit123/ from the directory which data resides in, but I want the contents of the zip to only be the contents of kit123, with paths relative to kit123.

This is the directory structure

myworkingdir
    data
        kit123
            kitpart1
                file.xcf
                anotherfile.xcf
            kitpart2
                ...
        kit124
            ...

My script runs in myworkingdir and cannot change directories.

If I call

zip -r kit123.zip data/kit123

then the structure in the zip file will be

data
    kit123
        kitpart1
            file.xcf
            anotherfile.xcf
        kitpart2

but I want it to be

kit123
    kitpart1
        file.xcf
        anotherfile.xcf
    kitpart2

Is there a zip option I can use to accomplish this? It seems odd that it should depend on my working directory

I know it's not -j. that one destroys the structure within kit123

2

1 Answer 1

31

As you note in the comments you now know how to change directory with your script, I'll explain the situation with zip regarding relative paths.

It is probably easiest to open terminal and cd to kit123; you have to make the target directory your working directory- you can't run the command from the base of your home directory or it will pick up all the paths of /home/$USER/.. If you use the -j option, it will strip out all paths, as there isn't really an equivalent of the tar option --strip with which different levels of path removal can be specified.

So please cd to your target directory (kit123) and enter:

zip -r kit123.zip *

This will recursively (-r) preserve all (*) files and directories of files in the current directory (kit123), and as relative paths (-p) are preserved automatically unless the -j option is present, the directory structure will be as you wished. Kit123 and subfolders (kitpart1) with their own files will be present:

kit123
    kitpart1
        file.xcf
        anotherfile.xcf
    kitpart2

You can check the contents of the zip file with unzip -l zipfile.zip.

For more information on the other options available for zip, see man zip and the Ubuntu manpage online.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .