Thursday, March 27, 2008

Download and extract tarballs at the same time

Linux system administrators or advanced users frequently have contact with tarballs. A tarball is a file that can or can't be compressed and contains a set of files and directories.

The standard way to access the content of a tarball available on the Web is downloading it and so, extracting it, as follows:

$ wget http://site.domain/file.tar.gz
$ tar zxpf file.tar.gz
$ ls

file.tar.gz    file


But you can save disk space and time doing all those steps in a single command, as follows:

$ wget http://site.domain/file.tar.gz -O - |
  tar zxpf -
$ ls

file


What we did here was instruct wget to record the file to stdout using the -O - option and tar to use stdin as the source file with the f - option. To finish, we have used |, which turns the stdout of the first command in the stdin of the second one. As a result, the file is download and extracted at the same time.

With a little modification we can download the tarball, save it in a file and extract it, all at the same time. Let's look how:

$ wget http://site.domain/file.tar.gz -O - |
  tee file.tar.gz |
  tar zxpf -
$ ls

file.tar.gz    file


What we have new here is tee file.tar.gz, that takes stdin, saves it in the specified file and copy it to stdout.

No comments: