how to specify the unzip target directory

21

2

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

seedhom

Posted 2013-07-10T06:10:40.417

Reputation: 313

On Unix: http://unix.stackexchange.com/questions/12686/unzip-to-a-specific-folder

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-01-12T09:16:56.403

Answers

18

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.

scai

Posted 2013-07-10T06:10:40.417

Reputation: 882

4

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.

Chaminda Bandara

Posted 2013-07-10T06:10:40.417

Reputation: 415

0

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.

mpy

Posted 2013-07-10T06:10:40.417

Reputation: 20 866

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 – 2013-07-10T08:00:27.207