Showing posts with label tar. Show all posts
Showing posts with label tar. Show all posts

Thursday, August 14, 2008

Tar Gzip On-The-Fly

I typically create tarballs by doing "tar cvf file.tar somedir" and then gzipping the resultant tarball. Sometimes there's not enough space on disk to have an intermediate uncompressed file sitting around. As an example, I just made a backup of 48 gigs of MySQL data yesterday. One handy way to avoid storing the uncompressed file on disk is to gzip on the fly.

tar cvf - somedir | gzip -c > somedir.tar.gz

In writing this post, I just noticed that you can also just give tar the -z flag to accomplish the same thing; however, explicitly piping to gzip allows you to specify options to gzip such as compression level.

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)

Friday, March 14, 2008

Tar + SSH

If you want to transfer a directory structure from your local machine to a remote host, there are obviously a lot of ways to do this. You could use a recursive scp, an rsync, ftp (god forbid), or a variety of other techniques. Another way that's assuredly less popular but has the advantage of letting you specify the compression method and providing options to preserve attributes verbatim, is to use tar and ssh with the following syntax:

tar cvjf - * | ssh whoever@machine.com "(cd /path; tar xjf -)"

The previous example uses bzip2 compression, which may save time for large transfers. Thanks to Nate G. for contributing this tip.