unzip how to trim top directories

13

1

I have a zip archive. The unzip -l myarchive.zip command gives this listing:

top/subtop/files/1.txt
top/subtop/files2/2.txt
etc

The root folders are top/subtop for all files. How to extract those files without the two top/subtop folders?

E.g. when I am in /home/myuser/public_html directory, files/1.txt and files2/2.txt shoud be extracted directly to that directory.

I tried unzip myarchive.zip top/subtop/* but it created top/subtop directories anyway.

camcam

Posted 2012-07-01T22:14:17.493

Reputation: 529

Answers

8

Unfortunately, you can't. Your only real option is to just move them after they are extracted.

Sepero

Posted 2012-07-01T22:14:17.493

Reputation: 266

Note that it is possible to ignore all directories (simply placing all the files next to each other) with unzip -j. – Skylar Ittner – 2019-05-11T01:32:31.360

Hmm, good to know. I asked this because a few days ago, when creating an archive, it was adding some paths from above, unintentionally. So there was a problem unzipping them because of the additional top paths. However, I can't repeat this today - today it's working ok, i.e. when in top/subtop directory and doing zip -r files.zip * it does not add the top/subtop path as prefix to files path. I think I do exactly the same command, but strangely, results are different, so it is not necessary to strip top directories when unzipping. – camcam – 2012-07-05T06:12:21.963

4

You might want to try the tar command, which has a --strip-components feature and can operate on zip files.

Eamon

Posted 2012-07-01T22:14:17.493

Reputation: 41

1For debian users, FreeBSD's tar is available as bsdtar (apt-get install bsdtar) if you want to use tar to extract zip files. – Niklas B – 2017-05-09T19:55:47.970

2Does not work with tar (GNU tar) 1.15.1: tar: This does not look like a tar archive tar: Error exit delayed from previous errors – Kevin Panko – 2014-01-06T22:05:14.120

2FreeBSD's tar can handle ZIP-archives. Other tar-implementations aren't as full-featured, unfortunately. – Mikhail T. – 2014-04-25T20:30:55.043

0

I used this command to copy all files to my root file directory after Extracting All zipped files, it worked great. Huge time saver:

for /r %f in (00P*) do @copy "%f" 

(http://mlichtenberg.wordpress.com/2011/01/25/command-line-fun-how-to-flatten-a-folder-hierarchy/)

Kevin

Posted 2012-07-01T22:14:17.493

Reputation: 1

5You do realize this is a DOS command, and has nothing to do with a Linux command line, right? – MestreLion – 2015-08-31T11:11:36.687

-2

$ unzip -l myarchive.zip
$ mv top/subtop/* .

#kissPrinciple ;)

Romain DEQUIDT

Posted 2012-07-01T22:14:17.493

Reputation: 1