How to Use the Zip Command in Linux to Compress Files and Folders

Zipper Image Feature Image For How To Use Linux Zip Command

The zip command in Linux is probably the simplest and most intuitive way to work with ZIP files on the command line. And not just that, the ZIP file format is universal – almost every OS out there has a native program that can work with it. Here’s all the ways you can use this command in any Linux command line.

How to Install Zip in Linux

The latest version of the Linux zip command should be available in all major package managers.

For Debian/Ubuntu:

sudo apt install zip

For Arch Linux:

sudo pacman -S zip

For Fedora:

sudo dnf install zip

For Gentoo

sudo emerge --ask zip

Note: Before installing anything, make sure to update your package manager first to get the zip command’s latest version. You can learn how to do that and more with our Linux package manager cheatsheet.

Note: With zip installed, the unzip command will also be available now.

Zip Command in Linux: Syntax and Options

There’s a basic syntax to the zip command, and it takes just two parameters: the output ZIP file’s filename and the input’s filename.

zip [options] <zip-file.zip> <files-to-archive>
Terminal Linux Zip Command Example

Their positions are important here. If you do them the other way around, the computer will wonder why you’re trying to make a “file-to-archive.jpg” with it from a “zip-file.zip” that doesn’t exist and return an error.

As for setting the options, you’ll need to work with flags. There are 8 flags you would regularly need for normal use:

  • -d delete selected archived entries in the ZIP file.
  • -q quietly do task without printing anything on the terminal.
  • -T test the ZIP file’s integrity.
  • -e encrypt file.
  • -u update ZIP file by adding new files into the archive.
  • -m move file into archive.
  • -r recursively add files within a folder into a ZIP file.
  • -x exclude files with the selected filenames.
  • -i include only files with the selected names.

You can place the flags anywhere you like whether they’re before the output, after the input, between the two – you can even use multiple flags scattered all around the line in any order. Just make sure that you type them after typing zip.

The exception are -x and -i which need a new input after using them.

How to Zip a File in Linux Using the Zip Command

The easiest way to archive files with the zip command is to type zip followed by the filename of the ZIP file and then the file you are trying to archive or compress. Flags are optional, so you can do it like that to quickly zip any file on Linux.

zip archive.zip text_file.txt
Terminal Basic Zip Folder Command In Linux

You can also add more files by adding the others after the first file you were trying to archive. And if any of these have spaces in their names, you can enclose them in quotation marks.

zip archive.zip "file 1.txt" "file 2.txt" "file 3.txt" "file 4.txt"
Terminal Archiving Multiple Files In One Linux Zip Command Example

Using the -d Flag

The -d flag lets you delete a file archived within the ZIP file in the same way you would have added it into one.

zip -d archive.zip remove_me.jpg
Linux Terminal Using The D Flag 1

You can also use it to remove entire folders within the archive. However, this won’t work for files outside of the ZIP file (so you can’t use it to delete files after archiving).

Using the -q Flag

The -q flag is for making sure that zip won’t return a message when it does its thing.

zip -q archive.zip text_file.txt
Linux Terminal Using The Q Flag

It’s useful when you’re using the zip command with a script. But if you’re using this by yourself, you might want to avoid using the -q flag since you won’t know if the the archiving process was successful without checking with the dir or ls command.

Using the -T Flag

Some files Some ZIP files can get corrupted during download – it happens. You can use the -T flag to check the integrity so you would know if it really is a broken file or you’re just inputting the wrong password.

zip -T archive.zip
Linux Terminal Using The Big T Flag

This is pretty important when downloading large files like OS images. For files like these, you can successfully burn them into a live USB and end up with errors once you run the image file on your machine. The same goes with videos and music that get cut-up parts in random places.

Using the -u Flag

If you wanted to add a new file into an existing ZIP file, you can use the -u flag.

zip -u archive.zip new_file.txt
Linux Terminal Using The U Flag

You can also add more files by adding them after the first file, as if you were using the zip command normally to make a new ZIP archive.

Using the -e Flag

If you want to add a password, you need to use the -e flag. This option only applies the password and encryption into the file you add into the ZIP file within its command, so previous files will remain unencrypted if you use this with the -u flag.

zip -e archive.zip text_file.txt
Linux Terminal Using The E Flag With Terminal Asking For Password

The great thing about ZIP files is that the password-making process also adds an extra layer of protection besides giving access. Your password works like a digital signature which is used to decrypt an extra encryption layer added into the ZIP file.

Using the -m Flag

Lastly, the -m flag deletes the original file after it gets archived into the ZIP file. Think of this as automating an extra step so you won’t have to remove it by yourself.

zip -m archive.zip text_file.txt
Linux Terminal Using The M Flag With Before And After Dir To Show File Disappeared

One important bit to remember though: the -m flag deletes files in such a way that makes it impossible to recover them without using a file restoration tool. So be sure to keep a backup, or just remove things manually using the rm command instead.

How to Zip a Folder in Linux Using the Zip Command

If you use the zip command on a raw folder, you’ll be left with a folder that does not contain any files at all. Whenever you are trying to zip a folder, you need to use the -r flag.

Using the -r Flag

The -r flag, aka recursive, goes through each content of a folder and its subfolders and archives every single one into the ZIP file. It will also retain the current structure of each subfolder.

zip -r archive.zip my_folder
Linux Terminal Using The R Flag

This also works if you want to archive everything within the current folder. Just replace my_folder with ./ and it will change its target to everything within the current one.

zip -r archive.zip ./
Using The Linux Unzip Command With R Flag On All Files Within Current Folder

Furthermore, you can use this alongside the -u flag to include folders and their contents into an existing ZIP file.

zip -r -u archive.zip another_folder
Linux Terminal Using The R Flag With The U Flag

Using the -i Flag

The -i flag lets you pick out a specific file that you want to include and excludes everything else within a folder

zip -r archive.zip my_folder -i my_folder/a_subfolder/file1.txt
Linux Terminal Using The I Flag

By using * asterisks with the -i flag, you can select files that share similar names but could end with something like a number. For example, typing *.txt will make the zip command include all TXT files within the folder.

zip -r archive.zip my_folder -i "*.txt"
Linux Terminal Using The I Flag With Asterisk

Using the -x Flag

The -x flag lets you exclude only certain files while zipping a whole folder.

zip -r archive.zip my_folder -x my_folder/dont_zip.jpg
Linux Terminal Using The X Flag

Also, the -x flag overrides the -i flag when used together. This way, you can, for example, tell zip to include all the other files of the same type except for the file you don’t want it to zip.

zip -r archive.zip my_folder -i "*.txt" -x my_folder/dont_zip.txt
Linux Terminal Using The X Flag And I Flag Together

How to Unzip a File Using the Linux Zip Command

The unzip program is a partner program to the default zip program. It extracts files from your ZIP archive.

To use this program, you just need to type unzip and the filename of the ZIP archive.

unzip archive.zip
Basic Linux Unzip Command

By using the unzip command, you will unzip the ZIP file within the current working directory regardless of where the ZIP file came from.

To extract or unzip a Linux file within a ZIP file so the others remain unextracted, you can add its filename after the part where you type the archive.

unzip archive.zip file.txt
Selective Unzip File Txt

How to Unzip a Linux Folder With Zip

While unzip will normally extract folders located within a ZIP archive, it can be pretty hard to make it extract only certain folders. But that does not mean it’s impossible.

unzip archive.zip my_folder/a_subfolder/*
Selective Unzip Contents Of A Single Folder

That line of code extracts everything within a_subfolder.

Also, when you unzip a folder, the unzip program creates a new folder within the current working directory and unpacks its folders there. If a folder exists with the same name (or a previous version of the folder is present), it ask if it’s okay to replace the file that’s currently in the folder.

Frequently Asked Questions

What does the gzip command in Linux do?

The Linux gzip command is a tool that you can use to store and compress files into a GZ file.

Is ZIP better than TAR?

In a way, ZIP is better than TAR because ZIP files can compress, encrypt, and protect their contents. However, TAR files are meant to combine files into one file for faster file transfer while ZIP files (despite being used for the same purpose) come with those extra features in mind.

Is TAR faster than ZIP?

TAR files do not compress nor encrypt their contents. So if you are looking for a faster way to combine a large amount of files into a single easy-to-transfer archive, you’ll see some gains with TAR compared to ZIP. However, you might find it easier to transfer a ZIP file instead of a TAR file because ZIP files tend to be much smaller than TAR files.

Image credit: Unsplash, Screenshots by Terenz Jomar Dela Cruz

Terenz Jomar Dela Cruz
Terenz Jomar Dela Cruz

Terenz is a hobbyist roboticist trying to build the most awesome robot the world has ever seen. He could have done that already if he wasn't so busy burning through LEDs as a second hobby.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox