Linux Zip Commands to Archive or Unarchive files

Extracting zip files on Linux using the command line a piece of cake. It is super easy and the tool we require to do that mostly comes pre-installed. However, if not then here we let you know not only how to install and use ZIP commands on Linux such as Ubuntu, AlmaLinux, Rocky Linux, CentOS, Linux Mint, etc.

Archives files are very useful to compress and store multiple files into a single file. For example, if want to upload a folder containing multiple files online, then rather than doing this for every single one, we bundled them in an archive. So that other users can easily download them. Furthermore, various compression algorithms are there to keep the data size as small as possible, which also favors the transfer.

In most Linux distributions, standard tools are already installed that allow to extract ZIP files graphically and unpack an archive. For that usually, you have to just right-click on the archive file and select the unzip option. However, if you are looking for a command-line way then here it is.

 

1. Install ZIP on Linux

In order to be able to create or unpack Zip archives under Linux, you must first ensure that the required tools are available on the system. Although, generally ZIP tool comes pre-installed however, if not then use the below given commands as per your distro:

If you are using a Linux distro with APT package management such as Ubuntu, Debian, Linux Mint, Kali, Mx Linux, POP! OS etc then use:

sudo apt install zip unzip

Whereas the RPM baes Linux such as RHEL, CentOS, Fedora, AlmaLinux, Rocky Linux and others can use:

sudo dnf install zip unzip

 

2. Zip Linux Command Syntax

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.

# Here are the options that we can use with the ZIP commands-

-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
[email protected] 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

Let’s see some common commands that will help us to use this tool for extracting archive files and repacking the ones we want.

3. Create and update zip archive

The syntax of the individual commands is always the same:

zip options archive.zip files and folders list

If the archive already exists, the files and folders are added to the archive or updated. The archive is not replaced.

Add individual files to an archive:

zip archive-filename.zip file1 file2 (...)

With this command, an archive “archive-filename.zip” with the files “file1” and “file2” is created or updated in the current directory.

To add a folder to an archive:

zip -r archive-filename.zip folder1 folder2 (...)

This command creates an archive “archive-filename.zip” with the folders “folder1” and “folder2” including all subfolders and files in the current directory.

Create Zip archive and delete the original files

With creating of Archive file you want to remove the original files or folders that have been used to create the ZIP archive file of yours, then use -m option. It meant to move files permanently rather than copying them to the archive.

Example:

zip -m archive-filename.zip file1 file2

 

4. Removes/Delete folders and/or files from an archive:

zip -d archive.zip file1 file2 folder / * (...)

The command removes the files “file1” and “file2” as well as the folder “folder” & its contents from the archive “archive.zip”

Show more options:

zip -h2

Displays complete help with all available options.

 

5. Display the contents of a zip archive

unzip -l archive.zip

This command shows the complete content of the archive “archive.zip”

 

6. Password protected Linux ZIP file

Many times, we need protection for sensitive files or data while archiving them. Thus, if you want to create a Linux zip with a password then simply use -e option. With this, we can significantly improve the security of our zipped files.

zip -e archive.zip file1 file2

As you use -e parameter, the system will ask you to create a password for the files by entering it two times.

 

7. Unpack the zip archive

Unpack the complete archive:

unzip archive.zip

Unpacks the complete archive into the current directory.

Extract archive to a specific location:

unzip archiv.zip -d /tmp/target

Extract the complete archive to “/tmp/target”

Extract only certain files or folders:

unzip archive.zip file1 folder1 file2 -d /tmp/target

-d stands for the directory.

Show more options:

unzip -hh

Displays complete help with all available options.

unzipsfx options:
-c, -p - Output to pipe. (See above for unzip.)
-f, -u - Freshen and Update, as for unzip.
-t - Test embedded archive. (Can be used to list contents.)
-z - Print archive comment. (See unzip above.)

unzipsfx modifiers:
Most unzip modifiers are supported. These include
-a - Convert text files.
-n - Never overwrite.
-o - Overwrite without prompting.
-q - Quiet operation.
-C - Match names case-insensitively.
-j - Junk paths.
-V - Keep version numbers.
-s - Convert spaces to underscores.
-$ - Restore volume label.

 

Ending note:

These were few common ZIP commands that we can use on Linux operating system for packaging or unpacking archive files using the terminal.

 

 

Other tutorials:

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

X