11

Possible Duplicate:
How to de-unzip, de-tar -xvf — de-unarchive in a messy folder?

This is a pretty annoying occurrence. Sometimes, I download an archive (tar.gz, tar.bz2, zip, rar, etc) and run tar xf [file] (or similar) in the file's directory. In rare occasions, all the files extract in the current working directory instead of a sub-directory. This can lead to hundreds of files and hundreds of patterns that can't simply be removed using a pattern matching solution.

Is there a way to get the file contents of an archive and then delete all files on that list in the current working directory?

marked as duplicate by Gilles, Michael Mrozek Feb 14 '12 at 4:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I hate tar-bombs, so I always create a subdirectory, put the archive there and uncompress it. Just in case. An efective way is also to always redirect the verbose list of extracted files to a file, so you have a list of what to remove if this happens. – orion Mar 19 '14 at 19:22
18

You can list the content of the archive and then pass the list to rm using xargs

Example for a tarball (test it without the rm first):

tar tfz archive.tar.gz | xargs rm -rf
  • It's definitely the smartest way to do it. – Deniz Oct 9 '15 at 9:26
  • 1
    How could it be for a zip file? – Joshua Salazar Feb 2 '18 at 1:00
-1

Assuming all the files were extracted on the same date/time, you could write a one-liner and pass the regex to rm.

  • 2
    Could you elaborate a bit, in particular to distinguish your response from the already accepted one? Would the one-liner use find based on the mtime? And where do regexes come into this at all? – Kevin Feb 14 '12 at 4:53

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