Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
admin$ ls
    pots            pans 
admin$ unzip
admin$ ls
    pots               pans 
    zebraturd.doc      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

You could try

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


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

and if you have spaces in filenames

unzip -l | 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
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 script.. Comment the rm line to preview the "action".

COUNT=0                                   # USAGE:
PURGE=CLEAR                               # chmod +x && ./
THATDARNzip=$1                            # THIS IS YOUR, 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 ]
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 – mralexgray Aug 22 '11 at 14:46

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

Try this:

$ unzip -l | sed '1,3d' | sed 'N;$!P;$!D;$d' | awk '{ print $4 }' | xargs rm -fr
share|improve this answer
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 | sed '1,3d' | sed 'N;$!P;$!D;$d' | awk '{ print $4 }'. – quanta Aug 22 '11 at 13:35

Your Answer


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.