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 zip compression algorithm popular too. Currently, a lot of file and folders are compressed with zip algorithm too. Zip compression is supported by Minix, Atari, MacOS, Android too. We look how to compress and decompress with 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

Install Zip For Fedora, CentOS, RHEL

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

$ sudo yum install zip

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 to 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 we will add all file 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 -@
Compress File By Searching Them
Compress File By Searching Them

Decompress 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 replace 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 extension.

$ unzip test.zip *rc
Extract Specific File From Zip Archive
Extract Specific File From Zip Archive

This will extract only files ends with rc.

Leave a Comment