Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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
up vote 13 down vote accepted

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
share|improve this answer
Thank god you came around! – n0pe Nov 20 '11 at 16:32
It's definitely the smartest way to do it. – Deniz Toprak Oct 9 '15 at 9:26

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

share|improve this answer
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.