Working with compressed archives is a daily task for most Linux administrators and developers. The ZIP format remains one of the most universally supported ways to package and compress files. In Linux, creating and extracting ZIP files from the command line is straightforward using the zip and unzip utilities.
Note: If you receive a “command not found” error, you need to install the tools first. On Ubuntu/Debian, run sudo apt install zip unzip.
How to Create a ZIP File (Compress)
Zipping a Single File
To compress a single file, use the zip command followed by the name you want for the new archive, and then the name of the file you are compressing.
zip archive_name.zip filename.txt
Zipping a Directory (Recursively)
Most of the time, you will want to compress an entire folder. To do this, you must use the -r (recursive) flag, which tells the command to include all files and subdirectories inside the target folder.
zip -r project_backup.zip /path/to/project_folder
Excluding Specific Files
If you are zipping a directory but want to leave certain files out (like hidden system files or large logs), use the -x flag.
zip -r project.zip folder_name -x "*.DS_Store" "*.log"
How to Extract a ZIP File (Unzip)
Extracting to the Current Directory
To extract the contents of a ZIP file directly into the folder you are currently working in, simply use the unzip command.
unzip archive_name.zip
Extracting to a Specific Destination
It is often cleaner to extract the archive into a different directory rather than cluttering your current location. Use the -d (directory) flag, followed by the path where you want the files to go. If the destination directory does not exist, the command will create it for you.
unzip archive_name.zip -d /path/to/destination_folder
Viewing Contents Without Extracting
If you want to see what is inside a ZIP file before you extract it, you can list its contents using the -l (list) flag.
unzip -l archive_name.zip