FOSSwire Reference - Using tar to create, extract and manipulate archives

Quick headnote - welcome to a new breed of FOSSwire post, the Reference post. In a Reference post, we'll try to put as much useful information as possible that you might want to quickly come back and find later!

If you've ever worked with a Unix system, you've probably used tar. Put simply, tar is a way to archive a folder, or multiple files into one file (and optionally add compression). It comes in really handy for pretty much anything, from backups, to moving files between places to using it to store lots of files for a download.

We're using GNU tar here, which is pretty much standard everywhere. If you're using a different flavour of tar, not everything here might work.

So let's get straight to it!

Creating an archive


Run tar with the c switch to create an archive. You'll probably want to also use the f switch to send tar's output to a file:
$ tar cf outputfile.tar inputs

To add compression, use the z switch for gzip, and the j switch for bzip2:
$ tar cjf outputfile.tar.bz2 inputs

Finally, you can also use the v switch (verbose) to watch progress.

Extracting an archive


Extracting is basically the same, but you use the x option to extract:
$ tar xf inputfile.tar

That will extract said archive to the current directory. For a compressed archive, you'll again need to add the z for a .tar.gz, or j for .tar.bz2.
$ tar xjf inputfile.tar.bz2

Again, to see progress you may want to add the v option for verbose logging.

Listing contents of an archive


The t switch is helpful if you want to peek into an archive without actually extracting it. Unless you're piping output in and out, you'll need to use the f switch again to determine the archive file.
$ tar tvf myarchive.tar

You will get an ls -l style listing of the contents. You may wish to pipe the output to less for a large archive for readability.

Again, for compressed tar archives, add z or j.

Wrapping up


There's plenty more that tar can do, so I've just touched on the most common uses of tar. Feel free to fire your power tar user tips in the comments!

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: FOSSwire Reference - Using tar to create, extract and manipulate archives

  1. Lee (guest)

    # Posted on 22 May 2007 at 11:40 AM

    Thanks for this, it's something i've been curious about for a while, and not being lazt not bothered to find out about!



  2. # Posted on 04 August 2007 at 12:41 PM

    [...] Bear that in mind if you’re backing up data to a CD where permissions are important (consider making a tar archive first if [...]



  3. # Posted on 07 October 2007 at 01:23 PM

    [...] anything special like that. If you need to preserve that sort of information, you’ll need to use tar instead! Like this article? Subscribe to FOSSwire now (via email and in your feed reader) - it's free, [...]



  4. Lea-Cheater (guest)

    # Posted on 29 August 2009 at 06:10 PM

    Great idea, but will this work over the long run?



  5. taift (guest)

    # Posted on 09 October 2009 at 03:11 PM

    Do messages, photos, etc upload onto your computer? Can you read your iphone messages on your computer? And if so where are they stored?


    <a href="http://www.youtube.com/watch?v=EpZmDnMZahc">iphone 3gs</a>



Home » Articles » FOSSwire Reference - Using tar to create, extract and manipulate archives