This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

ZIP is the most widely used archive file format that supports lossless data compression. A ZIP file is a data container containing one or more compressed files or directories.

In this tutorial, we will show you how to compress and decompress files in Linux systems through the command line using the zip and unzip command.

In this tutorial, we are using commands with examples. So, replace the “path” with the file path, “filename” with the name of the file and “.ext” with the extension of the file.

Installing zip

Installing zip On Debian Based Systems

root@codesposts:~$ apt install zip

Installing zip On Red-Hat Based Systems

root@codesposts:~$ yum install zip

Installing unzip

Installing unzip On Debian Based Systems

root@codesposts:~$ apt install unzip

Installing unzip On Red-Hat Based Systems

root@codesposts:~$ yum install unzip

Compressing Files

There are a number of different ways to compress files using the Linux Command Line. zip command is one of the most widely used commands to compress files using linux command line.

General Syntax

Following is the general syntax for the zip command

zip [options] zipfile filename

Compressing A Single File

To compress a single file using the zip command, you can run the command like this:

root@codesposts:~$ zip zipname.zip filename.ext

This command will compress the file “filename.ext” into the archive “zipname.zip”.

Compressing Multiple Files

To compress multiple files using zip command, you can run the command like this:

root@codesposts:~$ zip zipname.zip file1.ext file2.ext file3.ext

This command will compress all three files “file1.ext, file2.ext, file3.ext” into a single archive “zipname.zip”.

Compressing Directory

You can also compress a whole directory using zip command. You can follow the command below:

root@codesposts:~$ zip zipname.zip /path/to/directory

Removing File From Zip

If you want to remove a file from the zip archive, you can use -d option with the zip command.

root@codesposts:~$ zip -d zipname.zip filename.ext

This command will delete the file “filename.ext” from the archive zip file “zipname.zip”.

Updating File In The Zip

If you want to remove a file from the zip archive, you can use -u option with the zip command.

root@codesposts:~$ zip -u zipname.zip filename.ext

Deleting Original Files After Compressing Into Zip File

If you want to delete the files after compressing them into the zip file, you can use the option -m option with the zip command.

root@codesposts:~$ zip -m zipname.zip filename.ext

This will compress the file “filename.ext” into the archive “zipname.zip” and then delete the target file. This actually moves the target file into the zip file.

Compress A Directory Recursively

If you want to compress a directory recursively into a zip file, you can use the option -r with the zip command.

root@codesposts:~$ zip -r zipname.zip /path/to/directory

Exclude Files While Compressing

If you want to compress the current directory excluding some files, you can use -x option with the zip command.

root@codesposts:~$ zip –x zipname.zip file_to_be_excluded

Display Progess While Compressing

If you want to display the progress of the compression while compressing files, you can use -v option with the zip command.

root@codesposts:~$ zip -v zipname.zip /path/to/directory

Decompressing Files

There are a number of different ways to compress files using the Linux Command Line. unzip command is one of the most widely used commands to compress files using linux command line.

General Syntax

Following is the general syntax of the unzip command

unzip [zipname].zip

Decompressing A Single Zip File

If you want to decompress a zip file in the current directory, you can run the command like this:

root@codesposts:~$ unzip zipname.zip

Decompressing Multiple Zip Files

If you want to decompress multiple zip files in the current directory, you can run the command like this:

root@codesposts:~$ unzip file1.zip file2.zip file3.zip

Decompressing Into A Specific Directory

If you want to decompress a zip file into a specific directory, you can use the option -d with the unzip command

root@codesposts:~$ unzip zipname.zip -d /path/to/directory

Decompress Without Displaying Any Output

If you want to suppress the output of the unzip command, you can use the option -q with the unzip command.

root@codesposts:~$ unzip -q zipname.zip

Decompress A Password Protected File

If you want to decompress a password protected zip file, you can use
the option -P with the unzip command.

root@codesposts:~$ unzip -P [password] zipname.zip

Replace the [password] with the password of the zip file.

Excluding Files While Decompressing

If you want to exclude some files while decompressing a zip file, you can use the option -P with the unzip command.

root@codesposts:~$ unzip zipname.zip –x file_to_be_excluded

Overwrite Existing Files While Decompressing

If you want to overwrite the existing files while decompressing a zip file, you can use the option -o with the unzip command.

root@codesposts:~$ unzip -o zipname.zip

Decompress Zip File Without Overwriting Existing Files

If you don’t want to overwrite the existing files while decompressing a zip file, you can use the option -n with the unzip command.

root@codesposts:~$ unzip -n zipname.zip

Decompress All Zip Files In The Directory

If you want to decompress all the zip files at once, you can runt he command like this:

root@codesposts:~$ unzip *zip

Display Contents Of Zip File

If you want to display the list of the contents of a zip file, you can use the option -l with the unzip command.

root@codesposts:~$ unzip -l zipname.zip

This command will return:

  • Length in bytes
  • Date created
  • Time created
  • Name