{"id":15819,"date":"2019-03-05T07:43:41","date_gmt":"2019-03-05T07:43:41","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15819"},"modified":"2025-04-28T07:58:34","modified_gmt":"2025-04-28T07:58:34","slug":"how-to-unzip-files-linux","status":"publish","type":"post","link":"\/tutorials\/how-to-unzip-files-linux","title":{"rendered":"How to use unzip command in Linux"},"content":{"rendered":"<p>To unzip, Linux users can apply a command utility similar to other compression formats, such as <strong>tar <\/strong>and <strong>gzip<\/strong>. Understanding how to use the <strong>unzip<\/strong> command helps improve efficiency with various tasks, including software distribution or data archiving.<\/p><p>Unlike the two utilities, <strong>unzip<\/strong> is not installed by default. It also has different options to change the extraction behavior and is suitable for a particular development scenario.<\/p><p>Continue reading as we explain how to install and use <strong>unzip <\/strong>to extract zip files in a Linux virtual private server (VPS). We will also discuss how it differs from other unpacking commands and its best practices.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/public\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-unzip\">How to Install Unzip<\/h2><p>In this section, we will go over the steps to install<strong> unzip <\/strong>in <a href=\"\/tutorials\/best-linux-distro\">popular Linux distributions<\/a> like <strong>Debian<\/strong> and <strong>CentOS<\/strong>. To run the commands remotely, connect to your VPS <a href=\"\/tutorials\/how-to-use-putty-ssh\">using an SSH client like PuTTY<\/a>.<\/p><p><strong>Debian Derivatives<\/strong><\/p><p>For Debian-based operating systems like<strong> Ubuntu<\/strong>, use the APT package manager to install <strong>unzip<\/strong> in Linux. Before doing so, update and upgrade the repository with the following command:<\/p><pre class=\"wp-block-preformatted\">sudo apt update &amp;&amp; sudo apt upgrade<\/pre><p>Then, install <strong>unzip<\/strong> using this:<\/p><pre class=\"wp-block-preformatted\">sudo apt install unzip<\/pre><p>If you use an older operating system, apply the following command instead:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get install unzip<\/pre><p><strong>Red Hat Linux Enterprise (RHEL) Derivatives<\/strong><\/p><p>Commands for RHEL-based operating systems like <strong>CentOS<\/strong>,<strong> AlmaLinux<\/strong>, and <strong>Rocky Linux <\/strong>differ depending on their version. The older releases use the<strong> yum<\/strong> package manager, while the newer ones utilize <strong>dnf<\/strong>.<\/p><p>Read their documentation or use websites like <a href=\"https:\/\/distrowatch.com\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>DistroWatch<\/strong><\/a> to check your operating system&rsquo;s package manager. That said, the commands are similar:<\/p><pre class=\"wp-block-preformatted\">sudo yum update &amp;&amp; sudo yum upgrade<\/pre><pre class=\"wp-block-preformatted\">sudo dnf update &amp;&amp; sudo dnf upgrade<\/pre><p>Likewise, the <strong>unzip<\/strong> utility installation commands are as follows:<\/p><pre class=\"wp-block-preformatted\">sudo yum install unzip<\/pre><pre class=\"wp-block-preformatted\">sudo dnf install unzip<\/pre><p>After the process is finished, check whether <strong>unzip<\/strong> is successfully installed using the following command. It is the same for every distribution:<\/p><pre class=\"wp-block-preformatted\">unzip -v<\/pre><p>If the utility is configured correctly, your command-line interface should show the version number.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-returns-installed-version-of-unzip.png\"><img decoding=\"async\" width=\"886\" height=\"60\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-returns-installed-version-of-unzip.png\/public\" alt=\"Terminal returns the installed unzip version number\" class=\"wp-image-104136\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-returns-installed-version-of-unzip.png\/w=886,fit=scale-down 886w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-returns-installed-version-of-unzip.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-returns-installed-version-of-unzip.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-returns-installed-version-of-unzip.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-how-to-zip-and-unzip-files-in-linux\">How to Zip and Unzip Files in Linux<\/h2><p>After the <strong>unzip<\/strong> utility is installed, you can start extracting files on the Linux system. In this section, we will explain how to use the tool for several common use cases.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> The size limit setting in the file system might trigger an error when you unzip a file in Linux. If you encounter the issue, increase the threshold accordingly.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-unzip-a-file\">How to Unzip a File<\/h3><p>To unzip a file in Linux, use the command with the item name you want to unpack. Remember that the archive must also be in your current directory. Here is an example:<\/p><pre class=\"wp-block-preformatted\">unzip archive_file.zip<\/pre><p>It will extract the <strong>ZIP<\/strong> archive content into the current working directory if you have read-write permissions.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-files-from-an-archive.png\"><img decoding=\"async\" width=\"734\" height=\"112\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-files-from-an-archive.png\/public\" alt=\"The unzip command extracts files from an archive\" class=\"wp-image-104138\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-files-from-an-archive.png\/w=734,fit=scale-down 734w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-files-from-an-archive.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-files-from-an-archive.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 734px) 100vw, 734px\" \/><\/a><\/figure><\/div><p>You can also unzip zip files in a different directory by specifying the path like the following command:<\/p><pre class=\"wp-block-preformatted\">unzip path\/to\/the\/archive_file.zip<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/02\/unzip-command-extracts-an-archive-from-another-location.png\"><img decoding=\"async\" width=\"844\" height=\"106\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/02\/unzip-command-extracts-an-archive-from-another-location.png\/public\" alt=\"The unzip command extracts an archive from another location\" class=\"wp-image-104139\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/02\/unzip-command-extracts-an-archive-from-another-location.png\/w=844,fit=scale-down 844w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/02\/unzip-command-extracts-an-archive-from-another-location.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/02\/unzip-command-extracts-an-archive-from-another-location.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/02\/unzip-command-extracts-an-archive-from-another-location.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 844px) 100vw, 844px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-unzip-multiple-files\">How to Unzip Multiple Files<\/h3><p>To simultaneously extract multiple zip files in the same folder, use a wildcard like the following:<\/p><pre class=\"wp-block-preformatted\">unzip \\*.zip<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-multiple-archives.png\"><img decoding=\"async\" width=\"636\" height=\"191\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-multiple-archives.png\/public\" alt=\"The unzip command extracts multiple archives\" class=\"wp-image-104140\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-multiple-archives.png\/w=636,fit=scale-down 636w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-multiple-archives.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-extracts-multiple-archives.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><\/figure><\/div><p>The asterisk (<strong>*<\/strong>) symbol represents any character. For instance, run the following to unzip archive files with similar names, like <strong>file-1.zip<\/strong>, <strong>file-2.zip<\/strong>, and so on:<\/p><pre class=\"wp-block-preformatted\">unzip file-\\*.zip<\/pre><p>When extracting multiple<strong> ZIP <\/strong>files using a wildcard, ensure to use the backslash escape character (<strong>\\<\/strong>) to prevent Terminal from unpacking an archive from another.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-extract-files-to-a-specific-directory\">How to Extract Files to a Specific Directory<\/h3><p>Instead of using the<strong> cd<\/strong> command, use the <strong>-d<\/strong> option to unzip to a folder in another location. For example:<\/p><pre class=\"wp-block-preformatted\">unzip archive_file.zip -d \/target\/folder\/destination<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-an-archive-into-another-folder.png\"><img decoding=\"async\" width=\"884\" height=\"107\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-an-archive-into-another-folder.png\/public\" alt=\"The unzip command unpacks an archive into another folder\" class=\"wp-image-104141\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-an-archive-into-another-folder.png\/w=884,fit=scale-down 884w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-an-archive-into-another-folder.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-an-archive-into-another-folder.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-an-archive-into-another-folder.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 884px) 100vw, 884px\" \/><\/a><\/figure><\/div><p>The destination can be absolute or relative to the present working directory. For instance, if you are in the<strong> \/target <\/strong>directory, enter the following to extract the archive file to the same destination:<\/p><pre class=\"wp-block-preformatted\">unzip archive_file.zip -d \/directory\/destination<\/pre><p>This command also lets you unzip a file from one location to a different directory path without moving to either. The command looks like the following:<\/p><pre class=\"wp-block-preformatted\">unzip \/origin\/path\/archive_file.zip -d \/target\/folder\/destination<\/pre><h3 class=\"wp-block-heading\" id=\"h-how-to-do-partial-extraction-for-specific-files-or-folders\">How to Do Partial Extraction For Specific Files or Folders<\/h3><p>A <strong>ZIP<\/strong> file usually comprises several files or folders. In Linux, you can choose to extract a specific item by specifying their names after the<strong> ZIP <\/strong>archive like:<\/p><pre class=\"wp-block-preformatted\">unzip file_archive.zip file1.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-a-specific-file.png\"><img decoding=\"async\" width=\"730\" height=\"64\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-a-specific-file.png\/public\" alt=\"The unzip command unpacks a specific file\" class=\"wp-image-104142\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-a-specific-file.png\/w=730,fit=scale-down 730w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-a-specific-file.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-unpacks-a-specific-file.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 730px) 100vw, 730px\" \/><\/a><\/figure><\/div><p>To unpack multiple files, simply list their names after the command. You can also extract a specific folder:<\/p><pre class=\"wp-block-preformatted\">unzip file_archive.zip folder_name<\/pre><p>However, if you want to unpack a particular folder or file inside a directory, specify the full path. For example, to extract <strong>\/path<\/strong> inside the <strong>\/directory<\/strong> folder, enter:<\/p><pre class=\"wp-block-preformatted\">unzip file_archive.zip \/directory\/path<\/pre><p>You can also exclude specific files from the extraction using the<strong> -x<\/strong> option. For instance, we will keep <strong>file_1.txt <\/strong>and <strong>file_2.txt <\/strong>archived using this command:<\/p><pre class=\"wp-block-preformatted\">unzip file_archive.zip -x file_1.txt file_2.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-excludes-a-file-from-extraction.png\"><img decoding=\"async\" width=\"773\" height=\"67\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-excludes-a-file-from-extraction.png\/public\" alt=\"The unzip command excludes a file from the extraction\" class=\"wp-image-104143\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-excludes-a-file-from-extraction.png\/w=773,fit=scale-down 773w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-excludes-a-file-from-extraction.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-excludes-a-file-from-extraction.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-excludes-a-file-from-extraction.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/a><\/figure><\/div><p>Server administrators usually exclude files if the archive has a lot of content. They also do so to partially update a specific item, making the process more efficient since they don&rsquo;t have to work with much data.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-unzip-a-password-protected-zip-file\">How to Unzip a Password-Protected Zip File<\/h3><p>If you unzip password-protected ZIP files without entering the identification key, Terminal will ask for the credential.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-prompts-user-to-enter-a-password.png\"><img decoding=\"async\" width=\"773\" height=\"65\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-prompts-user-to-enter-a-password.png\/public\" alt=\"The unzip command prompts user to enter an archive file's password\" class=\"wp-image-104144\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-prompts-user-to-enter-a-password.png\/w=773,fit=scale-down 773w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-prompts-user-to-enter-a-password.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-prompts-user-to-enter-a-password.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-command-prompts-user-to-enter-a-password.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/a><\/figure><\/div><p>You can type the password manually and hit <strong>Enter<\/strong>. However, the Linux unzip command lets you enter the credentials directly using the <strong>-p<\/strong> option:<\/p><pre class=\"wp-block-preformatted\">unzip -p password file_archive.zip<\/pre><p>It makes the extraction process more efficient. However, we advise you to avoid this method when multiple users work on the same system since it will expose sensitive credentials in the command logs.<\/p><p>The <strong>-p<\/strong> option is useful if you use the <strong>unzip<\/strong> command in automation scripts. It lets you extract a password-protected ZIP file by passing the credentials automatically instead of entering them through the command line.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-use-unzip-in-automation-scripts\">How to Use Unzip in Automation Scripts<\/h3><p>Server administrators commonly export products from processes like backup or event logging as<strong> ZIP <\/strong>archives to save storage space. They must unpack these files to extract the data for other purposes.<\/p><p>For example, extracting a <strong>ZIP<\/strong> log lets you read the event history or pass it to another command to filter the data. Instead of running <strong>unzip <\/strong>for each archive, use an automation script to run the same command on a single execution.<\/p><p>To create one, <a href=\"\/tutorials\/how-to-install-and-use-nano-text-editor\">use a text editor like <strong>Nano<\/strong><\/a>. Enter the name with<strong> .sh<\/strong> as the file extension like the following example:<\/p><pre class=\"wp-block-preformatted\">sudo nano unzip_log.sh<\/pre><p>Enter the command inside the file. For example, the following script will unzip a new log file in a folder and automatically remove the older ones once they exceed a specific number:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\n#Log archive files location\nlog_archive=\"\/path\/to\/logs\/log_archive.zip\"\n\n#Destination for extracted files\nlog_destination=\"\/path\/to\/extracted\/logs\/\"\n\n#Maximum number of log files to keep\nmax_logs=5\n\n#Use the unzip command to extract files\nunzip \"$log_archive\" -d \"$log_destination\"\n\n#Log rotation\ncd \"$log_destination\" || exit\n\n# List and sort log files\nlog_files=($(ls -1t *.log))\n\n# Check if log rotation is needed\nif [ ${#log_files[@]} -gt $max_logs ]; then\n\n# Delete files and keep the recent ones\nexcess_logs=$(( ${#log_files[@]} - max_logs ))\nfor ((i = 0; i &lt; excess_logs; i++)); do\nrm \"${log_files[$i]}\"\ndone\nfi<\/pre><p>Press<strong> Ctrl + X<\/strong>, <strong>Y<\/strong>, and <strong>Enter<\/strong> to save the changes. Run these commands subsequently to make the script executable and run it:<\/p><pre class=\"wp-block-preformatted\">sudo chmod +x unzip_log.sh<\/pre><pre class=\"wp-block-preformatted\">.\/unzip_log.sh<\/pre><p>Currently, the command extracts the log only when you run it. To execute it regularly, set a schedule using a <a href=\"\/tutorials\/cron-job\">cronjob<\/a>. For instance, the following will apply the script daily at 2 AM:<\/p><pre class=\"wp-block-preformatted\">0 2 * * * \/path\/to\/unzip_log.sh<\/pre><h3 class=\"wp-block-heading\" id=\"h-how-to-combine-unzip-with-other-linux-commands\">How to Combine Unzip With Other Linux Commands<\/h3><p>Linux lets you combine several utilities to complete a specific task in a single execution. It makes server administration more efficient since you don&rsquo;t need to run the commands individually.<\/p><p>For example, combine the <strong>unzip<\/strong> command with<strong> find<\/strong> to locate multiple archives and extract them simultaneously. Here is an example:<\/p><pre class=\"wp-block-preformatted\">find \/path\/to\/zips -type f -name \"*.zip\" -exec unzip {} -d \/path\/to\/destination\/ \\;<\/pre><p>In the snippet, the<strong> find<\/strong> command locates <strong>ZIP<\/strong> files in the <strong>\/path\/to\/zips<\/strong> directory. Then, it passes the retrieved items into the <strong>unzip <\/strong>command, which the curly brackets represent.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-and-find-command-combinations.png\"><img decoding=\"async\" width=\"961\" height=\"84\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-and-find-command-combinations.png\/public\" alt=\"The unzip and find command combinations\" class=\"wp-image-104145\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-and-find-command-combinations.png\/w=961,fit=scale-down 961w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-and-find-command-combinations.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-and-find-command-combinations.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/unzip-and-find-command-combinations.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 961px) 100vw, 961px\" \/><\/a><\/figure><\/div><p>You can also add the <strong>grep <\/strong>command to unzip files containing a particular pattern. Consider the following example:<\/p><pre class=\"wp-block-preformatted\">unzip archive.zip $(unzip -l archive.zip | grep \"pattern\" | awk '{print $4}')<\/pre><p>In the command, we specify the items to extract from <strong>archive.zip<\/strong> within the parentheses. The <strong>-l<\/strong> option lists all the <strong>ZIP<\/strong> file&rsquo;s content, which <strong>grep <\/strong>will filter based on the given pattern.<\/p><p>Then, the pipe (<strong>|<\/strong>) passes the items containing the pattern to the <strong>awk<\/strong> command, extracting the fourth column containing the file name.<\/p><p>You can incorporate <strong>unzip <\/strong>with various other utilities specific to your tasks. Read our <a href=\"\/tutorials\/linux-commands\" target=\"_blank\" rel=\"noreferrer noopener\">Linux commands<\/a> article and check their documentation on <a href=\"https:\/\/www.gnu.org\/home.en.html\" target=\"_blank\" rel=\"noreferrer noopener\">GNU&rsquo;s website<\/a> to learn more about the potential combination.<\/p><h2 class=\"wp-block-heading\" id=\"h-comparing-the-unzip-command-with-alternative-methods\">Comparing the Unzip Command With Alternative Methods<\/h2><p>In Linux, there are several other archive and compression formats, like <strong>tar <\/strong>and <strong>tar.gz<\/strong>. The <a href=\"\/tutorials\/linux-tar-command-with-examples\"><strong>tar<\/strong> command<\/a> lets you create an archive without reducing the file size, while <strong>gzip <\/strong>enables compression.<\/p><p>Unlike the <strong>zip<\/strong> command, these two utilities don&rsquo;t support password protection by default and might be incompatible with non-UNIX operating systems like Windows.<\/p><p>That said, these compressions are more common in Linux systems since they are pre-installed, and most installation packages come in<strong> <\/strong>a<strong> tar.gz<\/strong> or <strong>tar <\/strong>file. In addition, these utilities offer more modularity and flexibility.<\/p><p>To unzip <strong>tar.gz <\/strong>or <strong>tar<\/strong> file, use the following commands:<\/p><pre class=\"wp-block-preformatted\">tar -x archive.tar<\/pre><pre class=\"wp-block-preformatted\">gunzip archive.tar.gz<\/pre><p>For desktop Linux distributions, you can also use a <strong>ZIP<\/strong> file viewer and extractor with a graphical user interface like <a href=\"https:\/\/peazip.github.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">PeaZip<\/a>. It is simpler than the command-line interface but might be inefficient since you can&rsquo;t combine utilities or use script automation.<\/p><h2 class=\"wp-block-heading\" id=\"h-best-practices-for-managing-zip-files-in-linux\">Best Practices for Managing Zip Files in Linux<\/h2><p>In this section, we will explain how to manage <strong>ZIP <\/strong>files securely and efficiently in a Linux system.<\/p><p><strong>Check Archive Files Integrity<\/strong><\/p><p>Cyber criminals often inject a <strong>ZIP <\/strong>archive with malicious code or malware. Since a <strong>ZIP<\/strong> file comprises multiple items, users usually can&rsquo;t notice oddities even after unpacking compressed files.<\/p><p>To avoid the security risk, list the contents of a <strong>ZIP <\/strong>file before unzipping using the <strong>-l <\/strong>option:<\/p><pre class=\"wp-block-preformatted\">unzip -l downloaded_file.zip<\/pre><p>In addition, ensure the file&rsquo;s checksum matches the download source&rsquo;s to ensure its integrity. To do so, run:<\/p><pre class=\"wp-block-preformatted\">md5sum downloaded_file.zip<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-zip-file-checksum.png\"><img decoding=\"async\" width=\"592\" height=\"46\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-zip-file-checksum.png\/public\" alt=\"Terminal prints a ZIP file checksum\" class=\"wp-image-104146\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-zip-file-checksum.png\/w=592,fit=scale-down 592w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-zip-file-checksum.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-zip-file-checksum.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 592px) 100vw, 592px\" \/><\/a><\/figure><\/div><p>Also, use the<strong> -t<\/strong> option to check the archive&rsquo;s structural consistency:<\/p><pre class=\"wp-block-preformatted\">unzip -t downloaded_file.zip<\/pre><p><strong>Preserve Existing Files and Permissions<\/strong><\/p><p>Extracting can overwrite existing files with the same name in the destination, resulting in a data loss. Use the <strong>-n<\/strong> option to force <strong>unzip<\/strong> to skip already present items:<\/p><pre class=\"wp-block-preformatted\">unzip -n archive.zip<\/pre><p>You don&rsquo;t need to confirm the<strong> ZIP<\/strong> file extraction if the same item already exists in the target folder since <strong>unzip<\/strong> will skip them automatically.<\/p><p>Aside from overwriting, files&rsquo; or folders&rsquo; permissions might differ from the ones in the original <strong>ZIP <\/strong>file after extraction.<\/p><p>While you can <a href=\"\/tutorials\/how-to-change-linux-permissions-and-owners\">change the file permissions manually using <strong>chmod<\/strong><\/a>, it is inefficient if you extract many files from a <strong>ZIP<\/strong> archive. To preserve these properties, use the<strong> -o<\/strong> option:<\/p><pre class=\"wp-block-preformatted\">unzip -o archive.zip<\/pre><p><strong>Leverage Kodee<\/strong><\/p><p>For VPS beginners, using the command-line interface to unzip files in Linux might be difficult due to the lack of graphical UI. Moreover, writing automation scripts or specific commands for particular tasks is complicated.<\/p><p>With <a href=\"\/vps-hosting\">Hostinger&rsquo;s VPS hosting<\/a>, you can use our AI assistant, <strong>Kodee<\/strong>, to easily generate <strong>unzip <\/strong>commands and bash scripts using simple prompts. They can then copy-paste the output to save time and effort.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\"><img decoding=\"async\" width=\"2160\" height=\"1056\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/public\" alt=\"unzip command generated by Kodee AI assistant\" class=\"wp-image-117115\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=2160,fit=scale-down 2160w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/10\/unzip-command-generated-by-Kodee.png\/w=2048,fit=scale-down 2048w\" sizes=\"(max-width: 2160px) 100vw, 2160px\" \/><\/a><\/figure><\/div><p>For example, enter &ldquo;Generate a Linux unzip command to extract files containing the pattern &lsquo;<strong>log<\/strong>&rsquo; from the<strong> archive.zip<\/strong> file into the<strong> \/home\/log_extract<\/strong>&rdquo; or &ldquo;Write a bash script that automatically unzips an archived log without overwriting existing files.&rdquo;<\/p><p>You can also use <strong>Kodee<\/strong> to view the MD5 checksum of a <strong>ZIP<\/strong> file before unzipping it, like this:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/hpanel-vps-kodee-md5-checksum.png\"><img decoding=\"async\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/hpanel-vps-kodee-md5-checksum-1024x332.png\" alt=\"Kodee responds to a question about checking a ZIP file's MD5 checksum\"><\/a><\/figure><\/div><p>To access the tool, click VPS from <strong>hPanel<\/strong>&rsquo;s top menu and select <strong>Manage<\/strong> on your plan. Navigate to the sidebar &rarr; <strong>Kodee<\/strong>.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Like <strong>tar<\/strong> and <strong>gunzip<\/strong>, the <strong>unzip <\/strong>command lets you extract an archive file in Linux. However, you must install it manually via a package manager like<strong> APT<\/strong> for<strong> Debian<\/strong> or <strong>DNF<\/strong> for RHEL derivatives such as <strong>CentOS<\/strong>.<\/p><p>To unzip files, write their names after the command. By default, it will extract the content in your current folder, but you can specify a different directory using the <strong>-d<\/strong> option. Moreover, unpack specific items by listing their names after the archive.<\/p><p>You can use the <strong>unzip <\/strong>command for a specific task by integrating it into a bash script or combining it with other utilities. Before extracting, use the <strong>-n <\/strong>option to ensure you don&rsquo;t overwrite existing files and check the contents of a<strong> ZIP<\/strong> archive using <strong>-l <\/strong>to minimize security risks.<\/p><p>With Hostinger, you can simply enter prompts, and <strong>Kodee<\/strong>, our AI assistant, will generate <strong>unzip <\/strong>commands or scripts for your specific tasks. Just enter the prompts and copy-paste the output into your Terminal.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Learn More Linux Commands for File Management<\/h4>\n                    <p><a href=\"\/tutorials\/how-to-remove-files-and-folders-using-linux-command-line\/\">How to Remove Files and Directories<\/a><br>\n<a href=\"\/tutorials\/linux-touch-command-with-useful-examples\/\">How to Create an Empty File<\/a><br>\n<a href=\"\/tutorials\/how-to-use-find-and-locate-commands-in-linux\/\">How to Locate a File<\/a><br>\n<a href=\"\/tutorials\/linux-tar-command-with-examples\/\">How to Compress a File with Tar Command<\/a><br>\n<a href=\"\/tutorials\/linux-chown-command\/\">How to Change File Ownership with Chown Command<\/a><br>\n<a href=\"\/tutorials\/vps\/change-linux-permissions-and-owners\">How to Change FIle Permissions with Chmod Command<\/a><br>\n<a href=\"\/tutorials\/how-to-rename-files-in-linux\/\">How to Rename a File<\/a><br>\n<a href=\"\/tutorials\/linux-file-command\/\">How to Check File Type<\/a><br>\n<a href=\"\/tutorials\/how-to-create-symbolic-links-in-linux\/\">How to Create a Symbolic Link (Symlink)<\/a><\/p>\n                <\/div>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To unzip, Linux users can apply a command utility similar to other compression formats, such as tar and gzip. Understanding [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/how-to-unzip-files-linux\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Unzip a File in Linux With Useful Command Examples","rank_math_description":"Unzip is a utility that is used to extract compressed files. Check this article to learn how to install and unzip files in Linux efficiently.","rank_math_focus_keyword":"unzip linux","footnotes":""},"categories":[22648,22644],"tags":[],"class_list":["post-15819","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-unzip-files-linux","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/unzip-linux","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/comando-unzip-linux","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/unzip-linux","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-unzip-files-linux","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-unzip-files-linux\/","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-unzip-files-linux\/","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/comando-unzip-linux","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/comando-unzip-linux","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/comando-unzip-linux","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-unzip-files-linux","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-unzip-files-linux","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-unzip-files-linux","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-unzip-files-linux","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=15819"}],"version-history":[{"count":26,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15819\/revisions"}],"predecessor-version":[{"id":124603,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15819\/revisions\/124603"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=15819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=15819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=15819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}