Monday, July 21, 2008

Untar a Single File

From time to time, I've had the need to extract a single file from a tar archive. How to do so is pretty poorly documented in the man page, but it's actually easy to do. If you have a tarball named "my.tar" with a group of files in it, and you want to extract "hello.txt", you would do the following:

tar -x hello.txt -vf my.tar

Before I extract anything from a tarball, I always preview it's contents to avoid "tarbombs" (tarballs that extract directly into the CWD).

tar -tvf my.tar

Or if it's gzipped:

tar tzvf my.tar.gz

Lastly, newer versions of Vim can browse a tarball out of the box and open any file contained within.

vim my.tar (displays a file browser)


housetier said...

it also works with compressed tar archives: "vim foo.tar.bz2" will open a file browser as well.

Whispers in your ear said...

I find this order of arguments a little easier to remember:

tar -xf tarball
tar -xf tarball member
tar -xf tarball member1 member2

Where the member(s) can be files or directories within the tarball.

Andrew said...

This is very neat.

How recent does vim have to be to support this?