34

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

7 Answers 7

36

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 {} 
8
  • Fails if any files have spaces in their names... (+1 now with the -i edit. :)
    – sarnold
    Commented Feb 1, 2012 at 3:17
  • The problem is in the awk '{print $4}' -- a filename with spaces might be in $4 $5 $6 .... :)
    – sarnold
    Commented Feb 1, 2012 at 3:24
  • 1
    Ok. Fixed that too. Commented Feb 1, 2012 at 3:29
  • 8
    FTR this also deletes the original zip file
    – sjr
    Commented Jun 14, 2014 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
    Commented Apr 28, 2016 at 17:31
13
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. Commented Jan 8, 2021 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).

2

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 `
5
  • apologies for rushing to the incorrect syntax, can the vote down be removed? Commented Aug 25, 2014 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). Commented Feb 15, 2019 at 16:04
  • 1
    Or rather, ls -A, to avoid errors from listing . and .. Commented Apr 25, 2019 at 16:18
  • I'd add the recursive list when the archive you extracted contained subfolders and you extracted all the files from there without keeping the folder structure. rm -r ` ls -AR folder `
    – prkos
    Commented Mar 2, 2022 at 2:00
  • Doesn't work if any filename includes whitespace (pretty common) or any glob char(s) (less common but possible) or begins with hyphen. OTOH (cd goodunzip; find .)|xargs -I{} rm {} works for anything but newlines (and already includes dotfiles and handles hyphen). Commented Mar 2, 2022 at 6:26
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
    Commented Sep 26, 2016 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. Commented Sep 26, 2016 at 14:58
0

I used

 find ./folderwithUnwantedfiles -amin -5 -delete

You have to be careful with the find command and -delete option, be careful what folder you're in, what options you're including, and always include the -delete at the very end so it doesn't miss the conditions and delete everything including what you don't intend!

The -amin specifies how many minutes ago a file has been accessed. It was useful in my case as the archive I extracted was old so I couldn't use the modified time, but accessed time provided the difference from the other files I had in that folder to be able to delete only the ones extracted there by mistake.

It's an alternative that doesn't strictly depend on unzip and is easy to understand and use.

-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
    Commented Aug 10, 2017 at 11:37

You must log in to answer this question.

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