5

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?

4 Answers 4

11

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.

2
  • I don't think that this would work if there are spaces in any of the file names, though I can't think of a better answer off the top of my head... Aug 22, 2011 at 15:36
  • @StephenDarlington: you're right and what a pain that is to work around !
    – user9517
    Aug 22, 2011 at 16:13
5

Iain's answer threw a head-related error on my machine that I couldn't figure out... so I went ahead and wrote little BASH script that includes part of his solution, and that works quite nicely... just pass the original zip as an argument to this ununzip.sh script.. Comment the rm line to preview the "action".

#!/bin/bash
COUNT=0                                   # USAGE:
PURGE=CLEAR                               # chmod +x ununzip.sh && ./ununzip.sh file.zip
THATDARNzip=$1                            # THIS IS YOUR file.zip, THE ARGUMENT
PURGE=(`unzip -t THATDARNzip | awk '{print $2}' |  xargs echo`)
COUNT=${#PURGE[@]}                        # HOW MUCH STUFF GOT UNZIPPED?
echo "total items "$COUNT
echo -e "item at 0 is ${PURGE[0]}"        # WE DON'T DELETE THE ORIGINAL ZIP FILE
while [ "$COUNT" -gt 0 ]; do
echo -e "deleting ${PURGE[${COUNT}]}"
rm -r "${PURGE[${COUNT}]}"                # COMMENT THIS LINE FOR A DRY RUN
COUNT=$[ $COUNT - 1 ]
done
exit 0
3
  • Brain fade on my part - should have been a tail command.
    – user9517
    Aug 22, 2011 at 13:40
  • Oh and for the record, this can all be avoided by passing the -e $DIR option along with unzip. for example unzip -e folderInThisDir ZIPinThisFolder.zip
    – mralexgray
    Aug 22, 2011 at 14:46
  • On line 5, shouldn't it be unzip -t $THATDARNzip (missing dollar sign)?
    – Stardust
    Sep 11, 2021 at 16:46
3

I just manually delete the crud, whilst strenuously admonishing myself for not checking the zip file was sensibly formed (which they invariably aren't) with unzip -l first.

3

Try this:

$ unzip -l scrubbers.zip | sed '1,3d' | sed 'N;$!P;$!D;$d' | awk '{ print $4 }' | xargs rm -fr
2
  • 2
    It might work, but I'd be tempted never to /try/ anything with an rm -fr at the end. Aug 22, 2011 at 12:03
  • Just cut off the xargs: $ unzip -l scrubbers.zip | sed '1,3d' | sed 'N;$!P;$!D;$d' | awk '{ print $4 }'.
    – quanta
    Aug 22, 2011 at 13:35

You must log in to answer this question.

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