25

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?

1
32

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 {} 
7
  • 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. Feb 1 '12 at 3:29
  • 6
    FTR this also deletes the original zip file
    – sjr
    Jun 14 '14 at 15:39
  • 3
    I tried this on an unzipped excel file (xlsx); got the following errors: rm: cannot remove 'Name': No such file or directory rm: unrecognized option '----' ...(many files removed)... rm: unrecognized option '-------' rm: cannot remove 'files': No such file or directory Not sure what happened there exactly; it also left a bunch of empty directories and empty directory tree.
    – jhaagsma
    Apr 28 '16 at 17:31
7
unzip -Z -1 <filename.zip> | xargs -I{} rm -v {}

Does the job because -Z invokes zipinfo utility and -1 option tells it to print only filenames

You can find more details about this through man unzip and man 1 zipinfo commands

1
  • 1
    This is cleaner than the accepted answer (e.g., it does not remove the original archive), but it fails to remove directories. I suppose you need to add -r to the rm command for that. Jan 8 '21 at 16:15
6

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

3

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.

The comments have improved this answer, if you want to ensure you remove hidden files too (which you probably do), do this

rm -r ` ls -A folder `
3
  • apologies for rushing to the incorrect syntax, can the vote down be removed? Aug 25 '14 at 13:13
  • 1
    If zip refused to overwrite any files, then the originals will be deleted. If any hidden files were unzipped, they will not be removed (unless you use ls -a). Feb 15 '19 at 16:04
  • 1
    Or rather, ls -A, to avoid errors from listing . and .. Apr 25 '19 at 16:18
0

To do this in Nautilus (without the command line), you can just navigate to where you accidentally extracted the files, sort the files by clicking on the "Modified" tab, then select and delete all of the files labelled with the exact time you ran the unzip command.

2
  • 1
    Please read the question again carefully. Your answer does not answer the original question. Unfortunately Linux does not include Windows Explorer.
    – DavidPostill
    Sep 26 '16 at 14:23
  • Ah, yep! It hadn't occurred to me that unzip wasn't a windows terminal command until just now. Thanks! I'll check to see if Nautilus has similar functionality and edit the answer. Otherwise, I'll just remove it. Sep 26 '16 at 14:58
-1
unzip -Z -1 /path/to/zip/file.zip | xargs -I{} rm -rf {}
1
  • 2
    While this may answer the question, it would be a better answer if you could provide some explanation why it does so.
    – DavidPostill
    Aug 10 '17 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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