{"id":7243,"date":"2018-11-16T13:47:50","date_gmt":"2018-11-16T10:47:50","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-zip-command\/"},"modified":"2018-11-16T13:47:50","modified_gmt":"2018-11-16T10:47:50","slug":"linux-zip-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-zip-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux zip Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<div><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-zip-command-tutorial-for-beginners-5-examples.jpg\" class=\"ff-og-image-inserted\" alt=\"\" title=\"\"><\/div>\n<p>As the name suggests, the zip command lets you create archives. In this tutorial, we will discuss the basics of zip using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-zip-command\">Linux zip command<\/h2>\n<p>The zip command line Linux lets you package and compress (archive) files. Following is its syntax:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">zip [OPTIONS] archive_name list_of_files<\/code><\/pre>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre readability=\"4.5\"><code spellcheck=\"false\" readability=\"3\">zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS\/2, Windows 9x\/NT\/XP, <br\/>Minix, Atari, Macintosh, Amiga, and Acorn RISC OS.\u00a0 <p>It is analogous to a combination of the Unix commands tar(1) and compress(1) and is\u00a0 compatible\u00a0 <br\/>with\u00a0 PKZIP\u00a0 (Phil Katz's ZIP for MSDOS systems).<\/p><\/code><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how the zip command works.<\/p>\n<h2 id=\"q-how-to-use-the-zip-command\">Q1. How to use the zip command?<\/h2>\n<p>Basic usage is fairly easy &#8211; just provide the zip file name and files to be compressed as input. Here&#8217;s one example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">zip files.zip file1.txt file2.txt file3.txt<\/code><\/pre>\n<p>So the idea behind this command is to compress the three .txt files into a .zip file. Here&#8217;s the output this command produced:<\/p>\n<pre><code spellcheck=\"false\">adding: file1.txt (stored 0%)<br\/>adding: file2.txt (stored 0%)<br\/>adding: file3.txt (stored 0%)<\/code><\/pre>\n<p>And a file named &#8216;files.zip&#8217; was produced in the current working directory.<\/p>\n<h2 id=\"q-how-to-delete-a-file-from-an-archive-compressed-file\">Q2. How to delete a file from an archive (compressed) file?<\/h2>\n<p>This can be done using the -d command line option. For example, to remove file3.txt from the files.zip file, I executed the zip command in the following way:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">zip -d files.zip file3.txt<\/code><\/pre>\n<p>The tool notified me of the delete operation through the following message:<\/p>\n<pre><code spellcheck=\"false\">deleting: file3.txt<\/code><\/pre>\n<h2 id=\"q-how-to-add-new-files-to-existing-compressed-archive-file\">Q3. How to add new files to existing compressed (archive) file?<\/h2>\n<p>Just like the deletion operation is carried out through -d, in case you want to add new files to an existing compressed file, you can do that using the -u command line option.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">zip -u files.zip file3.txt file4.txt<\/code><\/pre>\n<p>Here are the messages this command produced in output:<\/p>\n<pre><code spellcheck=\"false\">adding: file3.txt (stored 0%)<br\/>adding: file4.txt (stored 0%)<\/code><\/pre>\n<h2 id=\"q-how-to-make-zip-delete-original-files-after-archiving\">Q4. How to make zip delete original files after archiving?<\/h2>\n<p>By default, the original files don&#8217;t get deleted even after zip creates a compressed file. However, if you want, you can force the tool to delete original files. This can be done using the -m command line option.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">zip -m files.zip file1.txt file2.txt file3.txt file4.txt<\/code><\/pre>\n<p>This command created files.zip in output while deleting all the .txt files in the process.<\/p>\n<h2 id=\"q-what-are-some-other-useful-zip-command-options\">Q5. What are some other useful zip command options?<\/h2>\n<p>One command line option you&#8217;ll likely require is -x. It is used when you want to leave some files from being compressed. So what you have to do is, you have to specify -x followed by name of the compressed file followed by the names of files you want to exclude.<\/p>\n<p>For example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">zip files.zip file2.txt<\/code><\/pre>\n<p>This command will compress all files present in the current working directory, except file2.txt.<\/p>\n<p>Moving on, the other command line option you may require is -r. It lets you recursively compress, something which is required when you want to compress directories (including of course their contents).<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>We&#8217;ve just scratched the surface here as the zip command offers a lot of other features as well. Once you are done practicing the examples we&#8217;ve discussed here, you can head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/zip\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As the name suggests, the zip command lets you create archives. In this tutorial, we will discuss the basics of zip using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine. Linux zip command The zip command line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7243","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7243"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7243\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}