Is there an option on linux zip command such that I can hard code the target directory it unzips into. For example, I would like to zip ./mydir files into myzipfile.zip but I want the unzip command to create a specific directory "projx" and expand the files which were under ./mydir to it.

zip -r myzipfile.zip mydir (-option to expand into 'projx' when unzipped? )

There is no such option, at least not for Info-ZIP. You have to create a directory projx yourself, move/copy your files into it and then pass it to zip.

If the directory already exists then just pass option -d to unzip to specify the target directory for extraction.


To do that there is an another way. You can flatted the entire contents of the zip file using following method. Target directory is "projx" and execute following command.

cd /projx && unzip -j /path/to/myzipfile.zip

Option -j is called "junk paths" and just dumps each file into the current directory instead of extracting any directory structure.


I don't know about a specific command line option, but you can use the following commands to reach you goal:

ln -s mydir projx
zip -r myzipfile.zip projx
rm projx

The first command creates a symbolic link, so that you can access the files in mydir also via projx. Then using Info-ZIP (tested with version 2.31) the symbolic link is dereferenced (this is the default, unless you specify -y) and the files are stored recursively:

adding: projx/ (stored 0%)
adding: projx/foo.1 (deflated 23%)
adding: projx/bar.2 (deflated 73%)

You see, the folder name is stored in the file, too -- so upon extraction the directory projx is created.

However, I can't think of a possibility to store absolute paths, but this is a bad idea anyway.

  • Absolute path only works on MacOS by specifying option -jj. On other operating systems only option -j is supported (and -jj is interpreted as -j) which removes any leading path and thus leading to the opposite (a very clever design...). – scai Jul 10 '13 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.