summaryrefslogblamecommitdiffstats
path: root/slackbook/html/archive-files-tar.html
blob: b23db4b12d4959575e6e13f6886b8e3b2a78e9b9 (plain) (tree)







































































































































































































































                                                                                         
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<title>tar</title>
<meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" />
<link rel="HOME" title="Slackware Linux Essentials" href="index.html" />
<link rel="UP" title="Archive Files" href="archive-files.html" />
<link rel="PREVIOUS" title="bzip2" href="archive-files-bzip2.html" />
<link rel="NEXT" title="zip" href="archive-files-zip.html" />
<link rel="STYLESHEET" type="text/css" href="docbook.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"
alink="#0000FF">
<div class="NAVHEADER">
<table summary="Header navigation table" width="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<th colspan="3" align="center">Slackware Linux Essentials</th>
</tr>

<tr>
<td width="10%" align="left" valign="bottom"><a href="archive-files-bzip2.html"
accesskey="P">Prev</a></td>
<td width="80%" align="center" valign="bottom">Chapter 15 Archive Files</td>
<td width="10%" align="right" valign="bottom"><a href="archive-files-zip.html"
accesskey="N">Next</a></td>
</tr>
</table>

<hr align="LEFT" width="100%" />
</div>

<div class="SECT1">
<h1 class="SECT1"><a id="ARCHIVE-FILES-TAR" name="ARCHIVE-FILES-TAR">15.3 <tt
class="COMMAND">tar</tt></a></h1>

<p><tt class="COMMAND">tar</tt>(1) is the GNU tape archiver. It takes several files or
directories and creates one large file. This allows you to compress an entire directory
tree, which is impossible by just using <tt class="COMMAND">gzip</tt> or <tt
class="COMMAND">bzip2</tt>. <tt class="COMMAND">tar</tt> has many command line options,
which are explained in its man page. This section will just cover the most common uses of
<tt class="COMMAND">tar</tt>.</p>

<p>The most common use for <tt class="COMMAND">tar</tt> is to decompress and unarchive a
package that you've downloaded from a web site or ftp site. Most files will come with a
<tt class="FILENAME">.tar.gz</tt> extension. This is commonly known as a
&#8220;tarball&#8221;. It means that several files were archived using <tt
class="COMMAND">tar</tt> and then compressed using <tt class="COMMAND">gzip</tt>. You
might also see this listed as a <tt class="FILENAME">.tar.Z</tt> file. It means the same
thing, but this is usually encountered on older Unix systems.</p>

<p>Alternatively, you might find a <tt class="FILENAME">.tar.bz2</tt> file somewhere.
Kernel source is distributed as such because it is a smaller download. As you might have
guessed, this is several files archived with <tt class="COMMAND">tar</tt> and then
bzipped.</p>

<p>You can get to all the files in this archive by making use of <tt
class="COMMAND">tar</tt> and some command line arguments. Unarchiving a tarball makes use
of the <var class="OPTION">-z</var> flag, which means to first run the file through <tt
class="COMMAND">gunzip</tt> and decompress it. The most common way to decompress a
tarball is like so:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">tar -xvzf filename.tar.gz</kbd>
</pre>
</td>
</tr>
</table>

<p>That's quite a few options. So what do they all mean? The <var class="OPTION">-x</var>
means to extract. This is important, as it tells <tt class="COMMAND">tar</tt> exactly
what to do with the input file. In this case, we'll be splitting it back up into all the
files that it came from. <var class="OPTION">-v</var> means to be verbose. This will list
all the files that are being unarchived. It is perfectly acceptable to leave this option
off, if somewhat boring. Alternatively, you could use <var class="OPTION">-vv</var> to be
very verbose and list even more information about each file being unarchived. The <var
class="OPTION">-z</var> option tells <tt class="COMMAND">tar</tt> to run <tt
class="FILENAME">filename.tar.gz</tt> through <tt class="COMMAND">gunzip</tt> first. And
finally, the <var class="OPTION">-f</var> option tells <tt class="COMMAND">tar</tt> that
the next string on the command line is the file to operate on.</p>

<p>There are a few other ways to write this same command. On older systems lacking a
decent copy of GNU <tt class="COMMAND">tar</tt>, you might see it written like so:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd
class="USERINPUT">gunzip filename.tar.gz | tar -xvf -</kbd>
</pre>
</td>
</tr>
</table>

<p>This command line will uncompress the file and send the output to <tt
class="COMMAND">tar</tt>. Since <tt class="COMMAND">gzip</tt> will write its output to
standard out if told to do so, this command will write the decompressed file to standard
out. The pipe then sends it to <tt class="COMMAND">tar</tt> for unarchiving. The
&#8220;-&#8221; means to operate on standard input. It will unarchive the stream of data
that it gets from <tt class="COMMAND">gzip</tt> and write that to the disk.</p>

<p>Another way to write the first command line is to leave off the dash before the
options, like so:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">tar xvzf filename.tar.gz</kbd>
</pre>
</td>
</tr>
</table>

<p>You might also encounter a bzipped archive. The version of <tt
class="COMMAND">tar</tt> that comes with Slackware Linux can handle these the same as
gzipped archives. Instead of the <var class="OPTION">-z</var> command line option, you'd
use <var class="OPTION">-j</var>:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">tar -xvjf filename.tar.bz2</kbd>
</pre>
</td>
</tr>
</table>

<p>It is important to note that <tt class="COMMAND">tar</tt> will place the unarchived
files in the current directory. So, if you had an archive in <tt
class="FILENAME">/tmp</tt> that you wanted to decompress into your home directory, there
are a few options. First, the archive could be moved into your home directory and then
run through <tt class="COMMAND">tar</tt>. Second, you could specify the path to the
archive file on the command line. Third, you can use the <var class="OPTION">-C</var>
option to &#8220;explode&#8221; the tarball in a specified directory.</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">cd $HOME</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">cp /tmp/filename.tar.gz .</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">tar -xvzf filename.tar.gz</kbd>

<samp class="PROMPT">%</samp> <kbd class="USERINPUT">cd $HOME</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">tar -xvzf /tmp/filename.tar.gz</kbd>

<samp class="PROMPT">%</samp> <kbd class="USERINPUT">cd /</kbd>
<samp class="PROMPT">%</samp> <kbd
class="USERINPUT">tar -xvzf /tmp/filename.tar.gz -C $HOME</kbd>
</pre>
</td>
</tr>
</table>

<p>All the above statements are equivalent. In each case, the archive is unpacked inside
your home directory and the original uncompressed archive is left in place.</p>

<p>So what good is being able to uncompress these archives if you can't make them? Well,
<tt class="COMMAND">tar</tt> handles that too. In most cases it's as easy as removing the
&#8220;<var class="OPTION">-x</var>&#8221; option and replacing it with the &#8220;<var
class="OPTION">-c</var>&#8221; option.</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">tar -cvzf filename.tar.gz .</kbd>
</pre>
</td>
</tr>
</table>

<p>In this command line, the <var class="OPTION">-c</var> option tells <tt
class="COMMAND">tar</tt> to create an archive, while the <var class="OPTION">-z</var>
option runs the resulting archive file through <tt class="COMMAND">gzip</tt> to compress
it. <tt class="FILENAME">filename.tar.gz</tt> is the file that you want to create.</p>

<p>Specifying the &#8220;<var class="OPTION">-f</var>&#8221; option isn't always
necessary, but is typically good practice anyway. Without it, <tt
class="COMMAND">tar</tt> writes to standard output, which is usually desired for piping
<tt class="COMMAND">tar</tt>'s output to another program, like so.</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd
class="USERINPUT">tar -cv filename.tar . | gpg --encrypt</kbd>
</pre>
</td>
</tr>
</table>

<p>That command creates an non-compressed tar archive of the current directory, pipes the
tarball through <tt class="COMMAND">gpg</tt> which encrypts and compresses the tarball,
making it realistically impossible to read by anyone other than the person knowing the
secret key.</p>
</div>

<div class="NAVFOOTER">
<hr align="LEFT" width="100%" />
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="33%" align="left" valign="top"><a href="archive-files-bzip2.html"
accesskey="P">Prev</a></td>
<td width="34%" align="center" valign="top"><a href="index.html"
accesskey="H">Home</a></td>
<td width="33%" align="right" valign="top"><a href="archive-files-zip.html"
accesskey="N">Next</a></td>
</tr>

<tr>
<td width="33%" align="left" valign="top"><tt class="COMMAND">bzip2</tt></td>
<td width="34%" align="center" valign="top"><a href="archive-files.html"
accesskey="U">Up</a></td>
<td width="33%" align="right" valign="top"><tt class="COMMAND">zip</tt></td>
</tr>
</table>
</div>
</body>
</html>