Linux zip Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux zip command to compress and archive files and directories. You will explore the basic usage of the zip command, including creating and extracting zip archives, as well as compressing and encrypting files. The lab covers practical examples and demonstrates the versatility of the zip command in managing compressed data on Linux systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/man("`Manual Access`") linux/CompressionandArchivingGroup -.-> linux/zip("`Compressing`") linux/CompressionandArchivingGroup -.-> linux/unzip("`Decompressing`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/man -.-> lab-423021{{"`Linux zip Command with Practical Examples`"}} linux/zip -.-> lab-423021{{"`Linux zip Command with Practical Examples`"}} linux/unzip -.-> lab-423021{{"`Linux zip Command with Practical Examples`"}} linux/cd -.-> lab-423021{{"`Linux zip Command with Practical Examples`"}} linux/touch -.-> lab-423021{{"`Linux zip Command with Practical Examples`"}} end

Understand the zip Command

In this step, you will learn about the basic usage of the zip command in Linux. The zip command is a popular tool for compressing and archiving files and directories.

First, let's check the version of the zip command installed on your system:

zip --version

Example output:

Zip 3.0 (July 5th 2008)
 Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.

The zip command has a wide range of options and features. Let's explore some of the most common ones:

  1. Creating a zip archive:

    zip archive.zip file1.txt file2.txt directory/

    This command will create a zip archive named archive.zip containing file1.txt, file2.txt, and all files and subdirectories in the directory/ folder.

  2. Listing the contents of a zip archive:

    unzip -l archive.zip

    This will list the contents of the archive.zip file without extracting it.

  3. Extracting a zip archive:

    unzip archive.zip

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

  4. Extracting a zip archive to a specific directory:

    unzip archive.zip -d /path/to/extract/directory

    This will extract the contents of archive.zip to the specified directory.

  5. Compressing a directory with the zip command:

    zip -r archive.zip directory/

    This will create a zip archive named archive.zip that contains all the files and subdirectories in the directory/ folder.

  6. Encrypting a zip archive:

    zip -e archive.zip file1.txt file2.txt

    This will create an encrypted zip archive named archive.zip that contains file1.txt and file2.txt. You will be prompted to enter a password for the encryption.

Remember, the zip command provides many more options and features. You can explore them by running man zip in your terminal.

Create and Extract zip Archives

In this step, you will learn how to create and extract zip archives using the zip and unzip commands.

First, let's create a zip archive:

cd ~/project
touch file1.txt file2.txt
zip my_archive.zip file1.txt file2.txt

Example output:

  adding: file1.txt (stored 0%)
  adding: file2.txt (stored 0%)

The zip my_archive.zip file1.txt file2.txt command creates a zip archive named my_archive.zip that contains file1.txt and file2.txt.

Now, let's list the contents of the zip archive:

unzip -l my_archive.zip

Example output:

Archive:  my_archive.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- -----   ------   ----
       0  Stored        0   0% 03-23-2023 11:11  00000000  file1.txt
       0  Stored        0   0% 03-23-2023 11:11  00000000  file2.txt
--------          -------  ---                            -------
       0                0   0%                            2 files

To extract the zip archive, run the following command:

unzip my_archive.zip

Example output:

Archive:  my_archive.zip
 extracting: file1.txt
 extracting: file2.txt

This will extract the contents of the my_archive.zip file to the current directory.

You can also extract the zip archive to a specific directory:

mkdir extracted_files
unzip my_archive.zip -d extracted_files

This will extract the contents of the my_archive.zip file to the extracted_files directory.

Compress and Encrypt Files with zip

In this step, you will learn how to compress and encrypt files using the zip command.

First, let's create some sample files to work with:

cd ~/project
touch secret_file.txt important_document.pdf

Now, let's compress and encrypt these files using the zip command:

zip -e encrypted_archive.zip secret_file.txt important_document.pdf

You will be prompted to enter a password for the encryption:

Enter password:
Verify password:

After entering the password, the encrypted_archive.zip file will be created, containing the encrypted secret_file.txt and important_document.pdf files.

To extract the encrypted zip archive, you will need to use the unzip command and provide the password:

unzip encrypted_archive.zip

You will be prompted to enter the password:

Archive:  encrypted_archive.zip
[encrypted_archive.zip] secret_file.txt password:

After entering the correct password, the files will be extracted from the encrypted zip archive.

Summary

In this lab, you learned about the basic usage of the zip command in Linux, including creating and extracting zip archives, compressing directories, and encrypting files. You explored various zip command options such as creating a zip archive, listing the contents of an archive, extracting files to a specific directory, and compressing a directory. Additionally, you learned how to encrypt a zip archive using the -e option and set a password for the encryption.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like