If you have an extra folder or file within a tarball (.tar) file, you can remove it without extracting the entire tarball first. This can be really handy when you have a massive file that you don’t want to spend a lot of time extracting and re-archiving. Open up a terminal shell and follow along with the example below.
We’ll first see what’s in the compressed tarball named sandbox.tar.gz by using the
--list option of
thelinuxdaily$ tar --list --file=sandbox.tar.gz sandbox/ sandbox/delete_me/ sandbox/delete_me/hello.txt sandbox/hello.txt sandbox/hello2.txt sandbox/hello3.txt sandbox/save_me/ sandbox/save_me/hello.txt
Let’s try to delete the folder called sandbox/delete_me from the compressed tarball:
thelinuxdaily$ tar --delete --file=sandbox.tar.gz sandbox/delete_me tar: Cannot update compressed archives tar: Error is not recoverable: exiting now
See what happened? That means we need to uncompress it. In this case, it’s a .gz compression type, so we’ll use
gunzip (if you were using bz2, you’d use
thelinuxdaily$ gunzip sandbox.tar.gz
Let’s try to delete the folder sandbox/delete_me again:
thelinuxdaily$ tar --delete --file=sandbox.tar sandbox/delete_me
We didn’t get an error message, so that’s good. Let’s see if it’s gone by using
thelinuxdaily$ tar --list --file=sandbox.tar sandbox/ sandbox/hello.txt sandbox/hello2.txt sandbox/hello3.txt sandbox/save_me/ sandbox/save_me/hello.txt thelinuxdaily$ gzip sandbox.tar
Excellent! That’s what we needed to see. If you have any other tips, feel free to use the comments below.