Often you will need to compress files & folders before sending them to others, or for the purpose of archival. Linux offers many compression tools like gzip and tar for this purpose. It also supports zip compression utility, which is commonly used in Windows. Zip is a lossless compression tool supported by all Linux systems. It allows you to easily compress files & folders to transfer or store. The compressed archives have .zip extension. In this article, we will look at how to zip files & folders in Linux.
How to Zip Files & Folders in Linux
Here are the steps to zip files & folders in Linux.
1. Install Zip
Open terminal and run the following command to install zip utility.
$ sudo apt-get update $ sudo apt-get install zip -y
$ sudo yum update $ sudo yum install zip
2. Zip Files & Folders
Here is the syntax to zip one or more files & folders
$ zip zip_file_name file(s)
In the above command you need to mention the name of your zip archive after zip command, followed by a space-separated list of file names.
If you only mention zip_file_name then the zip file will be created in your present working directory. If you want to create zip file in another directory, specify full path to zip file.
It is important that you have the permission to create zip file in the target directory. Otherwise use sudo keyword before zip command above.
Now we will look at a few common use cases for your reference.
How to Zip Files in Linux
Let us say you have to compress 3 files file1.txt, file2.txt and file3.txt in your present working directory, just run the following command to zip them.
$ sudo zip myfiles.zip file1.txt file2.txt file3.txt adding : file1.txt (stored 0%) adding : file2.txt (deflated 30%) adding : file3.txt (stored 0%)
It displays the compression percent for each file.
Please note, if you do not specify .zip extension to your zip file, it will automatically add it.
How to Zip Files in Directory
If you want to compress all files in directory, you need to mention -r option for recursive compression. Let us say you want to compress folders /home/data and /home/project then here is the command to do it.
$ sudo zip -r myfiles.zip /home/data /home/project adding : /home/data/file1.txt (stored 0%) adding : /home/data/file2.txt (deflated 10%) adding : /home/project/file3.txt (deflated 20%) ...
The above command will show the compression percent for each file and can be quite big depending on your folder contents. If you don’t want to view the compression progress, use -q option for quiet mode.
$ sudo zip -r myfiles.zip /home/data /home/project
How to Zip All Files in Directory
If you want to compress all files in directory and not the directory itself, you need to use wildcard patterns as shown below. Here is an example to compress all files in /home/data folder
$ sudo zip -q myfiles.zip /home/data<em>/*</em>
In the above case, zip utility will add all files & directories present in the mentioned directory to your archive.
How to Zip Hidden Files
If you also want to zip hidden files, use *.* wildcard expression as shown below. Here is an example to compress hidden files in /home/data
$ sudo zip -q myfiles.zip /home/data/.* *
That’s it. In this article, we have seen how to install zip compression utility, and also several common use cases of file compression.