How to Use Zip & Unzip in Linux

December 12, 2022

How to Use Zip & Unzip in Linux

This guide is part of the “Linux Commands” series. This series is focused on providing an in-depth overview of Linux commands and tools, in an easy-to-follow manner!


Zip and unzip are two commonly used commands in Linux for compressing and archiving files.

This tutorial will show you how to use these commands to manage your files.

Some common uses for zip files include:

  • Sharing files: Zip files are commonly used to share large numbers of files, such as photos or documents, over the internet.
  • Reducing file size: Compressing files into a zip archive can reduce their size, making it easier to store them on a hard drive or send them via email.
  • Protecting files: Zip files can be password-protected to prevent unauthorized access to their data.
  • Organizing files: Zip files can be used to organize and group related files together, making it easier to manage large numbers of files.
  • Backing up data: Zip files can be used to create backups of important data, allowing users to restore their files in the event of data loss.
  • Installing software: Some software applications are distributed as zip files, which must be unzipped before being installed and used.
  • Encrypting data: Zip files support encryption, allowing users to protect sensitive data by encrypting it before adding it to a zip archive.
OptionDescription
-rRecursively zips the contents of directories.
-jExcludes the path information for files being zipped.
-pEncrypts the contents of the zip file with a password.
-uUpdates an existing zip file with new or changed files.
-vVerbose mode, which displays detailed information about the progress of the zip operation.
-mDeletes the original files after they have been added to the zip file.
-xExcludes specified files from the zip file.
-dSelect entries in an existing archive and delete them.

These are the most common options; the full list is in the zip manual.

OptionDescription
-lLists the contents of the zip file without extracting it.
-tTests the integrity of the zip file to ensure it is valid.
-vVerbosely lists the contents of the zip file.
-xExtracts files from the zip file, but excludes specified files or directories.
-dExtracts files to a specified directory.
-jExtracts files from the zip file without preserving the directory structure.
-nExtracts files from the zip file, but only if the file has a specified suffix.
-oOverwrites existing files without prompting.
-FSpecifies the name of the first split file when extracting split zip files.

For a full list of options and more detailed information, you can use the unzip command with the -h or --help option.

How to Install zip & unzip

For Debian-based systems, such as Ubuntu:

sudo apt-get update
sudo apt-get install unzip zip

For CentOS and Fedora:

sudo yum update
sudo yum install unzip zip

If you are using a different system, you can try using similar commands to install unzip and zip. For example, on some systems you may need to use dnf instead of yum, or use a different package manager altogether. You can usually find instructions on how to install these tools on your specific system by doing a quick search online.


How to Use zip & unzip in Linux

The following section is dedicated entirely to common use cases for zip and unzip.

Creating a zip file

To create a zip file in Linux, you can use the zip command. Here is an example of how to use it:

zip my_zip_file.zip file1 file2 file3

This will create a zip file called my_zip_file.zip that contains the files file1, file2, and file3. You can replace these filenames with the actual names of the files you want to include in the zip file.

If you want to include all the files in a directory, you can use the -r flag to recursively add the files in the directory and its subdirectories. For example:

zip -r my_zip_file.zip my_directory

This will create a zip file called my_zip_file.zip that contains all the files and subdirectories in the my_directory directory.

You can also specify the full path to the files you want to include in the zip file, like this:

zip myfiles.zip /path/to/file1.txt /path/to/file2.txt /path/to/file3.txt

You can use the -v option to display a verbose output of the files being added to the zip file. This can be useful for checking that the correct files are being included. For example:

zip -v mydir.zip mydir

How to unzip a file

To unzip a file in Linux, follow these steps:

  1. Open a terminal window.
  2. Navigate to the directory where the zip file is located.
  3. Use the unzip command to extract the contents of the zip file. The basic syntax for the unzip command is as follows:
unzip myzipfile.zip

This will extract the contents of the zip file into the current directory. To extract the contents of the zip file into a specific directory, you can use the -d option, followed by the path to the destination directory. For example:

unzip myzipfile.zip -d /path/to/destination

How to remove individual files from a .zip file

To remove individual files from a zip file in Linux, you can use the zip command with the -d option, followed by the names of the files you want to delete. The basic syntax for this command is as follows:

zip -d <zipfile.zip> <file1> <file2> ...

For example, to delete the files file1.txt and file2.txt from a zip file called myzipfile.zip, you would run the following command:

zip -d myzipfile.zip file1.txt file2.txt

This will delete the specified files from the zip file. Note that this operation cannot be undone, so be careful when using it.

You can also use the -i option to specify a pattern that matches the names of the files you want to delete. This can be useful if you want to delete multiple files with similar names. For example, to delete all files with the .txt extension from the myzipfile.zip file, you would run the following command:

zip -d myzipfile.zip \*.txt

The -i option uses the same pattern-matching syntax as the rm command, so you can use wildcards and other pattern-matching characters to specify the files you want to delete.


How to update zip files (add new or update existing)

To update a zip file by adding a new file to it, you can use the zip command with the -u option, followed by the zip file's name and the file you want to add. The basic syntax for this command is as follows:

zip -u <zipfile.zip> <file>

For example, to add a file called newfile.txt to a zip file called myzipfile.zip, you would run the following command:

zip -u myzipfile.zip newfile.txt

This will add the newfile.txt file to the myzipfile.zip zip file, without affecting any of the other files that are already in the zip file.

You can also use the -g option to update an existing file in the zip file with a newer version of the same file. This can be useful if you want to replace an old version of a file with a newer one. For example, to update the file1.txt file in the myzipfile.zip zip file with a newer version of the same file, you would run the following command:

zip -g myzipfile.zip file1.txt

This will replace the old version of the file1.txt file with the new one.


How to exclude files when using unzip

To exclude files using the unzip command in Linux, you can use the -x option followed by the name of the file you want to exclude. For example, if you want to unzip the example.zip archive but exclude the file1.txt file, you can use the following command:

unzip example.zip -x file1.txt

This will unzip all the files in the example.zip archive except for file1.txt. Note that this only works for unzipping a single file - if you want to exclude multiple files, you'll need to use the -x option multiple times, once for each file you want to exclude.

unzip example.zip -x file1.txt -x file2.txt -x file3.txt

You can also use the -x option to exclude specific file extensions/formats:

unzip example.zip -x '*.txt'

This will unzip all the files in the example.zip archive except for those with the .txt file extension. You can use the -x option with a wildcard pattern to exclude files of any file format, not just .txt files.

unzip example.zip -x '*.doc' -x '*.pdf' -x '*.jpg'

This will unzip all the files in the example.zip archive except for those with the .doc, .pdf, and .jpg file extensions.


How to unzip a password-protected file

To unzip password-protected files in Linux, you will need to use the unzip command along with the -P option:

unzip secured.zip -P password123

How to create a zip file with a password

To create a password-protected zip file, you can use the zip command with the -e option. This will allow you to encrypt the zip file's contents with a password:

zip -e encrypted.zip /path/to/files

How to merge multiple zip files

To merge multiple archives with zip, you can use the zip command with the -F option. This allows you to specify the archive you want to merge into, followed by the names of the archives you want to merge.

zip -F main.zip file1.zip file2.zip file3.zip

You can also use the -u option to update an existing archive with the contents of another archive, without overwriting any existing files. Here is an example:

zip -u main.zip file1.zip

In this example, main.zip will be updated with the contents of file1.zip, but any existing files in main.zip will not be overwritten.


How to split large zip files

To split a large zip file into multiple smaller files, you can use the split command.

This command allows you to specify the size of the split files and the prefix to use for the split files.

split -b 50m large.zip split_

In this example, the split command will split the large.zip file into multiple files, each with a maximum size of 50 MB. The split files will be named split_aa, split_ab, split_ac, and so on.

To extract the split files, you will need to use the cat command to concatenate the split files into a single file, and then use the unzip command to extract the contents of the concatenated file:

cat split_* > large.zip
unzip large.zip

This will concatenate the split files into a single file called large.zip, and then extract the contents of the file. Keep in mind that the split files must be in the same directory as the cat and unzip commands.

Alternatively, you can use the zip command with the -s option to split a zip file into multiple files. Here is an example:

zip -s 50m large.zip

This will create multiple files, each with a maximum size of 50 MB, that contain the contents of the large.zip file. The split files will be named large.z01, large.z02, large.z03, and so on.

To extract the split files, you can use the unzip command with the -F option to specify the name of the first split file. Here is an example:

unzip -F large.z01