How to "undo" unzip command?

Solution 1:

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 {} 

Solution 2:

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

Solution 3:

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

Solution 4:

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 `