{"id":6333,"date":"2021-05-09T20:04:49","date_gmt":"2021-05-09T20:04:49","guid":{"rendered":"https:\/\/linuxways.net\/?p=6333"},"modified":"2021-05-09T20:04:49","modified_gmt":"2021-05-09T20:04:49","slug":"how-to-compress-and-extract-files-using-the-tar-zip-command-on-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-compress-and-extract-files-using-the-tar-zip-command-on-linux\/","title":{"rendered":"How to Compress and Extract Files Using the Tar, Zip Command on Linux"},"content":{"rendered":"<p>Zip and tar are popular command-line utility for compressing and archiving multiple directories and files into one archive file in a Linux system. By default, the tar command doesn\u2019t compress files but only collects files and their metadata to generate a single tar file but we can archive files using gzip\/bzip2. Whereas, zip provides lossless data compression which is a data compression algorithm class that allows compressed data is reconstructed to its original form. The gzip and zip uses DEFLATE algorithm for compressing files whereas bzip2 uses the Burrows-Wheeler algorithm that produces highly compressed file but consumes more time as compare to gzip and zip.<\/p>\n<h3>Installation<\/h3>\n<p>By default, the tar command is integrated with the Linux system so let\u2019s begin with installing zip and unzip. The unzip command won\u2019t be installed while installing zip command so we need to install it separately.<\/p>\n<p>Before continue with installation don\u2019t forget to update the package info.<\/p>\n<pre>$ sudo apt-get update<\/pre>\n<p>Installing Zip.<\/p>\n<pre>$ sudo apt-get install zip<\/pre>\n<p>Installing Unzip<\/p>\n<pre>$ sudo apt-get install unzip<\/pre>\n<p>For showing the following example, I have used Ubuntu 20.4 and has the following files in the home directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"170\" class=\"wp-image-6334\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-12.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-12.jpeg 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-12-300x70.jpeg 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<h3>Some Examples of Tar Commands<\/h3>\n<p>Before continuing with the example you must know the syntax of the tar so the syntax is like this:<\/p>\n<pre>$ <strong>tar -c [option] [archive name] [file] | [directory] |<\/strong><\/pre>\n<p>The following options are the main and compulsory options that lead the command to what task should be performed. The following options can be used once in the command without it the command won\u2019t be run.<\/p>\n<p><strong>-c<\/strong>: To generate archive file.<\/p>\n<p><strong>-r<\/strong>: To add more files to the existing archive file.<\/p>\n<p><strong>-t<\/strong>: View all files inside the archive.<\/p>\n<p><strong>-x:<\/strong> To extract the archive files.<\/p>\n<h4>Compressing Files Using gzip and bzip2<\/h4>\n<p>Tar command produces a compressed file using gzip and bzip2 which can be accessed by providing the -z and -j options to the command respectively. For archiving and compressing tar files we need to execute the following command.<\/p>\n<pre>$ tar -cvzf compress_sample.tar.gz *.txt<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"156\" class=\"wp-image-6335\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-13.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-13.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-13-300x64.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/strong><\/p>\n<p>Compressing file using gzip.<\/p>\n<pre>$ tar -cvjf compress_sample.tar.bz2 *.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"173\" class=\"wp-image-6336\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-14.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-14.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-14-300x71.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Compressing file using bz2.<\/p>\n<p>In the above example, the -c option refers to create the archive, the -v option displays the verbose of the archived files, and -f denotes the name of the archive.<\/p>\n<p>Note: -f option always must be before the filename.<\/p>\n<h4>Extracting Files From Compressed File<\/h4>\n<p>For extracting the compressed file you need to run:<\/p>\n<pre>$ tar -xvf compress_sample.tar.gz<\/pre>\n<p>The above command will extract file in the current working directory but if want to extract files in a different directory you can do so by using -C to the command.<\/p>\n<pre>$ tar -xvf compress_sample.tar.gz -C Documents\/<\/pre>\n<h4><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"154\" class=\"wp-image-6337\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-15.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-15.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-15-300x63.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/h4>\n<p>Extracting files to different dir using tar.<\/p>\n<h4>Extracting Specific Files From Compressed File<\/h4>\n<p>We can extract specific files from the compressed file for that we need to specify the file name along with the directory name if it is inside the directory.<\/p>\n<pre>$ tar -xvf compress_sample.tar sample.txt student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"57\" class=\"wp-image-6338\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-16.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-16.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-16-300x23.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Extracting specific files using tar.<\/p>\n<h3>Some Examples of Zip and Unzip Command.<\/h3>\n<p>Syntax for zip and unzip are as follows:<\/p>\n<pre>$ zip [option] [archive_name] [Files]<\/pre>\n<pre>$ unzip [option] [archive_name]<\/pre>\n<h4>Compressing Files Using Zip<\/h4>\n<p>Zip is the archive compressor so we don\u2019t need to provide any extra parameter to compress the files. For compression we need to run the command like this:<\/p>\n<pre>$ zip zip_demo.zip *.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"146\" class=\"wp-image-6339\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-17.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-17.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-17-300x60.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Zip multiple files<\/p>\n<h4>Unzipping File to Different Directory<\/h4>\n<p>We can unzip the zip files to the different directories using the -d option and specifying the directory in the command.<\/p>\n<pre>$ unzip zip_demo.zip -d Documents\/<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"128\" class=\"wp-image-6340\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-18.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-18.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-18-300x53.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Unzip the file to a different directory.<\/p>\n<h4>Unzipping Specific Files From Compress File<\/h4>\n<p>To unzip a specific file from the archive the command must be executed in the following way.<\/p>\n<pre>$ unzip zip_demo.zip student.txt sample.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"94\" class=\"wp-image-6341\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-19.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-19.jpeg 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-19-300x39.jpeg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Unzip specific files from the zip file.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this article, we learn how to archive and compress files using tar and zip commands. I have included the most common example in the article hope this will help you how to compress files.<\/p>","protected":false},"excerpt":{"rendered":"<p>Zip and tar are popular command-line utility for compressing and archiving multiple directories and files into one archive file in a Linux system. By default, the tar command&hellip;<\/p>","protected":false},"author":1,"featured_media":6345,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[321,322],"class_list":["post-6333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-tar","tag-zip"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=6333"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6333\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/6345"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=6333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=6333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=6333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}