14

I have a file called All CRGs.zip and I want to unzip its contents into a directory called data (which already exists and has some other files in).

Is this possible? I'm working on OSX.

I've tried:

$ unzip "All CRGs.zip" -d data/

But this unzips everything into data/All CRGs which is not what I'm looking for. I'd like everything to go straight into data.

Is this possible?

2
  • I'm sure it would have been easier to just double-click it, let it extract to it default location [normally right next to the .zip] then drag the contents over to the new location.
    – Tetsujin
    Apr 10, 2015 at 11:09
  • I think you can change data/ to simply data and it will fix the issue. Dec 2, 2022 at 21:30

4 Answers 4

8

You can either recreate the complete folder-structure including the All CRGs-Folder or you can ommit all folders inside the ZIP-file by using the -j-flag for the unzip-command.

The problem is that the ZIP-file has been created using the All CRGs-Folder as top-level like zip "All CRGs.zip" "All CRGs". The correct way would have been zip "All CRGs.zip" "All CRGs/*"which would have created a ZIP-Archive of all the files and folders inside the All CRGs-folder without the surrounding folder.

So the only way to extract only the files by retaining the folder-structure would be something like this:

unzip "All CRGs.zip" -d data/ && mv "data/All CRGs/*" "data/" && rmdir "data/All CRGs"

It will unzip the complete folder and after that move the content of the folder up one level and finaly remove the (now empty) "All CRGs"-folder.

4
  • "The correct way would have been..." doesn't match with my expectation. I tried creating a zip file using that syntax and I get "name not matched". If I take All CRGs/* out of the quotes, the files get added to the zip, but with the parent directory present. Jun 22, 2017 at 18:54
  • There have been at least 2 new Versions of OS-X (macOS) since that question was answered. So there might have been a change. What Version of macOS are you working on? Jun 22, 2017 at 19:02
  • Good point. 10.12.5 is the latest version and the version I was using at the time of this comment. Jun 22, 2017 at 19:04
  • the mv command will not move the hidden files. an additional mv "data/All CRGs/.*" "data/" is required. Dec 7, 2017 at 4:01
2

Since you know the zip file contains an unwanted top-level folder, and since you know the name of that top level folder, you can use a symlink to cause all the contents of that folder to appear in the parent like so:

ln -s . 'data/All CRGs'
unzip 'All CRGs.zip' -d data

The ln step causes the folder data/All CRGs to be created, linking to the current directory (relative to data/), which is data/. Then, when you extract files from All CRGs.zip and the unzip command tries to create data/All CRGs/file.dat, that file will get created as data/./file.dat.

This technique can be demonstrated without a zip file using touch:

$ mkdir data
$ ln -s . data/subdir     
$ touch data/subdir/foo.txt
$ ls data
foo.txt subdir

You can use this trick too to cause certain files or folders to be extracted to an alternate folder:

ln -s /tmp data/subdir2

Then anything in the archive being extracted to subdir2 will appear in /tmp.

0

you can use Keka, free on mac. Click on the ZIP and choose the directory to extract into

0

The default behavior for MacOS unzip command is to unzip the file into the current directory. From man unzip:

The default behavior (with no options) is to extract into the current directory (and subdirectories below it) all files from the specified ZIP archive.

So if you cd data and run unzip "All CRGs.zip" the contents of the zip file will be extracted into the data folder. Unzip will prompt you for any name collisions before overwriting.

You must log in to answer this question.

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