How To Zip, Unzip Files In Linux?


Winzip, Winrar are popular packaging and compression tools for Windows operating system. Winzip was very popular in old times and this made the zip compression algorithm popular too. Currently, a lot of files and folders are compressed with zip algorithms too. Zip compression is supported by Minix, Atari, MacOS, Android too. We look at how to compress and decompress with the zip algorithm in this post.

Install Zip For Ubuntu, Debian, Mint, Kali

To use zip functionality required packages should be downloaded they are not installed by default. We will install the packages named zip and unzip for deb based distributions.

$ sudo apt-get install zip unzip -y
Install Zip
Install Zip For Ubuntu, Debian, Mint, Kali

Install Zip For Fedora, CentOS, RHEL

We can use yum package manager in order to install zip.

$ sudo yum install zip

zip Command Syntax

zip command uses the following syntax. It accepts multiple files and folders to zip with a single step with.

zip OPTIONS ARCHIVE INPATH INPATH ...
  • OPTIONS will be used to set some attributes or behavior to the zip command like compressions level etc.
  • ARCHIVE is the name of the compressed file which will be created.
  • INPATH is the files and folders we can compress. There may be more than one INPATH.

Compress A File

We will start with a simple example where we will compress a single file named report.xm and compress it into an archive named report.zip.  We can see from the output that the compression level is about 63%.

$ zip report.zip report.xml
Compress A File
Compress A File

Compress Multiple Files With Zip

We can compress multiple files with zip command we will add all files and folders at the end of the command as a parameter. In this example will compress files and folders named report.xml, workspace/, example.txt into an archive named backup.zip.

$ zip backup.zip report.xml workspace/ example.txt httpd-2.4.38.tar.bz2.asc package.json
Compress Multiple Files With Zip
Compress Multiple Files With Zip

Compress Multiple Files According To Their Extension

We can compress multiple files according to their extension. We will use the glob for bash and provide the extension like txt, png, XML, etc. In this example, we will compress text files with *.txt extension into an archive file named text_backup.zip.

$ zip text_backup.zip *.txt
Compress Multiple Files According To Their Extension
Compress Multiple Files According To Their Extension

Compress Folders and Directories

Compressing files requires no option to zip command but compressing folders requires a recursive flag to the zip command. In order to compress directories or folders, we have to provide the -r option like below.

$ zip -r backup.zip perl5/ masscan/ nmap/
Compress Folders and Directories
Compress Folders and Directories

Compress File By Searching Them

find command can be used to find for a specific file by searching its name, extension, size, etc. and then we can compress the files or folders with the zip command. In this example, we will search the current working directory for files with txt extension and compress them in their current path.

$ find . -name "*.txt" -print | zip source [email protected]
Compress File By Searching Them
Compress File By Searching Them

Decompress or Extract Zip Files

Decompressing is easier than compressing we just provide archive file to unzip command. In this example, we will unzip the compressed file named  text_backup.zip. We may be asked the replacement question if the same files exist.

$ unzip text_backup.zip
Decompress Zip Files
Decompress Zip Files

Extract Specific File From Zip Archive

We can decompress a specific file from a zip archive like below. In this example, we will extract files with the rc as an extension.

$ unzip test.zip *rc

This will extract only files ends with rc.

LEARN MORE  How To List and Unrar Rar Files In Ubuntu, Debian, Kali, Mint?

Leave a Comment