{"id":778,"date":"2017-03-18T15:58:37","date_gmt":"2017-03-18T10:28:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=778"},"modified":"2017-03-29T14:58:27","modified_gmt":"2017-03-29T09:28:27","slug":"can-i-zip-an-entire-folder-using-gzip","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/can-i-zip-an-entire-folder-using-gzip\/","title":{"rendered":"Can I zip an entire folder using gzip"},"content":{"rendered":"<h4 id=\"generally-we-can-zip-compress-the-entire-folder-whole-linux-or-unix-directory\"><span style=\"color: #ff6600;\"><b>Generally we can Zip\/compress the <\/b><b>entire folder <\/b><b>\/ <\/b><b>Whole<\/b><b> Linux or UNIX <\/b><b>Directory<\/b><\/span><\/h4>\n<ul>\n<li><span style=\"color: #000000;\">Compressing a whole linux\/UNIX directory is easy, certainly in terms of technical basis it is called as a compressed archive.<\/span><\/li>\n<li><span style=\"color: #000000;\">GNU tar command is best for this work.<\/span><\/li>\n<li><span style=\"color: #000000;\">It can be used on remote linux or UNIX server.<\/span><\/li>\n<li><span style=\"color: #000000;\">It performs two things:<\/span>\n<ol>\n<li><span style=\"color: #000000;\"><b>Create the archive<\/b><\/span><\/li>\n<li><span style=\"color: #000000;\"><b>Compress the archive<\/b><\/span><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>Syntax of tar command :<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar -zcvf archive-name.tar.gz directory-name<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><b>Where,<\/b><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">-z : Compress archive using gzip program<\/span><\/li>\n<li><span style=\"color: #000000;\">-c : Create archive<\/span><\/li>\n<li><span style=\"color: #000000;\">-v : Verbose i.e. display progress while creating archive<\/span><\/li>\n<li><span style=\"color: #000000;\">-f : Archive File name<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>Syntax of tar command :<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar -zcvf archive-name.tar.gz directory-name<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">Considering, we have a directory called <strong>\/home\/wikitechy\/prog<\/strong> and we would wish to compress this directory then we need to type <strong>tar command as follows:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar -zcvf wiki.tar.gz \/home\/wikitechy\/prog<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">[ad type=&#8221;banner&#8221;]<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Above command will create an archive file called wiki.tar.gz in current directory.<\/span><\/li>\n<li><span style=\"color: #000000;\">To restore the archive we need to use the following command (it will extract all files in current directory):<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar -zxvf wiki.tar.gz<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">Where-<strong>x<\/strong> : Extract files<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">To extract files in particular directory, for example in \/<strong>tmpwiki<\/strong> then we need to use the following command<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar -zxvf wiki.tar.gz -C \/tmpwiki<br\/>$ cd \/tmpwiki<br\/>$ ls -<\/code><\/pre> <\/div>\n<h4 id=\"trying-to-zip-a-folder-in-unix-can-be-done-using-the-gzip-command\"><span style=\"color: #800000;\"><strong>Trying to zip a folder in unix can be done using the gzip command<\/strong><\/span><\/h4>\n<ul>\n<li><span style=\"color: #000000;\">We can use gzip -r myfolder which will gzip files recursively.<\/span><\/li>\n<li><span style=\"color: #000000;\">The same way we can unzip files using gunzip -r myfolder which will unzip files recursively.<\/span><\/li>\n<li><span style=\"color: #000000;\">At some cases we can Use tar; it supports options for compression.<\/span><\/li>\n<li><span style=\"color: #000000;\">gzip command is designed as a complement to tar, not as a replacement.<\/span><\/li>\n<li><span style=\"color: #000000;\">Zipping an entire folder using gzip is not possible , because Unlike zip, gzip functions as a compression algorithm only.<\/span><\/li>\n<li><span style=\"color: #000000;\">Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip, bzip2, 7zip, etc.<\/span><\/li>\n<li><span style=\"color: #000000;\">In order to &#8220;zip&#8221; a directory, the correct command would be mentioned bellow :<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar -zcvf archive.tar.gz directory\/ <\/code><\/pre> <\/div>\n<ul>\n<li><span style=\"color: #000000;\">This will tell tar to c (create) an archive from the files in directory (tar is recursive by default), compress it using the z (gzip) algorithm, store the output as a f (file) named archive.tar.gz, and v (verbosely) list (on \/dev\/stderr so it doesn&#8217;t affect piped commands) all the files it adds to the archive<\/span><\/li>\n<\/ul>\n<ul>\n<li><span style=\"color: #000000;\">The <b>tar<\/b> command offers <b>gzip<\/b> support (via the <b>-z<\/b> flag).<\/span><\/li>\n<li><span style=\"color: #000000;\">The <b>gzip<\/b> command\/lib is completely separate. The command above is effectively the same as<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar -cv directory | gzip &gt; archive.tar.gz<\/code><\/pre> <\/div>\n<ul>\n<li><span style=\"color: #000000;\">To decompress and unpack the archive into the current directory we would use<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar -zxvf archive.tar.gz<\/code><\/pre> <\/div>\n<ul>\n<li><span style=\"color: #000000;\">That command is effectively the same as<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">gunzip &lt; archive.tar.gz | tar -xv<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">Using the -r switch , the gzip command will not recursively compress a directory into a single zip file, rather it will go into that directory structure and zip each file that it finds into a separate file.<\/span><\/p>\n<p><span style=\"color: #000000;\"><b>Example:<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\">Before running the gzip command<\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tree dir1\/<br\/>dir1\/<br\/>|-- dir11<br\/>|   |-- file11<br\/>|   |-- file12<br\/>|   `-- file13<br\/>|-- file1<br\/>|-- file2<br\/>`-- file3<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><b>Example:<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\"><b>Now on executing the gzip command<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ gzip -r dir1<br\/>after<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><b>After running the gzip command<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tree dir1\/<br\/>dir1\/<br\/>|-- dir11<br\/>|   |-- file11.gz<br\/>|   |-- file12.gz<br\/>|   `-- file13.gz<br\/>|-- file1.gz<br\/>|-- file2.gz<br\/>`-- file3.gz<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">[ad type=&#8221;banner&#8221;]<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">To zip up the directory structure , use the tar command, and then compress the resulting .<strong>tar<\/strong> file<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar zcvf dir1.tar.gz dir1\/<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><b>Example:<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar zcvf dir1.tar.gz dir1\/<br\/>dir1\/<br\/>dir1\/file1<br\/>dir1\/file2<br\/>dir1\/dir11\/<br\/>dir1\/dir11\/file11.gz<br\/>dir1\/dir11\/file12.gz<br\/>dir1\/dir11\/file13.gz<br\/>dir1\/file3<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><b>Which results in the following single file :<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ ls -l | grep tar<br\/>-rw-rw-r-- 1 saml saml  271 Nov  1 08:07 dir1.tar.gz<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><b>Finally we can confirm its contents:<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar ztvf dir1.tar.gz <br\/>drwxrwxr-x saml\/saml         0 2017-10-01 08:05 dir1\/<br\/>-rw-rw-r-- saml\/saml         0 2017-10-01 07:45 dir1\/file1<br\/>-rw-rw-r-- saml\/saml         0 2017-10-01 07:45 dir1\/file2<br\/>drwxrwxr-x saml\/saml         0 2017-10-01 08:04 dir1\/dir11\/<br\/>-rw-rw-r-- saml\/saml        27 2017-10-01 07:45 dir1\/dir11\/file11.gz<br\/>-rw-rw-r-- saml\/saml        27 2017-10-01 07:45 dir1\/dir11\/file12.gz<br\/>-rw-rw-r-- saml\/saml        27 2017-10-01 07:45 dir1\/dir11\/file13.gz<br\/>-rw-rw-r-- saml\/saml         0 2017-10-01 07:45 dir1\/file3<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\"><strong>How to use:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">cd \/home\/; gzipdir MyDirectory    <\/code><\/pre> <\/div>\n<ul>\n<li><span style=\"color: #000000;\">It will create \/home\/MyDirectory.tgz and remove MyDirectory<\/span><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">gunzipdir \/home\/MyDirectory.tgz<\/code><\/pre> <\/div>\n<ul>\n<li><span style=\"color: #000000;\">It will create \/home\/MyDirectory and remove \/home\/MyDirectory.tgz<\/span><\/li>\n<li><span style=\"color: #000000;\">In Linux, using gzip its unable to compress a folder, since its being used to compress a single file only.<\/span><\/li>\n<li><span style=\"color: #000000;\">To compress a folder, we should use tar + gzip, which is tar -z<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>Note:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar --help<br\/>                 -z, -j, -J, --lzma  Compress archive with gzip\/bzip2\/xz\/lzma<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">Here is an example:<\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar -zcvf outputFileName folderToCompress<\/code><\/pre> <\/div>\n<h4 id=\"how-to-gzip-a-folder-in-linux\"><span style=\"color: #800080;\"><b>How to gzip a folder in Linux:<\/b><\/span><\/h4>\n<p><span style=\"color: #993300;\"><b>1<\/b><b>. Tar + Gzip a folder:<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\">Lets see the example to gzip a folder named \u201c<b>wikitechy<\/b>\u201d, and also all the files within that folder, into a single compressed file <b>wikitechy.tar.gz<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar -zcvf wikitechy.tar.gz wikitechy\/<br\/><br\/>a wikitechy<br\/>a wikitechy\/.DS_Store<br\/>a wikitechy\/CHANGELOG<br\/>a wikitechy\/extension-example.xml<br\/>a wikitechy\/extension.xsd<br\/>a wikitechy\/LICENSE<br\/>a wikitechy\/wikitechy-api-sources.jar<br\/>a wikitechy\/wikitechy-api.jar<br\/>a wikitechy\/wikitechy.jar<br\/>a wikitechy\/wikitechy.yml<br\/>a wikitechy\/nrcerts<br\/>a wikitechy\/README.txt<br\/><br\/>$ ls -lsa<br\/><br\/>     0 drwxr-xr-x  13 mkyong  staff        442 Mar 24 21:40 wikitechy<br\/> 11072 -rw-r--r--   1 mkyong  staff    5667938 Sep 23 15:44 wikitechy.tar.gz<\/code><\/pre> <\/div>\n<p><span style=\"color: #000000;\">[ad type=&#8221;banner&#8221;]<\/span><\/p>\n<p><span style=\"color: #993300;\"><b>2.List the contents of wikitechy.tar.gz:<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ tar -tf wikitechy.tar.gz<br\/>wikitechy\/<br\/>wikitechy\/._.DS_Store<br\/>wikitechy\/.DS_Store<br\/>wikitechy\/._CHANGELOG<br\/>wikitechy\/CHANGELOG<br\/>wikitechy\/._extension-example.xml<br\/>wikitechy\/extension-example.xml<br\/>wikitechy\/._extension.xsd<br\/>wikitechy\/extension.xsd<br\/>wikitechy\/._LICENSE<br\/>wikitechy\/LICENSE<br\/>wikitechy\/._wikitechy-api-sources.jar<br\/>wikitechy\/wikitechy-api-sources.jar<br\/>wikitechy\/._wikitechy-api.jar<br\/>wikitechy\/wikitechy-api.jar<br\/>wikitechy\/._wikitechy.jar<br\/>wikitechy\/wikitechy.jar<br\/>wikitechy\/._wikitechy.yml<br\/>wikitechy\/wikitechy.yml<br\/>wikitechy\/._nrcerts<br\/>wikitechy\/nrcerts<br\/>wikitechy\/._README.txt<br\/>wikitechy\/README.txt<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Generally we can Zip\/compress the entire folder \/ Whole Linux or UNIX Directory Compressing a whole linux\/UNIX directory is easy, certainly in terms of technical basis it is called as a compressed archive. GNU tar command is best for this work. It can be used on remote linux or UNIX server. It performs two things: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699,1331],"tags":[1457,1438,1437,1439,1380,1446,1440,1456,1453,1452,1450,1460,1449,1451,1455,1454,1443,1441,1442,1444,1447,1445,1459,1448,1458],"class_list":["post-778","post","type-post","status-publish","format-standard","hentry","category-linux","category-unix","tag-gz-file-extract","tag-gzip-a-directory-in-linux","tag-gzip-all-files-in-a-directory","tag-gzip-all-files-in-a-directory-recursively","tag-gzip-command-in-linux","tag-gzip-compression","tag-gzip-folder-and-subfolders","tag-how-to-compress-zip-files","tag-how-to-extract-tar-file","tag-how-to-extract-zip-file-in-linux","tag-how-to-tar-a-directory-in-linux","tag-how-to-untar-a-tar-file-in-linux","tag-how-to-unzip-gz-file-in-linux","tag-how-to-unzip-gz-file-in-unix","tag-how-to-zip-a-directory-in-linux","tag-how-to-zip-a-file-in-unix","tag-how-to-zip-a-folder-in-ubuntu","tag-linux-unzip-folder","tag-linux-zip-folder-and-subfolders","tag-tar-command","tag-tar-command-in-linux","tag-tar-file","tag-what-is-tar-file-how-to-open-tar-file","tag-zip-command-in-linux","tag-zip-file-in-linux"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=778"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/778\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}