I extracted a zip file into a non-empty folder. The zip file has lots of files and a deep hierarchy, that merged with the existing tree of the target directory. How can I remove the files and directories that where created by unzipping without destroying the files and directories that were already there? Of course, I still have the zip file that I merged in, so the information is there.
jjlin's answer is the way to go. I just want to add a few choices for directories:
You can use
The above command will have issues dealing with paths that have whitespace. This (more complicated) version should fix that:
|show 1 more comment|
With the switch
This way, you can use
to delete all files extracted from the zip file.
will delete directories as well, but you have to be careful. If the directories already existed before extracting the zip file, all pre-existing files in those directories will be deleted as well.
If you're going to re-extract the zip file anyway, there's another approach that is guaranteed to deal with strange file names.
First extract the zip file where you originally meant to extract it:
Now, change into the directory where you extracted the files by mistake and execute the following command:
To deal with leftover directories, you can execute the command:
Related man pages:
Here is an even easier and safer (I think) solution
What this is doing: The backquoted unzip command will produce a list of what was in your original file.
zip -m will then use that list to add add that each to getmeoutofhere.zip and remove it from the original directory (so theoretically it should be indential to myoriginalfile.zip.
The downside is that unzip -lqq will produce some extra text, dates, times, filesize, etc. These will cause zip -m to produce error messages but this should have no affect (unless you have the unlikely case of a file with the same name).
Please note that this will not remove any directories that were created during the original unzip.
If you extracted the files such that the modification timestamp in the archive is not preserved in the extracted copies (but rather the extracted files have their usual modification time) then the right way to attack this is via modification time. All the extracted files have a newer modification timestamp than the most recently modified existing file in that directory.
Here is a simple situation.
Suppose that none of the existing files in the current directory were touched for at least 24 hours. Anything that was modified in the last 24 hours is therefore junk from the zipfile.
This will find some directories too, but
Any directories which were recently modified were modified by the zip. If
Now we open
Then join it all into a big line:
Run the line under the cursor as a shell command:
Definitely, I would not automate the steps of this task, due to the risk of erasing files which were already there, or screwing up due to file name issues.
If you're going to go the obvious route of obtaining a list of the paths in the zip, then capture it to a file, look over it very carefully and transform it to a removal after doing any necessary editing.