summaryrefslogtreecommitdiff
path: root/slackbook/html/archive-files-tar.html
blob: b23db4b12d4959575e6e13f6886b8e3b2a78e9b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!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>