Home » Linux » Zip Command In Linux

The Zip Command in Linux: How to Zip Files and Directories

If you’re looking to archive your files and directories, regardless of your OS, “zip” is the format that is most popular and supported by the largest number of clients and operating systems. In this tutorial, we’ll go over how to use the applications in Linux to compress files and directories.

The zip file format supports lossless data compression. Its origins go back to 1993, designed by Phil Katz of PKWARE and Gary Conway of Infinity Design Concepts.

Compressing your data has multiple benefits, namely: less storage is used on your hard drive, data is quicker and more efficient to transfer and multiple files are combined into one file, making it more suitable for email and other applications.

The “zip” application on Linux allows you to zip files, and the “unzip” application allows you to extract compressed files.

Installing Zip and Unzip

To get started with zip on Linux, you’ll need to install it, along with unzip (if they haven’t been installed already).

Install Zip/Unzip in Debian/Ubuntu/Mint

$ sudo apt install zip
$ sudo apt install unzip

Install Zip/Unzip in Redhat, Centos or Fedora

$ yum install zip
$ yum install unzip

Installing in other versions of Linux

If you’re using a different version of Linux, simply use the typical command you’d use to install an application for the apps “zip” and “unzip”.

Using the zip Command

If you are looking to compress files or directories, you’ll need to use the zip command.

To find out which version of zip you have installed, you can type:

$ zip -v

To use the zip command to compress files or directories, the syntax is as follows:

$ zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

Zipping Files

To zip a single file, type:

$ zip archive.zip file

If you’d like to zip multiple files, you can list multiple names in the command, like so:

$ zip archive.zip file1 file2 file3 file4 file5

Either of the commands above would create an archive named “archive.zip”.

Zipping Directories

If you’d like to zip all the files within a directory, you can use the -r switch with zip, which will include all files within the directory recursively. An example of the command to zip a directory is as follows:

$ zip -r archive.zip directory

You can combine multiple directories like so:

$ zip -r archive.zip directory1 directory2 directory3

You can combine directories and files like so:

$ zip -r archive.zip directory1 directory2 directory3 file1 file2 file3

Other Useful zip Commands

The zip command has a number of other switches available which can be very useful, depending on your use case. We’ll go over some examples below.

Password Protecting Your Zip Archive

You can password-protect your resulting zip arhive using the -e switch, like this:

$ zip -e archive.zip file1 file2

You’ll then be asked to insert your password and confirm it before your archive is created.

Quiet Output

If you don’t want zip to print to the console, you can use the -q quiet switch. An example is as follows:

$ zip -q archive.zip file1 file2

Splitting Your Archive Across Multiple Files

If you need to split your archive up across multiple files to, for example, store the archive across multiple CDs, you can use the -s split switch.

This switch supports a multiplier which can be in the following formats:

  • k, for kilobytes
  • m, for megabytes
  • g, for gigabytes
  • t, for terabytes

An example command to create an archive and split it across multiple 700MB files is as follows:

$ zip -s 700m -r archive.zip directory

Conclusion

It’s relatively easy to create archives using lossless data compression in Linux using the zip command. There are numerous benefits to archiving your data. You can use the unzip command to extract your archived data.

You can learn more about zip on the zip man page or by using the -h help switch, like this:

$ zip -h

The output will be similar to the below, but may vary, depending on which version you have installed:

Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:

zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

The default action is to add or replace zipfile entries from list, which can include the special name - to compress standard input. If zipfile and list are omitted, zip compresses stdin to stdout.

-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete OS files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt -n don't compress these suffixes
-h2 show more help
SHARE:
Photo of author
Author
My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

Leave a Comment