{"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[pastacode lang=\u201dbash\u201d manual=\u201dtar%20-zcvf%20archive-name.tar.gz%20directory-name%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dtar%20-zcvf%20archive-name.tar.gz%20directory-name%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20-zcvf%20wiki.tar.gz%20%2Fhome%2Fwikitechy%2Fprog%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\">[ad type=\u201dbanner\u201d]<\/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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20-zxvf%20wiki.tar.gz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20-zxvf%20wiki.tar.gz%20-C%20%2Ftmpwiki%0A%24%20cd%20%2Ftmpwiki%0A%24%20ls%20-%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u201czip\u201d a directory, the correct command would be mentioned bellow :<\/span><\/li>\n<\/ul>\n[pastacode lang=\u201dbash\u201d manual=\u201dtar%20-zcvf%20archive.tar.gz%20directory%2F%20%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2019t 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[pastacode lang=\u201dbash\u201d manual=\u201dtar%20-cv%20directory%20%7C%20gzip%20%3E%20archive.tar.gz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dtar%20-zxvf%20archive.tar.gz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li><span style=\"color: #000000;\">That command is effectively the same as<\/span><\/li>\n<\/ul>\n[pastacode lang=\u201dbash\u201d manual=\u201dgunzip%20%3C%20archive.tar.gz%20%7C%20tar%20-xv%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tree%20dir1%2F%0Adir1%2F%0A%7C\u2013%20dir11%0A%7C%20%20%20%7C\u2013%20file11%0A%7C%20%20%20%7C\u2013%20file12%0A%7C%20%20%20%60\u2013%20file13%0A%7C\u2013%20file1%0A%7C\u2013%20file2%0A%60\u2013%20file3%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20gzip%20-r%20dir1%0Aafter%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\"><b>After running the gzip command<\/b><\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tree%20dir1%2F%0Adir1%2F%0A%7C\u2013%20dir11%0A%7C%20%20%20%7C\u2013%20file11.gz%0A%7C%20%20%20%7C\u2013%20file12.gz%0A%7C%20%20%20%60\u2013%20file13.gz%0A%7C\u2013%20file1.gz%0A%7C\u2013%20file2.gz%0A%60\u2013%20file3.gz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\">[ad type=\u201dbanner\u201d]<\/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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20zcvf%20dir1.tar.gz%20dir1%2F%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\"><b>Example:<\/b><\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20zcvf%20dir1.tar.gz%20dir1%2F%0Adir1%2F%0Adir1%2Ffile1%0Adir1%2Ffile2%0Adir1%2Fdir11%2F%0Adir1%2Fdir11%2Ffile11.gz%0Adir1%2Fdir11%2Ffile12.gz%0Adir1%2Fdir11%2Ffile13.gz%0Adir1%2Ffile3\u2033 message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\"><b>Which results in the following single file :<\/b><\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24%20ls%20-l%20%7C%20grep%20tar%0A-rw-rw-r\u2013%201%20saml%20saml%20%20271%20Nov%20%201%2008%3A07%20dir1.tar.gz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\"><b>Finally we can confirm its contents:<\/b><\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20ztvf%20dir1.tar.gz%20%0Adrwxrwxr-x%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2008%3A05%20dir1%2F%0A-rw-rw-r\u2013%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2007%3A45%20dir1%2Ffile1%0A-rw-rw-r\u2013%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2007%3A45%20dir1%2Ffile2%0Adrwxrwxr-x%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2008%3A04%20dir1%2Fdir11%2F%0A-rw-rw-r\u2013%20saml%2Fsaml%20%20%20%20%20%20%20%2027%202017-10-01%2007%3A45%20dir1%2Fdir11%2Ffile11.gz%0A-rw-rw-r\u2013%20saml%2Fsaml%20%20%20%20%20%20%20%2027%202017-10-01%2007%3A45%20dir1%2Fdir11%2Ffile12.gz%0A-rw-rw-r\u2013%20saml%2Fsaml%20%20%20%20%20%20%20%2027%202017-10-01%2007%3A45%20dir1%2Fdir11%2Ffile13.gz%0A-rw-rw-r\u2013%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2007%3A45%20dir1%2Ffile3%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\"><strong>How to use:<\/strong><\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dcd%20%2Fhome%2F%3B%20gzipdir%20MyDirectory%20%20%20%20\u2033 message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li><span style=\"color: #000000;\">It will create \/home\/MyDirectory.tgz and remove MyDirectory<\/span><\/li>\n<\/ul>\n[pastacode lang=\u201dbash\u201d manual=\u201dgunzipdir%20%2Fhome%2FMyDirectory.tgz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20\u2013help%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-z%2C%20-j%2C%20-J%2C%20\u2013lzma%20%20Compress%20archive%20with%20gzip%2Fbzip2%2Fxz%2Flzma%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\">Here is an example:<\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dtar%20-zcvf%20outputFileName%20folderToCompress%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20-zcvf%20wikitechy.tar.gz%20wikitechy%2F%0A%0Aa%20wikitechy%0Aa%20wikitechy%2F.DS_Store%0Aa%20wikitechy%2FCHANGELOG%0Aa%20wikitechy%2Fextension-example.xml%0Aa%20wikitechy%2Fextension.xsd%0Aa%20wikitechy%2FLICENSE%0Aa%20wikitechy%2Fwikitechy-api-sources.jar%0Aa%20wikitechy%2Fwikitechy-api.jar%0Aa%20wikitechy%2Fwikitechy.jar%0Aa%20wikitechy%2Fwikitechy.yml%0Aa%20wikitechy%2Fnrcerts%0Aa%20wikitechy%2FREADME.txt%0A%0A%24%20ls%20-lsa%0A%0A%20%20%20%20%200%20drwxr-xr-x%20%2013%20mkyong%20%20staff%20%20%20%20%20%20%20%20442%20Mar%2024%2021%3A40%20wikitechy%0A%2011072%20-rw-r\u2013r\u2013%20%20%201%20mkyong%20%20staff%20%20%20%205667938%20Sep%2023%2015%3A44%20wikitechy.tar.gz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\">[ad type=\u201dbanner\u201d]<\/span><\/p>\n<p><span style=\"color: #993300;\"><b>2.List the contents of wikitechy.tar.gz:<\/b><\/span><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24%20tar%20-tf%20wikitechy.tar.gz%0Awikitechy%2F%0Awikitechy%2F._.DS_Store%0Awikitechy%2F.DS_Store%0Awikitechy%2F._CHANGELOG%0Awikitechy%2FCHANGELOG%0Awikitechy%2F._extension-example.xml%0Awikitechy%2Fextension-example.xml%0Awikitechy%2F._extension.xsd%0Awikitechy%2Fextension.xsd%0Awikitechy%2F._LICENSE%0Awikitechy%2FLICENSE%0Awikitechy%2F._wikitechy-api-sources.jar%0Awikitechy%2Fwikitechy-api-sources.jar%0Awikitechy%2F._wikitechy-api.jar%0Awikitechy%2Fwikitechy-api.jar%0Awikitechy%2F._wikitechy.jar%0Awikitechy%2Fwikitechy.jar%0Awikitechy%2F._wikitechy.yml%0Awikitechy%2Fwikitechy.yml%0Awikitechy%2F._nrcerts%0Awikitechy%2Fnrcerts%0Awikitechy%2F._README.txt%0Awikitechy%2FREADME.txt%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}