How to Zip and Unzip Files in the Linux Terminal

Aidin

Folder with a zipper illustration for a guide to zip and unzip in Linux

Hi everyone. tar.gz is the natural archive format on Linux, but the moment you send files to someone on Windows or macOS, zip is the format that opens everywhere with a double click. In this guide I use zip and unzip in the Ubuntu 26.04 terminal to pack a folder for a teammate, check what is inside, extract it, deal with the replace question unzip asks when files already exist, and add a file to an existing archive.

Unlike some older guides that start by installing the tools, on this Ubuntu 26.04 desktop both zip and UnZip 6.00 were already present, so you can most likely start right away. If you get command not found, the fix is at the end of this guide.

If you have not read it yet, the previous article on the tar command covers the Linux native format and when to prefer it.

Distro : Ubuntu 26.04.1 LTS (Desktop)
Folder : ~/zip-lab/share
Archive: share.zip

Creating a Zip File

Zip a folder with -r

Creating share.zip with zip -r and the text file deflated by 59 percent

I prepared a share folder with a 20,000 line text file and the backup script from the chmod lab, then zipped it.

zip -r share.zip share
ls -lh share.zip share/numbers.txt
unzip -v | head -n 1

zip takes the archive name first and then what to add, and -r makes it include everything inside the folder. For each file it prints how it was stored. The text file was deflated by 59 percent (number 1), while the tiny script was simply stored. ls confirms the result, a 107K text file packed into a 45K zip (number 2). The last line shows the UnZip version, 6.00, which is what this desktop already had.

Note: Without -r, zip adds only the folder entry itself and leaves out its contents. It is the most common reason for a zip file that turns out to be almost empty.

Checking What Is Inside

List the contents with unzip -l

Listing the contents of share.zip with unzip -l
unzip -l share.zip

-l lists every file with its original size, date, and path, without extracting anything (number 1). Everything sits under share/, so extracting it will create one folder instead of scattering files around.

Extracting a Zip File

Extract into a separate folder with -d

Extracting into the extracted folder with backup.sh keeping its execute permission
unzip share.zip -d extracted
ls -l extracted/share

-d chooses the destination folder and creates it if needed. unzip reports each step, creating the folder, extracting the small file, and inflating the compressed one (number 1). Notice that backup.sh is still -rwxr-xr-x after extraction (number 2). Zip archives made on Linux keep the execute permission, so scripts still run after unzipping on another Linux machine.

My experience: This is one of the differences people notice only when it bites them. A zip created on Windows has no Linux permissions at all, so scripts unpacked from it need chmod +x again. Zipping on Linux avoided that here.

When Files Already Exist

The replace question

unzip asking to replace when files already exist in the destination

I ran the same extraction a second time into the same folder.

unzip share.zip -d extracted

unzip stopped and asked replace extracted/share/backup.sh? with the answers yes, no, All, None, and rename (number 1). I typed N for None, which keeps every existing file and extracts nothing new. A lower-case n would only skip this one file and then ask again for the next.

Overwrite without asking using -o

unzip -o overwriting existing files without asking
unzip -o share.zip -d extracted

When you know you want the archive version, -o overwrites existing files without asking (number 1), and unzip runs straight through (number 2). The opposite option is -n, which never overwrites. Both are useful in scripts, where nobody is there to answer the question.

Adding a File to an Existing Zip

Run zip again on the same archive

Adding readme.txt to an existing zip archive
echo Shared files for the team > share/readme.txt
zip -r share.zip share
unzip -l share.zip

Running zip on an archive that already exists updates it. Files that were already inside are marked as updating, and the new readme.txt is added (number 1). The listing now shows four entries, with readme.txt at the end (number 2).

Common Problems and How to Fix Them

zip: command not found

The tool is not installed on that system. Install both with sudo apt install zip unzip. On this Ubuntu 26.04 desktop they were already there, but minimal and server installs often leave them out.

The zip file is almost empty

You forgot -r when zipping a folder. Run zip -r again so the folder contents are included.

unzip keeps asking replace

Files with the same names already exist in the destination. Answer A to replace all or N to keep all, or use -o and -n to decide in advance.

A script from a zip will not run

The archive was probably created on Windows, so it has no execute permission. Run chmod +x on the script after extracting.

That covers zip and unzip for sharing files: create with -r, check with -l, extract with -d, decide what happens to existing files, and update an archive in place. Next in the series is the apt command for installing and removing software on Ubuntu. If zip or unzip shows a message I did not cover, paste it in the comments.

Leave a Comment