Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

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.

share|improve this 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.

share|improve this answer
    
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... –  Stephen Darlington Aug 22 '11 at 15:36
    
@StephenDarlington: you're right and what a pain that is to work around ! –  Iain Aug 22 '11 at 16:13

Try this:

$ unzip -l scrubbers.zip | sed '1,3d' | sed 'N;$!P;$!D;$d' | awk '{ print $4 }' | xargs rm -fr
share|improve this answer
2  
It might work, but I'd be tempted never to /try/ anything with an rm -fr at the end. –  EightBitTony Aug 22 '11 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 '11 at 13:35
up vote 5 down vote accepted

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
share|improve this answer
    
Brain fade on my part - should have been a tail command. –  Iain Aug 22 '11 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 '11 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.