I used "unzip XXX.zip" to extract a zip file, unfortunaly, i make a mistake.

Now i want to delete all the file and directorys generated by "unzip".
How can I undo it?

link|improve this question
feedback

migrated from stackoverflow.com Feb 1 at 3:19

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 6 down vote accepted

use this:

unzip -l filename |  awk 'BEGIN { OFS="" ; ORS="" } ; { for ( i=4; i<NF; i++ ) print $i " "; print $NF "\n" }' | xargs -I{} rm -v {}

Use this if you are skeptical (will prompt for confirmation)

unzip -l filename |  awk 'BEGIN { OFS="" ; ORS="" } ; { for ( i=4; i<NF; i++ ) print $i " "; print $NF "\n" }' | xargs -I{} rm -iv {} 
link|improve this answer
Fails if any files have spaces in their names... (+1 now with the -i edit. :) – sarnold Feb 1 at 3:17
The problem is in the awk '{print $4}' -- a filename with spaces might be in $4 $5 $6 .... :) – sarnold Feb 1 at 3:24
1  
Ok. Fixed that too. – shadyabhi Feb 1 at 3:29
Thanks very much, awk might be the best solution – hero2008 Feb 1 at 5:14
feedback

You're in a rough spot; the standard zipinfo(1) utility doesn't provide any mechanism to get unambiguous filenames out of an archive. But, you can come close:

zipinfo -1 /path/to/zip/file.zip | xargs -d '\n' rm -i

If you're sure none of the files have newlines in them, you can remove the -i option to rm(1) (which will surely get tedious).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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