Argh! I did it once again! This morning I was unzipping a zip file and stupidly it spilled some 9000 files onto my desktop.
Since you will probably encounter this later in your life, let me tell you how to deal with this situation. But even before that, let’s talk about preventing it from happening. It’s quite simple… you should always unzip a zip file into a directory. Suppose the zip file concerned is called foo.zip. Here is an example:
unzip foo.zip -d a_temp_dir\
This will place the new content into a subdirectory called a_temp_dir under the current directory. No spills.
But accidents do happen. What did I do to clean up this mess?
unzip -l foo.zip
| grep -E "[0-9]:[0-9]"
| cut -c 29-
| zip -@ -m removed.zip
(all these in one command)
- Obtain a listing of the zip file with
unzip -l. - Strip away the header and footer by retaining those lines that have a “digit colon digit” pattern with
grep. The pattern is in the time-stamp. - Cut away the size and time-stamp from each line with
cut. (Turns out these add up to 28 characters, so we want characters 29 onward; note that this could be platform dependent and so I recommend counting this yourself.) - Use
zipto remove the files and put them inremoved.zip. The parameter-@says “read the file list from stdin” and-msays “remove the files after zipping them”.
Once you are happy with the result (probably by inspecting the remaining files in the current directory), you can remove removed.zip any time you want.
There is a caveat I know of. If there is already an empty subdirectory called bar in the current directory and if bar is also in foo.zip, then in the last step zip -m will remove bar since it is hard (although technically not impossible) to distinguish whether bar is created as a result of unzipping foo.zip or not.
‘nough said. Going back to work, work and work.