Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
LPI Linux Essentials Exam 010-160 - Topic 3.1 - zip Archives
zip Overview
The zip
command is a data compression and archiving utility available on most Unix and Linux-based operating systems. It's commonly used to bundle and compress multiple files and directories into a single .zip
archive file. Unlike tar
, the zip
command can perform both archiving and compression in one step without relying on other commands. Also, zip
files are widely recognized and can be easily opened on multiple platforms, including Windows.
Create Practice Directory & Files
Before diving into the zip
command, let's set up some sample files and directories:
Exercise: Create the required directories & files for practicing the zip
command:
mkdir -p ~/zip-practice
cd ~/zip-practice
echo "This is file 1." > file1.txt
echo "This is file 2." > file2.txt
echo "This is file 3." > file3.txt
mkdir -p dir1
echo "This is file 4." > dir1/file4.txt
mkdir -p dir2
echo "This is file 5." > dir2/file5.txt
Confirm that the required files are created:
ls -lRh
The output should resemble the following:
total 20K
drwxrwxr-x 2 dtvlinux dtvlinux 4.0K Oct 26 08:20 dir1
drwxrwxr-x 2 dtvlinux dtvlinux 4.0K Oct 26 08:20 dir2
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file1.txt
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file2.txt
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file3.txt
./dir1:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file4.txt
./dir2:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file5.txt
Creating a zip Archive
To create a zip archive, use the zip
command followed by the archive name and the files or directories you want to include.
Exercise: Create a zip archive named archive.zip
containing file1.txt
, file2.txt
, dir1
, and dir2
:
zip -r archive.zip file1.txt file2.txt dir1 dir2
The command output should be:
adding: file1.txt (stored 0%)
adding: file2.txt (stored 0%)
adding: dir1/ (stored 0%)
adding: dir1/file4.txt (stored 0%)
adding: dir2/ (stored 0%)
adding: dir2/file5.txt (stored 0%)
Listing the Contents of a zip Archive
You can list the contents of a zip archive using the -l
option with the unzip
command:
Exercise: Display the list of files and directories in the archive.zip
archive:
unzip -l archive.zip
The command output should resemble:
Archive: archive.zip
Length Date Time Name
--------- ---------- ----- ----
16 2023-10-26 08:20 file1.txt
16 2023-10-26 08:20 file2.txt
0 2023-10-26 08:20 dir1/
16 2023-10-26 08:20 dir1/file4.txt
0 2023-10-26 08:20 dir2/
16 2023-10-26 08:20 dir2/file5.txt
--------- -------
64 6 files
Extracting Files from a zip Archive
To extract files from a zip archive, you can use the unzip
command:
Exercise: Extract all files and directories from the archive.zip
archive:
First remove the existing files and directories:
rm -rvf file1.txt file2.txt dir1 dir2
The output will look like:
removed 'file1.txt'
removed 'file2.txt'
removed 'dir1/file4.txt'
removed directory 'dir1'
removed 'dir2/file5.txt'
removed directory 'dir2'
Extract all files from the zip
archive to restore the deleted files and directories:
unzip archive.zip
The output will print the extracted files and directories:
Archive: archive.zip
extracting: file1.txt
extracting: file2.txt
creating: dir1/
extracting: dir1/file4.txt
creating: dir2/
extracting: dir2/file5.txt
Extracting Files from a zip Archive to a Specified Directory
You can specify the directory where you want to extract the files:
Exercise: Extract the files and directories from archive.zip
into the /tmp
directory:
Extract files and directories into the /tmp
directory:
unzip archive.zip -d /tmp
Confirm files extracted to /tmp
:
ls -lh /tmp/{dir{1,2},file{1,2}.txt}
Output should resemble the following output which shows file1.txt
, file2.txt
, dir1/file4.txt
& dir2/file5.txt
existing:
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 /tmp/file1.txt
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 /tmp/file2.txt
/tmp/dir1:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file4.txt
/tmp/dir2:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 26 08:20 file5.txt
Adding Files to an Existing zip Archive
If you want to add more files to an existing zip archive, you can use the -u
option:
Exercise: Add file3.txt
to the existing archive.zip
archive:
Append file3.txt
to archive:
zip -u archive.zip file3.txt
The output confirms the file was added:
adding: file3.txt (stored 0%)
Updating Files in a Zip Archive
To update or add files to an existing .zip
archive, you can use the zip
command with the -u
option. This will update the archive with new or changed files only.
Exercise: Update file1.txt
in the archive.tar
archive with the latest version:
Update the contents of file1.txt
:
echo "I am an updated file1.txt" > file1.txt
Update the archive with the updated file:
zip -u archive.zip file1.txt
The output shows the archive being updated:
updating: file1.txt (stored 0%)
Removing Files from an Existing zip Archive
To remove a file from an existing zip archive, you can use the -d
option:
Exercise: Remove file3.txt
from archive.zip
.
zip -d archive.zip file3.txt
The output shows file3.txt
being removed:
deleting: file3.txt
Compression Levels
The zip
command allows you to specify different levels of compression ranging from 0 (store only) to 9 (maximum compression). Use the -<level>
option to specify this:
Exercise: Create an archive with maximum compression:
Create a compressed archive of all commands starting with k
from /usr/bin
directory:
zip -9 archive_max_compressed.zip /usr/bin/k*
The output will show the percent compression of each file, for example:
adding: usr/bin/kbdinfo (deflated 74%)
adding: usr/bin/kbd_mode (deflated 69%)
adding: usr/bin/kbxutil (deflated 60%)
adding: usr/bin/keep-one-running (deflated 55%)
adding: usr/bin/kernel-install (deflated 66%)
adding: usr/bin/kerneloops-submit (deflated 75%)
adding: usr/bin/keyring (deflated 51%)
adding: usr/bin/keytool (deflated 83%)
adding: usr/bin/kill (deflated 68%)
adding: usr/bin/killall (deflated 67%)
adding: usr/bin/kmod (deflated 55%)
adding: usr/bin/kmodsign (deflated 73%)
Password-Protecting a zip Archive
You can add password protection to a zip archive using the -e
option:
Exercise: Create a password-protected archive:
zip -e secure_archive.zip file1.txt file2.txt
You will be prompted to enter and confirm a password before the files are added to the zip
:
Enter password:
Verify password:
adding: file1.txt (stored 0%)
adding: file2.txt (stored 0%)
Now attempt to unzip
the archive:
unzip secure_archive.zip
It will prompt for a password before unzipping as well as prompting for if you would like to replace the existing files. Enter the password you set and choose A for to replace all files:
[secure_archive.zip] file1.txt password:
replace file1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
extracting: file1.txt
extracting: file2.txt
Conclusion
The zip
command provides a comprehensive set of options for compressing, archiving, and managing .zip
files. This guide should give you a good starting point for understanding how to use the zip
command effectively. With its capabilities, you have a versatile tool at your disposal for handling a wide variety of archiving and compression tasks.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.