Linux unzip Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux unzip command to extract files from compressed ZIP archives. The lab covers the purpose and syntax of the unzip command, how to extract files from a ZIP archive, and how to unzip a password-protected ZIP file. The steps provided in the lab will guide you through these practical examples, helping you develop your compression and archiving skills on the Linux operating system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/CompressionandArchivingGroup -.-> linux/zip("`Compressing`") linux/CompressionandArchivingGroup -.-> linux/unzip("`Decompressing`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") subgraph Lab Skills linux/tar -.-> lab-422981{{"`Linux unzip Command with Practical Examples`"}} linux/zip -.-> lab-422981{{"`Linux unzip Command with Practical Examples`"}} linux/unzip -.-> lab-422981{{"`Linux unzip Command with Practical Examples`"}} linux/cd -.-> lab-422981{{"`Linux unzip Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the unzip Command

In this step, you will learn about the purpose and basic syntax of the unzip command in Linux. The unzip command is used to extract files from compressed ZIP archives.

To understand the purpose and syntax of the unzip command, run the following command:

unzip -h

Example output:

UnZip 6.0 (Unix), by Info-ZIP. Released under the GNU General Public License.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir.
  -Z                      show zipfile comment
  -f  -u  -o  -n          various extract options (see below)
  -x  -X                  exclude files that follow (in xlist)
  -d  exdir               extract files into exdir

The unzip command has several options that allow you to customize the extraction process. Here are some of the most commonly used options:

  • -Z: Show the ZIP file comment.
  • -f: Update existing files, don't replace newer files.
  • -u: Update files, create new files if necessary.
  • -o: Overwrite existing files without prompting.
  • -n: Never overwrite existing files.
  • -x: Exclude the specified files from extraction.
  • -d: Extract files to the specified directory.

To extract all files from a ZIP archive, you can use the following command:

unzip archive.zip

This will extract all files from the archive.zip file to the current directory.

Extract Files from a Compressed ZIP Archive

In this step, you will learn how to extract files from a compressed ZIP archive using the unzip command.

First, let's create a sample ZIP archive to work with. Run the following commands to create a ZIP file named example.zip containing a few text files:

cd ~/project
mkdir example
cd example
echo "This is file1.txt" > file1.txt
echo "This is file2.txt" > file2.txt
echo "This is file3.txt" > file3.txt
zip -r example.zip .

Now, to extract the files from the example.zip archive, use the following command:

unzip example.zip

Example output:

Archive:  example.zip
 extracting: file1.txt
 extracting: file2.txt
 extracting: file3.txt

This will extract all the files from the example.zip archive to the current directory.

If you want to extract the files to a different directory, you can use the -d option followed by the target directory:

unzip example.zip -d ~/project/extracted

This will extract the files from example.zip to the ~/project/extracted directory.

Unzip a Password-Protected ZIP File

In this step, you will learn how to extract files from a password-protected ZIP archive using the unzip command.

First, let's create a password-protected ZIP archive to work with. Run the following commands to create a ZIP file named protected.zip containing a text file:

cd ~/project
mkdir protected
cd protected
echo "This is a protected file." > protected_file.txt
zip -e protected.zip protected_file.txt

When prompted, enter a password for the ZIP file. For this example, let's use the password "mypassword".

Now, to extract the files from the protected.zip archive, use the following command:

unzip protected.zip

Example output:

Archive:  protected.zip
[protected.zip] protected_file.txt password:

The unzip command will prompt you to enter the password for the ZIP file. Enter the password you used when creating the archive (in this case, "mypassword").

If the password is correct, the file will be extracted:

inflating: protected_file.txt

If the password is incorrect, the unzip command will fail to extract the file.

Summary

In this lab, you first learned about the purpose and basic syntax of the unzip command in Linux, which is used to extract files from compressed ZIP archives. You explored the various options available with the unzip command, such as updating existing files, overwriting files, excluding specific files, and extracting files to a specified directory. You then learned how to extract files from a compressed ZIP archive using the unzip command, creating a sample ZIP file and extracting its contents. Finally, you discovered how to unzip a password-protected ZIP file, providing the correct password to access the archived files.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like