{"id":518,"date":"2020-07-09T00:53:00","date_gmt":"2020-07-09T04:53:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=518"},"modified":"2021-01-02T00:55:32","modified_gmt":"2021-01-02T05:55:32","slug":"linux-compressing-and-archiving","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/linux-compressing-and-archiving\/","title":{"rendered":"Linux: Compressing and Archiving"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">tar command<\/h3>\n\n\n\n<p>The tar command is used to collate collections of files into one larger file.<\/p>\n\n\n\n<p><strong>Creating archives<\/strong>&nbsp;Suppose we have directory xdir containing many files, we use the following command to archive them into one file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -cvf x.tar xdir<\/code><\/pre>\n\n\n\n<p>This command would archive the directory xdir and its contents into a file called x.tar. The extension &#8216;.tar&#8217; is used by convention to indicate tar files.<\/p>\n\n\n\n<p>To create an archive and apply gzip to compress the archive,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -czvf x.tar.gz xdir<\/code><\/pre>\n\n\n\n<p>The only difference from last example is the -z option. By convention, we add &#8216;.tar.gz&#8217; for tar and gzip compressed files.<\/p>\n\n\n\n<p><strong>Extracting archives<\/strong>&nbsp;To extract an archive, we use<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -xvf x.tar<\/code><\/pre>\n\n\n\n<p>It would be extracted to the same directory by default.<\/p>\n\n\n\n<p>To extract a zipped archive, we use<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -xzvf x.tar.gz<\/code><\/pre>\n\n\n\n<p><strong>Common command options<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c is for create\nv is for verbose\nf is to store in a file else it will go to stdin\/stdout\nx is for extract\nz is for gzip, ungzip to unzip\nj is for bzip2\np is to preserve permissions<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">gzip, gunzip, zcat<\/h3>\n\n\n\n<p>New unix users often get confused with gzip, gunzip, and zcat.<\/p>\n\n\n\n<p><strong>gzip<\/strong>&nbsp;gzip compresses a single file. A tar archive is a single file, so it is common to use tar files in conjuction with gzip<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -c xdir.tar > xdir.tar.gz<\/code><\/pre>\n\n\n\n<p>uncompress<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -d xdir.tar.gz<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gunzip xdir.tar.gz<\/code><\/pre>\n\n\n\n<p>uncompressing a .bz2 file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bzip2 -cd file.tar.bz2 | tar xvf -<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bzcat file.tar.bz2 | tar xvf -<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>tar command The tar command is used to collate collections of files into one larger file. Creating archives&nbsp;Suppose we have directory xdir containing many files, we use the following command to archive them into one file. This command would archive the directory xdir and its contents into a file called x.tar. The extension &#8216;.tar&#8217; is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[86,35,153],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-linux","tag-gzip","tag-linux","tag-tar"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/518\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}