Remove all files previously extracted from a tar(.gz) file.

tar -tf <file.tar.gz> | xargs rm -r

By: prayer
2009-07-06 22:23:11

2 Alternatives + Submit Alt

What Others Think

None of these alternatives is perfect. If one of the tar's directories has other files (like /usr/share/man), the 'rm -r' from alternative 1 will end up erasing the whole directory. And alternative 2 just does not remove directories. My take: tar tf <file.tar.gz> | sort -r | while read file; do if [ -d "$file" ]; then rmdir "$file"; else rm -f "$file"; fi; done --> This command will spit a few error messages if there are already files on some directories, etc., but it will work OK and safe and will erase files and directories in the right order (files first, respective directories afterwards).
Patola · 654 weeks and 6 days ago
many times people use * to build their archives which cause me some time stamp way to do this ... why not interrogate the archive ... Usually *nix users behave and put stuff in a folder then tar it up ... but most of the time when I see * archive it's .zip file ... this this inspired me to come up with this one: unzip -lt | grep testing | awk '{print $2}' | xargs rm -r
copremesis · 654 weeks and 5 days ago
use pipe | while read a; instead of xargs. it happened to me to have too many entry to finish the buffer using xargs
ioggstream · 654 weeks and 5 days ago
Teal was the color of your shirt a pat on the back
yeoyeo · 23 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: