{"id":125,"date":"2021-11-16T02:44:23","date_gmt":"2021-11-16T02:44:23","guid":{"rendered":"https:\/\/bestdedicatedhosting.in\/blog\/?p=125"},"modified":"2021-11-16T05:08:45","modified_gmt":"2021-11-16T05:08:45","slug":"how-to-zip-and-unzip-in-linux-with-examples","status":"publish","type":"post","link":"https:\/\/bestdedicatedhosting.in\/blog\/how-to-zip-and-unzip-in-linux-with-examples\/","title":{"rendered":"How to zip and unzip in Linux with Examples"},"content":{"rendered":"\n<p><strong>Description<\/strong><\/p>\n\n\n\n<p>A Zip archive is a compressed file format that supports lossless data compression.<\/p>\n\n\n\n<p> A zip archive might contain multiple files and directories inside it. <\/p>\n\n\n\n<p>Zip is a very popular archive file format. <\/p>\n\n\n\n<p>Almost every operating system supports zip including all the operating systems by Microsoft as well as all the Linux operating systems. <\/p>\n\n\n\n<p>In order to use Zip and Unzip in Linux, we have to first install Zip and Unzip utilities on our Linux operating system. <\/p>\n\n\n\n<p>Before we move on to the installation part, make sure you have sudo privilege on your Linux server. <\/p>\n\n\n\n<p>We need sudo privilege to install new software packages on our server.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Install Zip and Unzip on CentOS<\/strong><\/p>\n\n\n\n<p>Execute the following commands on your server to install Zip and Unzip on CentOS server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo yum update<\/code>\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo yum install zip unzip<\/code><\/pre>\n\n\n\n<p>Once the process is complete, execute the&nbsp;<code>zip<\/code>&nbsp;command to verify the installation. Once the installation is done, we can move on to learn how to zip and unzip in Linux operating systems.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>How to Zip in Linux<\/strong><\/p>\n\n\n\n<p>Zip is a very simple and straightforward utility. Let us first see the syntax of zip command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">zip [OPTIONS...] ZIP_FILE_NAME [FILES ...]\n<\/pre>\n\n\n\n<p>Options in zip command allows us to control the execution of the command. <\/p>\n\n\n\n<p>Then comes the ZIP_FILE_NAME, we have to replace this with the name of the zip file we want to create, for example,&nbsp;<code>example.zip<\/code>. <\/p>\n\n\n\n<p>Then, you can list all the files that you want to include in this zip archive.<\/p>\n\n\n\n<p><strong>COMPRESS INDIVIDUAL FILES<\/strong><\/p>\n\n\n\n<p>For example, Let\u2019s say we have&nbsp;<strong>file1.txt<\/strong>&nbsp;and&nbsp;<strong>file2.csv<\/strong>&nbsp;that we want to zip. <\/p>\n\n\n\n<p>To compress these files inside a zip archive, we have to run the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>zip files.zip file1.txt file2.csv<\/code><\/pre>\n\n\n\n<p>This command will create a zip archive called&nbsp;<code>files.zip<\/code>&nbsp;in the current directory.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>COMPRESS A DIRECTORY<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, Let\u2019s say we have a directory called&nbsp;<code>wordpress<\/code>&nbsp;that we want to compress in a zip archive. <\/p>\n\n\n\n<p>To create a zip archive of a directory, we will use the&nbsp;<code>-r<\/code>&nbsp;option in zip command. <\/p>\n\n\n\n<p>The&nbsp;<code>-r<\/code>&nbsp;option will make the process recursive, it means that the zip archive will contain every file and subdirectories contained inside the directory we want to compress.<\/p>\n\n\n\n<p>Here is how we can compress our&nbsp;<code>wordpress<\/code>&nbsp;directory including all the files and directories inside it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>zip -r wordpress_backup.zip wordpress<\/code><\/pre>\n\n\n\n<p><strong>COMPRESS EVERYTHING IN CURRENT DIRECTORY<\/strong><\/p>\n\n\n\n<p>If you want to compress everything in the current directory, navigate to the directory and execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>zip -r backup.zip *<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>*<\/code>&nbsp;stands for wildcard character. It means that it will match all the directories and files inside the current directory. Now, Let us see how to password protect our zip archive.<\/p>\n\n\n\n<p><strong>PASSWORD PROTECT A ZIP ARCHIVE<\/strong><\/p>\n\n\n\n<p>Zip also allows you to password protect a zip archive. <\/p>\n\n\n\n<p>However, this feature is not recommended because the password we will pass while executing the command will be in a plain text format. <\/p>\n\n\n\n<p>It is good only if no one is looking over your shoulder.<\/p>\n\n\n\n<p>To create a zip archive of our example&nbsp;<code>wordpress<\/code>&nbsp;directory with password protection, execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>zip -r --password=PASSWORD wordpress<\/code><\/pre>\n\n\n\n<p>This command will create a password protected zip archive containing our example wordpress directory with all the files inside it. It will ask for a password while using the&nbsp;<code>unzip<\/code>&nbsp;utility.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>How to unzip in Linux<\/strong><\/p>\n\n\n\n<p>Unzip is an utility in linux like zip. Unzip utility allows us to extract files from a zip archive. The simple syntax of unzip command is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unzip [OPTIONS] [ZIP_ARCHIVE] [FILES_TO_PROCESS] [-x FILES_TO_EXCLUDE]<\/code><\/pre>\n\n\n\n<p>The options allow us to control the execution of the command. <\/p>\n\n\n\n<p>After options, we have to specify the name of the zip archive that we want to extract. <\/p>\n\n\n\n<p>Then comes the&nbsp;<strong>two&nbsp;optional sections<\/strong>&nbsp;in which you can specify the files that you want to process and the files that you want to exclude.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>TEST VALIDITY OF ZIP ARCHIVE<\/strong><\/p>\n\n\n\n<p>For example, Let\u2019s say we have a zip archive called&nbsp;<code>wordpress.zip<\/code>&nbsp;that we want to extract. <\/p>\n\n\n\n<p>But first, we want to test the zip archive validity. <\/p>\n\n\n\n<p>To test the validity of our&nbsp;<code>wordpress.zip<\/code>&nbsp;archive, execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unzip -tq wordpress.zip<\/code><\/pre>\n\n\n\n<p>The output of the command should look like the following if the zip archive is valid.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:<code>\nNo errors detected in compressed data of wordpress.zip.<\/code><\/pre>\n\n\n\n<p><strong>LIST ALL THE FILES INSIDE ZIP ARCHIVE<\/strong><\/p>\n\n\n\n<p>Now, to take a look at all the files inside the zip archive, we can use the&nbsp;<code>-l<\/code>&nbsp;option. <\/p>\n\n\n\n<p>So, to get list of all the files and directories inside our zip archive without extracting it, we have to execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unzip -l wordpress.zip<\/code><\/pre>\n\n\n\n<p>This command will give you the list of every file and directory compressed in the archive. <\/p>\n\n\n\n<p>It will also list files inside directories and sub-directories as the function is recursive.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>UNZIP A ZIP ARCHIVE<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, Let&#8217;s extract our example&nbsp;<code>wordpress.zip<\/code>&nbsp;archive. To extract the full archive, execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unzip wordpress.zip<\/code>\n<\/pre>\n\n\n\n<p><strong>UNZIP A ZIP ARCHIVE EXCEPT FEW FILES<\/strong><\/p>\n\n\n\n<p>Now, Let&#8217;s say we want to extract all the files, but we do not want our&nbsp;<code>wp-config.php<\/code>&nbsp;file from our WordPress backup because we want to create a new file. <\/p>\n\n\n\n<p>In that case, we can use&nbsp;<code>-x<\/code>&nbsp;option to specify the list of files we want to ignore. <\/p>\n\n\n\n<p>To do this, we have to execute the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unzip wordpress.zip -x wp-config.php<\/code><\/pre>\n\n\n\n<p>This command will extract all the files from our example wordpress zip archive, except&nbsp;<code>wp-config.php<\/code>&nbsp;file. <\/p>\n\n\n\n<p>So, this is how we can use the unzip utility to extract zip archives in linux based operating systems. <\/p>\n\n\n\n<p>Just like zip and unzip, you can use&nbsp;tar command in linux&nbsp;to extract and compress files in linux.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We hope you\u2019ve found this useful!&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description A Zip archive is a compressed file format that supports lossless data compression. A zip archive might contain multiple files and directories inside it. Zip is a very popular archive file format. Almost every operating system supports zip including all the operating systems by Microsoft as well as all the Linux operating systems. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-linux"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":8,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}