Subscribe to How-To Geek

Linux QuickTip: Downloading and Un-tarring in One Step

Most of the time, when I download something it’s a file archive of some kind – usually a tarball or a zip file. This could be some source code for an app that isn’t included in Gentoo’s Portage tree, some documentation for an internal corporate app, or even something as mundane as a new WordPress installation.

The traditional way of downloading and untarring something in the terminal would be something like this:

wget http://wordpress.org/latest.tar.gz

tar xvzf latest.tar.gz

rm latest.tar.gz

Or perhaps the more compact form:

wget http://wordpress.org/latest.tar.gz && tar xvzf latest.tar.gz && rm latest.tar.gz

Either way is a bit clumsy. This is a very simple operation, a powerful shell like bash should allow such a task to be performed in a more “slick” manner.

Well, thanks to a useful little command “curl”, we can actually accomplish the mess above in just one piped statement:

curl http://wordpress.org/latest.tar.gz | tar xvz

No temporary files to get rid of, no messing around with ampersands. In short, a highly compact, efficient command. In fact, from a theoretical standpoint, the curl method can be faster than the concatenated wget/tar/rm mess since stdout piping will use RAM as a buffer if possible, whereas wget and tar (with the -f switch) must read/write directly from a disk.

Incidentally, tar with the -v option (the way we’re using it in all the above examples) prints each file name to stdout as each is untarred. This can get in the way of curl’s nice, ncurses output showing download status. We can silence tar by invoking it without -v thusly:

curl http://wordpress.org/latest.tar.gz | tar xz

And that’s all there is to it!

| More
This article was originally written on 06/22/07 Tagged with: Linux, Ubuntu

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (5)

  1. Michael B

    You can use wget instead of curl for this:

    wget http://wordpress.org/latest.tar.gz -O- | tar xz

    -O- means “output to standard output”.

  2. Mr Linux

    True, but curl doesn’t require any extra switches to pipe to stdout.

    On the plus side (for using wget), wget is included with most *nix systems whereas it’s hit-and-miss with curl.

  3. The Geek

    I wrapped this into a script that I called turl:

    ——— beginning of turl ———–
    #!/bin/sh
    # Usage: turl <url>

    curl “$1″ | tar xz
    ——— end of turl ———–

    I also wrapped wget into a script that I called tget, for servers that don’t have curl

    ——— beginning of tget ———–
    #!/bin/sh
    # Usage: tget <url>

    wget “$1″ -O- | tar xz
    ——— end of tget ———–

    Awesome tip…

  4. supermansghost

    I like the turl script – very useful

  5. Sikachu!

    Thank you very much :)

    I know I could done this, and you really made my day!


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
mDNSResponder.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.