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.
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:
Creating a zip archive:
zip archive.zip file1.txt file2.txt directory/This command will create a zip archive named
archive.zipcontainingfile1.txt,file2.txt, and all files and subdirectories in thedirectory/folder.Listing the contents of a zip archive:
unzip -l archive.zipThis will list the contents of the
archive.zipfile without extracting it.Extracting a zip archive:
unzip archive.zipThis will extract all the files and directories from the
archive.zipfile to the current directory.Extracting a zip archive to a specific directory:
unzip archive.zip -d /path/to/extract/directoryThis will extract the contents of
archive.zipto the specified directory.Compressing a directory with the
zipcommand:zip -r archive.zip directory/This will create a zip archive named
archive.zipthat contains all the files and subdirectories in thedirectory/folder.Encrypting a zip archive:
zip -e archive.zip file1.txt file2.txtThis will create an encrypted zip archive named
archive.zipthat containsfile1.txtandfile2.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.



