Macos – Terminal unzip outside default directory

macosshellzip

Using terminal command "unzip" seems to be extremely useful when deploying a CMS to a remote server, since you can transfer a zip and unzip it remotely. However, I cannot find a way to unzip the all files directly to the root of the directory the .zip-file resides in. By default it creates a folder with the same name as the .zip-file and extracts the files to this folder, after which I need to do a mv command to move the contents of the folder to the root.

Can I somehow do this in one step using unzip?

Best Answer

The specific answer to your question is no. The unzip that ships with OS X has no way to change directories in the zip on the fly. One thing to note however, is that zip is not 'creating the directory'. It is part of the archive. A properly created zip archive will not have this issue if you are controlling the creation process:

$ ls -l
total 0
-rw-r--r--  1 bsmith  staff  0 Sep 30 22:01 b
-rw-r--r--  1 bsmith  staff  0 Sep 30 22:01 c
$ zip -r a.zip .
  adding: b (stored 0%)
  adding: c (stored 0%)
$ cd ../b
$ unzip ../a/a.zip
Archive:  ../a/a.zip
 extracting: b                       
 extracting: c                       
$ ls -l
total 0
-rw-r--r--@ 1 bsmith  staff  0 Sep 30 22:01 b
-rw-r--r--@ 1 bsmith  staff  0 Sep 30 22:01 c
Related Question