Linux – How to “undo” an “unzip” performed in terminal / CLI

linux

I know this is possible with a botched unTAR, although I can't recall the exact command…. but is it possible to "undo" a recent "unzip" command, in this case, using the mac terminal?

This is using /usr/bin/unzip on 10.7, with UnZip 5.52.

Lets say I started with a folder structure like this….

admin$ pwd
    /usr/local/src
admin$ ls
    pots            pans           scrubbers.zip
admin$ unzip scrubbers.zip
admin$ ls
    pots               pans           scrubbers.zip
    zebraturd.doc      README.md      morestuffididntmeantoputhere.py      cleanme.junkmail                 
    surfraw.2.html     scrubbers.explaination

how can i just "rezip" or parse the zip file in reverse?

there is a section in the zip file that sorta lists the files in the archive, although quite seriously mangled.. for example..

 0L'†C  0L'†C  !∏V≤B PK&Å‘:√@`¢îF3$ Lmacos/lib/libexiv2.2.1.0.dylib

without manually sifting through that list, is there a magic bullet for this?

Best Answer

You could try

unzip -t zipfile.zip | awk '{print $2}' | tail -n +2 | xargs echo

or

unzip -t zipfile.zip | awk '{print $2}' | sed 's/zipfile.zip//' | xargs echo

and if you have spaces in filenames

unzip -l zipfile.zip | tail -n +4 | head -n -2 | awk '{print "\""substr($0,index($0,$4))"\""}' | xargs rm

check that the output is sensible and then change echo for rm. In particular check that your zipfile isn't listed in the output. This isn't perfect as it will leave directories untouched but it may be easier than wading through it all by hand.