How to "undo" an "unzip" performed in terminal / CLI?

Solution 1:

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.

Solution 2:

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

Solution 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.