Working entirely within a command-line interface can feel restrictive if you are used to visual file managers. When you need to send a massive folder containing hundreds of documents to a colleague, you cannot simply right-click and select “Compress”. Learning how to extract ZIP files in Linux, as well as how to create them from scratch, is a fundamental skill for anyone managing a server or working in a terminal environment.
In this straightforward guide, we will teach you the essential syntax for the zip and unzip commands, allowing you to efficiently compress and decompress your data.
How to Install the ZIP Utilities
Most modern Linux distributions, including Ubuntu and Debian, come with these tools pre-installed. However, if you are working on a minimal server installation, you might need to install them manually.
Open your terminal and run the following command to ensure both tools are available:
sudo apt install zip unzip
How to Create a ZIP File
Creating an archive allows you to bundle multiple files together and reduce their overall file size, saving both storage space and bandwidth.
Compressing a Single File
To compress one specific file, type zip, followed by the name you want to give the new archive, and then the name of the file you want to compress.
zip backup.zip my_document.txt
Compressing an Entire Directory (Folder)
This is the most common use case. To compress a folder and all the files inside it, you must use the -r (recursive) flag. This tells the command to go deep into the folder structure.
zip -r website_backup.zip /var/www/html/
The terminal will print out a list of every file as it is added to the new website_backup.zip archive.
How to Extract a ZIP File
When you download a compressed archive from the internet, you will need to unpack it before you can read the files inside.
Extracting to the Current Directory
To extract all the contents of a ZIP file directly into the folder you are currently working in, simply use the unzip command followed by the file name:
unzip project_files.zip
Extracting to a Specific Directory
If you want to keep your workspace clean and extract the files into a different location, use the -d (destination) flag.
unzip project_files.zip -d /home/user/documents/
If the destination folder does not exist, the command will automatically create it for you.
Frequently Asked Questions
Can I see what is inside a ZIP file without extracting it?
Yes. If you want to peek inside an archive before committing to extracting it, use the -l (list) flag. For example: unzip -l mysterious_file.zip. This will print a directory tree of the contents without modifying your hard drive.
Does the ZIP command delete the original files?
No. When you compress a folder, the original files remain completely untouched. You will end up with both the original uncompressed folder and the new compressed ZIP file.
By mastering these basic commands, you can easily manage, share, and backup your Linux files without ever needing a graphical interface.