<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>tar on
A Scripter's Notes</title><link>https://scripter.co/tags/tar/</link><description>Recent content in tar
on A Scripter's Notes</description><language>en-us</language><managingEditor>kaushal.modi@gmail.com (Kaushal Modi)</managingEditor><webMaster>kaushal.modi@gmail.com (Kaushal Modi)</webMaster><lastBuildDate>Wed, 22 Apr 2026 08:24:58 -0400</lastBuildDate><generator>Hugo -- gohugo.io</generator><docs>https://validator.w3.org/feed/docs/rss2.html</docs><atom:link href="https://scripter.co/tags/tar/index.xml" rel="self" type="application/rss+xml"/><item><title>Disarming the 'tar' bomb in 10 seconds</title><link>https://scripter.co/disarming-the-tar-bomb-in-10-seconds/</link><description>&lt;blockquote>Use &lt;code>tar -caf &amp;lt;file&amp;gt; &amp;lt;dir&amp;gt;&lt;/code> to create an archive, &lt;code>tar -xf &amp;lt;file&amp;gt;&lt;/code> to
extract one, and more.&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#mnemonics">Mnemonics&lt;/a>&lt;/li>
&lt;li>&lt;a href="#creating-an-archive">Creating an archive&lt;/a>&lt;/li>
&lt;li>&lt;a href="#extracting-an-archive">Extracting an archive&lt;/a>&lt;/li>
&lt;li>&lt;a href="#listing-contents-of-an-archive">Listing contents of an archive&lt;/a>&lt;/li>
&lt;li>&lt;a href="#summary">Summary&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;p>I had come across &lt;a href="https://fosstodon.org/@garritfra/108409516362853350">this post by user &lt;em>Garrit&lt;/em>&lt;/a> on Mastodon, and that
inspired this post.&lt;/p>
&lt;div class="mf2 reply">In reply to: &lt;p>&lt;a class="u-in-reply-to h-cite" rel="in-reply-to" href="https://garrit.xyz/posts/2022-06-02-tar-commands">https://garrit.xyz/posts/2022-06-02-tar-commands&lt;/a>&lt;/p>&lt;/div>
&lt;p>A post on the Unix command &lt;a href="https://www.gnu.org/software/tar/manual/html_node/index.html" title="Emacs Lisp: (info &amp;quot;(tar) Top&amp;quot;)">&lt;code>tar&lt;/code>&lt;/a> cannot leave out the obligatory &lt;em>xkcd
&lt;strong>tar&lt;/strong>&lt;/em> comic 😄, so here it is:&lt;/p>
&lt;p>&lt;a id="figure--xkcd-1168">&lt;/a>&lt;/p>
&lt;figure>
&lt;img src="https://scripter.co/disarming-the-tar-bomb-in-10-seconds/xkcd-1168.png" title="I don't know what's worse--the fact that after 15 years of using tar I still can't keep the flags straight, or that after 15 years of technological advancement I'm still mucking with tar flags that were 15 years old when I started."/> &lt;figcaption>
&lt;p>
&lt;a href="https://xkcd.com/1168/">
xkcd.com
&lt;/a>&lt;/p>
&lt;/figcaption>&lt;/figure>
&lt;h2 id="mnemonics">Mnemonics&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#mnemonics">#&lt;/a>&lt;/h2>
&lt;p>These few mnemonics help me remember the basic and my most frequently
used &lt;code>tar&lt;/code> options:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>c&lt;/strong> to (c)reate&lt;/li>
&lt;li>&lt;strong>a&lt;/strong> to (a)uto compress the archive based on the file name&lt;/li>
&lt;li>&lt;strong>x&lt;/strong> to e(x)tract&lt;/li>
&lt;li>&lt;strong>f&lt;/strong> for the archive (f)ile name we are dealing with (whether
creating, listing or extracting an archive)&lt;/li>
&lt;/ul>
&lt;p>.. and a few not so frequent options (for me):&lt;/p>
&lt;ul>
&lt;li>&lt;strong>t&lt;/strong> to lis(t) contents of an archive&lt;/li>
&lt;li>&lt;strong>v&lt;/strong> for (v)erbose output&lt;/li>
&lt;/ul>
&lt;h2 id="creating-an-archive">Creating an archive&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#creating-an-archive">#&lt;/a>&lt;/h2>
&lt;div class="org-center">
&lt;p>&lt;strong>tar -caf &amp;lt;file&amp;gt; &amp;lt;dir&amp;gt;&lt;/strong>&lt;/p>
&lt;/div>
&lt;p>A keen user might have noticed that I am using &lt;code>tar -caf ..&lt;/code> instead
of &lt;code>tar caf ..&lt;/code> i.e. I am using a hyphen before the &lt;code>tar&lt;/code>
options. Both approaches work and they look similar, but the approach
with the hyphen is the &lt;strong>newer&lt;/strong> &lt;a href="https://www.gnu.org/software/tar/manual/html_node/Short-Options.html" title="Emacs Lisp: (info &amp;quot;(tar) Short Options&amp;quot;)">Short Option style&lt;/a> while the other is
the &lt;a href="https://www.gnu.org/software/tar/manual/html_node/Old-Options.html" title="Emacs Lisp: (info &amp;quot;(tar) Old Options&amp;quot;)">Old Option style&lt;/a>.&lt;/p>
&lt;div class="note">
&lt;p>I prefer the &lt;em>short option style&lt;/em> because .. well.. the other style is
old.. and also because the &lt;em>short option style&lt;/em> is &lt;strong>stricter&lt;/strong> e.g. the
&lt;code>-f&lt;/code> switch has to be followed by the file name.&lt;/p>
&lt;/div>
&lt;p>Whether you are creating a regular &lt;strong>.tar&lt;/strong> archive, or a compressed
archive like &lt;strong>.tar.gz&lt;/strong>, always use the auto-compresion switch
&lt;strong>-a&lt;/strong>. That relieves you from deciding &lt;em>if&lt;/em> you need that switch, or
which compression algorithm switch should be used 😄.&lt;/p>
&lt;p>The &lt;strong>-a&lt;/strong> switch makes the decision for you based on the file
extension. For example, &lt;code>tar -caf foo.tar foo/&lt;/code> will create a regular
archive, while &lt;code>tar -caf foo.tar.gz foo/&lt;/code> will create a compressed
archive using &lt;code>gzip&lt;/code>. You can read more about it in &lt;a href="https://www.gnu.org/software/tar/manual/html_node/gzip.html" title="Emacs Lisp: (info &amp;quot;(tar) gzip&amp;quot;)">Creating and
Reading Compressed Archives&lt;/a>.&lt;/p>
&lt;h2 id="extracting-an-archive">Extracting an archive&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#extracting-an-archive">#&lt;/a>&lt;/h2>
&lt;div class="org-center">
&lt;p>&lt;strong>tar -xf &amp;lt;file&amp;gt;&lt;/strong>&lt;/p>
&lt;/div>
&lt;p>At times, it might be useful to add the verbosity switch &lt;strong>-v&lt;/strong> to this
command and do &lt;code>tar -xvf &amp;lt;file&amp;gt;&lt;/code>.&lt;/p>
&lt;div class="note">
&lt;p>Just to reiterate, the &lt;strong>-f&lt;/strong> switch must be followed by the archive
file name.&lt;/p>
&lt;/div>
&lt;h2 id="listing-contents-of-an-archive">Listing contents of an archive&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#listing-contents-of-an-archive">#&lt;/a>&lt;/h2>
&lt;div class="org-center">
&lt;p>&lt;strong>tar -tf &amp;lt;file&amp;gt;&lt;/strong>&lt;/p>
&lt;/div>
&lt;p>Once you are done creating an archive, you might feel the need to
check if the archive contains everything you expect. Or you might want
to check what&amp;rsquo;s inside the archive before you extract it.&lt;/p>
&lt;p>This command is often paired with &lt;code>grep&lt;/code> or &lt;a href="https://github.com/BurntSushi/ripgrep">&lt;code>rg&lt;/code> (ripgrep)&lt;/a> like so:
&lt;code>tar -tf foo.tar.xz | rg 'some_file_name_in_archive'&lt;/code>.&lt;/p>
&lt;h2 id="summary">Summary&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#summary">#&lt;/a>&lt;/h2>
&lt;p>If you simply glossed over the whole article, or didn&amp;rsquo;t read through
the all linked manual pages
&lt;span class="sidenote-number">&lt;small class="sidenote">
I know you didn&amp;rsquo;t 😉
&lt;/small>&lt;/span>
, just remember this &amp;mdash;&lt;/p>
&lt;div class="org-center">
&lt;p>&lt;strong>tar -caf&lt;/strong> to &lt;strong>c&lt;/strong>​reate and &lt;strong>tar -xf&lt;/strong> to e​&lt;strong>x&lt;/strong>​tract&lt;/p>
&lt;/div></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/unix">unix</category><category domain="https://scripter.co/categories/replies">replies</category><category domain="https://scripter.co/tags/tar">tar</category><category domain="https://scripter.co/tags/100daystooffload">100DaysToOffload</category><guid>https://scripter.co/disarming-the-tar-bomb-in-10-seconds/</guid><pubDate>Mon, 13 Jun 2022 01:14:00 -0400</pubDate></item></channel></rss>