{"id":476492,"date":"2022-07-21T23:00:00","date_gmt":"2022-07-21T20:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/"},"modified":"2022-07-21T23:00:00","modified_gmt":"2022-07-21T20:00:00","slug":"how-to-compress-and-extract-files-using-the-tar-command-on-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/","title":{"rendered":"#How to Compress and Extract Files Using the tar Command on Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e0fb3d6cd3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e0fb3d6cd3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/#%E2%80%9CHow_to_Compress_and_Extract_Files_Using_the_tar_Command_on_Linux%E2%80%9D\" >&#8220;How to Compress and Extract Files Using the tar Command on Linux&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/#Compress_an_Entire_Directory_or_a_Single_File\" >Compress an Entire Directory or a Single File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/#Compress_Multiple_Directories_or_Files_at_Once\" >Compress Multiple Directories or Files at Once<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/#Exclude_Directories_and_Files\" >Exclude\u00a0Directories and Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/#Use_bzip2_Compression_Instead\" >Use bzip2 Compression\u00a0Instead<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/#Extract_an_Archive\" >Extract an Archive<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Compress_and_Extract_Files_Using_the_tar_Command_on_Linux%E2%80%9D\"><\/span>&#8220;How to Compress and Extract Files Using the tar Command on Linux&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-819134\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/New-Header-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Compressed Tarball Header Ubuntu\" width=\"1200\" height=\"675\"\/><\/p>\n<p>The tar command on Linux is often used to create .tar.gz or .tgz archive files, also called\u00a0\u201ctarballs.\u201d This command\u00a0has a large number of options, but you just need to remember a few letters to quickly create archives with tar. The tar command\u00a0can extract the resulting archives, too.<\/p>\n<p>The GNU tar command included with Linux distributions has integrated compression. It can create\u00a0a .tar archive and then compress it with gzip or bzip2 compression in a single command. That\u2019s why the resulting file is a .tar.gz file or .tar.bz2 file.<\/p>\n<blockquote class=\"admonishment_update\"><p><strong>Update, 7\/18\/22:<\/strong> We\u2019ve reviewed this article for accuracy and can confirm it still works on the latest version of Ubuntu and other Linux distributions.<\/p><\/blockquote>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Compress_an_Entire_Directory_or_a_Single_File\"><\/span>Compress an Entire Directory or a Single File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use the following command to compress an entire directory or a single file on Linux. It\u2019ll also compress every other directory inside a directory you specify\u2013in other words, it works recursively.<\/p>\n<pre>tar -czvf name-of-archive.tar.gz\u00a0\/path\/to\/directory-or-file<\/pre>\n<p>Here\u2019s what those switches actually mean:<\/p>\n<ul>\n<li>-c: <strong>C<\/strong>reate an archive.<\/li>\n<li>-z: Compress the archive with g<strong>z<\/strong>ip.<\/li>\n<li>-v: Display progress in the terminal while creating the archive, also known as \u201c<strong>v<\/strong>erbose\u201d mode. The v is always optional in these commands, but it\u2019s helpful.<\/li>\n<li>-f: Allows you to specify the <strong>f<\/strong>ilename of the archive.<\/li>\n<\/ul>\n<p>Let\u2019s say you have a directory named \u201cstuff\u201d in the current directory and you want to save it to a file named archive.tar.gz. You\u2019d run the following command:<\/p>\n<pre>tar -czvf archive.tar.gz stuff<\/pre>\n<p>Or, let\u2019s say there\u2019s a directory at \/usr\/local\/something on the current system and you want to compress it to a file named archive.tar.gz. You\u2019d run the following command:<\/p>\n<pre>tar -czvf archive.tar.gz \/usr\/local\/something<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-249037\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/img_5702da2234b25.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Run &quot;tar -czvf&quot; on the desired folder to bundle and compress all of the contents. \" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Compress_Multiple_Directories_or_Files_at_Once\"><\/span>Compress Multiple Directories or Files at Once<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>RELATED:<\/strong> <strong><em>How to Manage Files from the Linux Terminal: 11 Commands You Need to Know<\/em><\/strong><\/p>\n<p>While tar is frequently used to compress\u00a0a single directory, you could also use it to compress multiple directories, multiple individual files, or both. Just provide a list of files or directories instead of a single one. For example, let\u2019s say you want to compress the \/home\/ubuntu\/<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Download<\/a>s directory, the \/usr\/local\/stuff directory, and the \/home\/ubuntu\/Documents\/notes.txt file. You\u2019d just run the following command:<\/p>\n<pre>tar -czvf archive.tar.gz \/home\/ubuntu\/Downloads \/usr\/local\/stuff \/home\/ubuntu\/Documents\/notes.txt<\/pre>\n<p>Just list as many directories or files as you want to back up.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-249040\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/img_5702db3dd51f8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Ubuntu Terminal compressing multiple directories at once with the &quot;tar&quot; command. \" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Exclude_Directories_and_Files\"><\/span>Exclude\u00a0Directories and Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In some cases, you may wish to compress an entire directory, but not include\u00a0certain files and directories. You can do so by appending an <code>--exclude<\/code> switch for each directory or file you want to exclude.<\/p>\n<p>For example, let\u2019s say you want to compress \/home\/ubuntu, but you don\u2019t want to compress the \/home\/ubuntu\/Downloads and \/home\/ubuntu\/.cache directories. Here\u2019s how you\u2019d do it:<\/p>\n<pre>tar -czvf archive.tar.gz \/home\/ubuntu --exclude=\/home\/ubuntu\/Downloads --exclude=\/home\/ubuntu\/.cache<\/pre>\n<p>The <code>--exclude<\/code> switch is very\u00a0powerful. It doesn\u2019t take names of directories and files\u2013it actually accepts patterns. There\u2019s a lot more you can do with it. For example, you could archive an entire directory and exclude all .mp4 files with the following command:<\/p>\n<pre>tar -czvf archive.tar.gz \/home\/ubuntu --exclude=*.mp4<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-249041\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/img_5702dbb8c57f1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The &quot;tar&quot; command can be tuned to selectively ignore some files. \" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Use_bzip2_Compression_Instead\"><\/span>Use bzip2 Compression\u00a0Instead<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>While gzip compression\u00a0is most frequently used to create .tar.gz or .tgz files, tar also supports bzip2 compression. This allows you to create bzip2-compressed files, often named .tar.bz2, .tar.bz, or .tbz files. To do so, just replace the -z for gzip in the commands here with a -j for bzip2.<\/p>\n<p>Gzip is faster, but it <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly compresses a bit less, so you get a somewhat larger file. Bzip2 is slower, but it compresses a bit more, so you get a somewhat smaller file. Gzip is also more common, with some s<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a>ped-down Linux systems including gzip support by default, but not bzip2 support. In general, though, gzip and bzip2 are practically the same thing, and both will work similarly.<\/p>\n<p>For example, instead of the first example we provided for compressing the stuff directory, you\u2019d run the following command:<\/p>\n<pre>tar -cjvf archive.tar.bz2\u00a0stuff<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-249042\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/img_5702dc4410e5d.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Terminal running bzip2 compression instead of gzip. \" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Extract_an_Archive\"><\/span>Extract an Archive<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once you have an archive, you can extract\u00a0it with the tar command.\u00a0The following command will extract the contents of archive.tar.gz to the current directory.<\/p>\n<pre>tar -xzvf archive.tar.gz<\/pre>\n<p>It\u2019s the same as the archive creation\u00a0command we used above, except the <code>-x<\/code> switch replaces the <code>-c<\/code> switch. This specifies you want to e<strong>x<\/strong>tract an archive instead of create one.<\/p>\n<p>You may want to extract the contents of the archive to a specific directory. You can do so by appending the <code>-C<\/code> switch to the end of the command. For example, the\u00a0following command will extract the contents of the archive.tar.gz file to the \/tmp\u00a0directory.<\/p>\n<pre>tar -xzvf archive.tar.gz -C \/tmp<\/pre>\n<p>If the file is a bzip2-compressed file, replace the \u201cz\u201d in the above commands with a \u201cj\u201d.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-249043\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/img_5702dc9e95b12.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Run &quot;tar&quot; with&quot;-xzvf&quot; instead of the &quot;-czvf&quot; argument to extract files from a tarball archive. \" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<hr\/>\n<p>This is the simplest possible usage of the tar command. The\u00a0command includes a large number of additional options, so we can\u2019t possibly list them all here. For more information. run the <strong>info\u00a0tar<\/strong> command at the shell to view the tar command\u2019s detailed\u00a0information page. Press the <strong>q<\/strong> key to quit the information page when you\u2019re done. You can also\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.gnu.org\/software\/tar\/manual\/tar.html\">read tar\u2019s manual online<\/a>.<\/p>\n<p>If you\u2019re using a graphical Linux desktop, you could also use the\u00a0file-compression utility or file manager included with your desktop to create or extract .tar files. On Windows, you can extract and create .tar archives\u00a0with the free\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.7-zip.org\/\">7-Zip<\/a>\u00a0utility.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/248780\/how-to-compress-and-extract-files-using-the-tar-command-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Compress and Extract Files Using the tar Command on Linux&#8221; The tar command on Linux is often used to create .tar.gz or .tgz archive files, also called\u00a0\u201ctarballs.\u201d This command\u00a0has a large number of options, but you just need to remember a few letters to quickly create archives with tar. The tar command\u00a0can extract&#8230;<\/p>\n","protected":false},"author":1,"featured_media":476493,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2016\/04\/New-Header-1.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-476492","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/476492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=476492"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/476492\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/476493"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=476492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=476492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=476492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}