Super User is a question and answer site for computer enthusiasts and power users. 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 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?

share|improve this question

migrated from stackoverflow.com Feb 1 '12 at 3:19

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

up vote 19 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 {} 
share|improve this answer
    
Fails if any files have spaces in their names... (+1 now with the -i edit. :) – sarnold Feb 1 '12 at 3:17
    
The problem is in the awk '{print $4}' -- a filename with spaces might be in $4 $5 $6 .... :) – sarnold Feb 1 '12 at 3:24
1  
Ok. Fixed that too. – Abhijeet Rastogi Feb 1 '12 at 3:29
    
Thanks very much, awk might be the best solution – hero2008 Feb 1 '12 at 5:14
3  
FTR this also deletes the original zip file – sjr Jun 14 '14 at 15:39

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

share|improve this answer

If you want to clean up your directory after accidentally unzipping without creating a folder first, you could create the folder, unzip to that and then:

rm -r ` ls folder `

" ` " is super useful for chaining commands but it's very literal.

share|improve this answer
    
apologies for rushing to the incorrect syntax, can the vote down be removed? – chrispepper1989 Aug 25 '14 at 13:13

You must log in to answer this question.

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