Last updated on February 10, 2018
In this post, you will find practical examples showing how to install and use the zip command to compact and organize files within your file system.
Zip is a compress tool which is available in most of the operating systems such as Linux/Unix, Apple OS, Microsoft OS etc.
Install zip and unzip command in Linux?
On Debian/Ubuntu, you can use the following commands to install zip/unzip:
sudo apt-get install zip unzip
On Redhat, Centos and Fedora based machines
yum install zip unzip
List of Zip and Unzip Linux Commands
Syntax for zipping a file:
$ zip myfile.zip myfile.txt
Syntax for zipping archive of several files:
$ zip myfiles.zip file1.txt photo.jpg script.js
The syntax for zipping a directory: Zipping a folder is a tricky thing as by default zip will not zip entire folder content such as subfolders and files. To zip with all files and subfolders we have to pass -r
parameter, then it will recursively zip.
$ zip -r photos.zip photos
Syntax for zipping a files and folders:
$ zip -r myFiles.zip file1.txt photo.jpg script.js photos downloads
Syntax for checking if a zipped archive is fine or not:
$ unzip -tq myFiles.zip
Syntax for unzipping a zip archive:
$ unzip myFiles.zip
Syntax for Deleting a file in an archive with out extracting entire zip file:
$ zip -d myFiles.zip downloads/photo.jpg
Syntax for seeing the contents of a zip archive:
$ unzip -l myFiles.zip
Syntax to extract a file from a zip archive:
$ unzip myFiles.zip photo.jpg
Syntax to unzip a zip archive to a specific location:
$ unzip myFiles.zip new_downloads or $ unzip myFiles.zip -d new_downloads
Syntax to Updating all the files in zip file if the original files are modified:
$ zip -fr myFiles.zip downloads/
Syntax to Updating a particular file which is modified and update our zip file with this update:
$ zip -u myFiles.zip downloads/photo.jpg
Syntax to exclude a file or folder in a folder when compressing it:
$ zip -r myFiles.zip downloads/ -x downloads/photo.jpg downloads/uploads
Syntax to compress fast :
$ zip -1 -r myFiles.zip downloads
To compress with better ratios use -9