16

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?

nopcorn
  • 9,559
  • 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

2 Answers2

25

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

For .zip files, replace tar tfz archive.tar.gz with unzip -Z1 file.zip (or equivalently zipinfo -1 file.zip) (ref: https://unix.stackexchange.com/a/128304/368910)

Matteo
  • 9,796
  • 4
  • 51
  • 66
-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 04:53