tar and untar

When transferring files and backing up data from one computer to another, the smaller the file and the fewest number of files speeds up the process enormously.

This article concentrates on compressing and uncompressing files and folders using the command line into, and from, different formats.


The three main ways of compressing files and folders are tar, tar.gz and tar.bz2.

Each has uses different compression methods and produce different sized results. The most common you will find are tar.gz and tar.bz2.

tar

Although not often used on it's own, tar (which comes from the words 'tape archive' or 'tape archiving'), does not actually compress files or folders but rather puts them into one file.

This is very handy when transferring or backing-up a directory as it produces one easy to manipulate file rather than an unwieldy folder full of them.

To tar files is very simple:

tar cvf dest.tar file.txt file2.txt

As you can see, the destination file is named first and anything named after that is placed into the tar file.

Tarring folders is done in exactly the same way:

tar cvf dest.tar myfolder/

Naturally, once transferred you will want to untar the file or folder like this:

tar xvf dest.tar

You may also wonder what is in the tar file so a quick list of files would be helpful. No worries:

tar tvf dest.tar

You may have notice the pattern in the commands: compressing uses the 'c' option, extracting uses the 'x' option and listing uses the 't' option.

That's all well and good but what if the tar file is several MB or even GB in size?

That's where compressing the archive comes into play. Once created, the tar file can be compressed to a smaller size.

tar.gz

One compression method uses gzip. So to tar a folder and then compress it using gzip we would use:

tar cvfz dest.tar.gz myfolder/

Again, the destination file, ending in tar.gz, is named first and the files and folders to be compressed are then named.

Uncompressing a gzip file is just as simple:

tar xvfz dest.tar.gz

And to have a quick peek inside the file without uncompressing the whole thing:

tar tvzf dest.tar.gz

tar.bz

The third method again uses tar to place all the files and folders into one large file but uses the bzip2 compressions utility instead of gzip.

Although not an absolute, bzip2 will usually produce a smaller file than when using gzip.

I am sure you are getting the hang of it, but to compress a folder, we first tar it and then, using bzip2, compress it:

tar cvjf dest.tar.bz2 myfolder/

To extract it:

tar xvjf dest.tar.bz2

And to have a little look inside:

tar tvjf dest.tar.bz2

Extracting individual files

I've left this useful addition to the end as it is easily applied to all three methods shown above.

The situation is commonly encountered: you have a compressed backup, perhaps several MB or GB in size, but you only want one file from it.

No problem. Simply name the file you want to extract after the initial command like so:

tar xvjf dest.tar.bz2 textfile.txt

As you can see, it uses exactly the same method of extraction used before, but instead of uncompressing the whole lot, it just uncompresses the named file - in this example I wanted textfile.txt to be uncompressed.

In all three methods (tar, tar.gz and tar.bz2), just name the specific file, or files, you want extracting after the standard uncompress command.

Some more advanced techniques are discussed in next article such as adding and deleting files from existing archives and excluding files when creating archives.

PickledOnion.

Article Comments:

Praveen commented Sat Oct 04 09:59:58 UTC 2008:

Simple and complete without losing information

Taras Mankovski commented Sat Oct 04 12:29:57 UTC 2008:

is it possible to extract to a specific folder?

SelfAdmin commented Sat Dec 20 15:09:04 UTC 2008:

@Taras: You can extract to a specific folder by specifying the folder with the "-C" parameter:

tar xvjf dest.tar.bz2 -C /some/dest/dir

Matt commented Tue Apr 14 03:18:17 UTC 2009:

Any idea why I would be getting this error?

gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error exit delayed from previous errors

shormin Ferdous commented Sun Jun 28 13:26:13 UTC 2009:

I have tarred one file using tar -cvf command but while try to untar it with tar -xvf command, it does not work. Cursor goes down one line and blinks. can anybody help me pls? Shormin

Holiday commented Sun Mar 21 23:25:51 UTC 2010:

What is the main idea of the non professionals?! For newbies?! What is easier actually?!

Jered commented Mon Mar 22 15:23:54 UTC 2010:

All three approaches are pretty easy, just different in terms of the flags used with tar.

In general I would say that if you want the maximum compression to save disk space, or are using the file only on your slice, go with the tar.bz method. If you want maximum compatibility with other systems, or will be putting the file up for download by other users, tar.gz would be safer. More utilities support gzip on other operating systems than bzip.

Ferienhaus commented Fri Apr 16 00:36:54 UTC 2010:

A really wonderful example of the way of making really good guides. Thanks!

Multimediafestplatte commented Tue Jun 08 19:47:13 UTC 2010:

Thanks for the guide. I bookmarked your site.

cc commented Fri Aug 27 08:13:17 UTC 2010:

Newbie question. What is the correct syntax to tar the /home directory and save to the /media/U/backup directory? (using .gz or.bz)

Jered commented Fri Aug 27 14:12:34 UTC 2010:

For gz the command would look like: "tar cvzf /media/U/backup/destination.gz /home". To use bz instead, change the "z" to a "j" in the command's options.

Aza commented Thu Sep 30 18:41:53 UTC 2010:

Very concise. Does tar automatically includes subdirectories? For example consider the following simple tree:

/usr/myfolder /usr/myfolder/mysubdirectory

Will "tar cvjf dest.tar.bz2 myfolder/" include the contents of both "myfolder" and "mysubdirectory"?

Jered commented Fri Oct 01 18:53:06 UTC 2010:

Yep, tar does archive everything in a specified directory, including subdirectories and their contents.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)