List of content you will read in this article:
The most popular lossless data compression archive file format is Zip. A Zip file is a data object that encapsulates one or more compressed files and folders. Uncompressed files take up more disc space and take longer to transmit from one device to another. Zip files may be readily extracted on Microsoft Windows, macOS, and Linux operating systems using the appropriate software for each platform. In this guide, you'll learn how to use the zip command in Linux with examples [ZIP File Linux/Zip Folder Linux] to compress files and directories.
Zip command in Linux with examples
Zip is a command-line tool that aids in the creation of Zip archives.
The syntax for the zip command is as follows:
zip Option Archive-name File
A user must have write permission on a directory in order to build a Zip archive in that directory.
Zip files do not allow ownership information in the Linux way. The person who executes the command owns the extracted files. Use the tar command to keep file ownership and permissions intact. Most Linux distributions do not include the zip program by default, but you may simply install it using your distribution's package management.
Zip may be installed on Ubuntu and Debian systems by the following command:
sudo apt install zip
Zip may be installed on CentOS and Fedora systems by following this command: CentOS and Fedora
sudo yum install zip
How to Compress Files and Directories in Linux [zip file and folder in linux]
To zip file/folder in Linux, you have to divide the files to be included in the pack by a space, as demonstrated below:
zip xyz.zip file1 file2 file3
adding: file1 (deflated 53%)
adding: file2 (stored 0%)
adding: file3 (deflated 48%)
The zip command publishes the names of the files that have been added to the archive as well as the compression technique by default. Unless the file which has to be compressed contains a .zip extension, the extension is created automatically if the archive name does not conclude with it.
zip xyz.zip file1 will produce the same archive as zip xyz file1.
1. Use the -q option to suppress the output of the zip command:
zip -q xyz.zip file1 file2 file3
Creating a zip archive of a directory that includes the content of subdirectories is common.
2. You may recursively scan the full directory tree using the -r option:
zip -r xyz.zip dir1
3. In the same zip file linux, you can also add numerous files and directories:
zip -r xyz.zip dir1 dir2 filename1 filename2
Compression Methods and Levels [Zip Compression]
Deflate is Zip's default compression algorithm. If a file cannot be compressed, the zip utility just puts it in the file as it is, instead of compressing it with the store method. The bzip2 compression algorithm is supported by the zip program in most Linux variants.
Use the -Z option to select a compression method.
zip -r -Z bzip2 xyz.zip dir1
adding: sub_dir/ (stored 0%)
adding: sub_dir/filename1 (bzipped 27%)
adding: sub_dir/filename2 (bzipped 68%)
The zip command lets you define a compression level from 0 to 9 by providing a number preceded with a dash. The compression level is set to -6 by default. When -0 is used, all files are saved without compression. -9 forces the zip command to perform the best compression possible for all files.
For example, if you wanted to utilize compression level -9, you would type:
zip -9 -r xyz.zip dir1
The zip operation becomes more CPU-intensive when the compression level is increased, and it takes longer to complete.
How to Create a Password-Protected ZIP file?
If you have sensitive data that has to be saved in the archive, use the -e option to encrypt it:
zip -e xyz.zip dir1
The archive password must be entered and verified using the following command:
The password you will be entering will not be seen on the screen. However, the next time you log in, make sure you remember your password.
How to Create a Split Zip File?
Assume you wish to upload the Zip archive to a file hosting provider with a 1GB upload restriction, and your Zip archive is 5GB in size.
To create a new split Zip file, use the -s option followed by size. k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes) can be used as the multiplier.
zip -s 500m -r xyz.zip dir
After a set hits the stated size limit, the operation above will keep producing new archives in it.
Bonus Zip Commands in Linux
1. Create a xyz.zip file that contains all of the files in the current directory.
zip xyz *
2. You may use the following command to include all the hidden files:
zip xyz .* *
3. Make a xyz.zip file that contains all MP4 files in the current directory, compressed.
zip -0 xyz *.mp4
The zip command in Linux may be used to build Zip archives [Zip files and Folders in Linux]. On a Linux operating system, the unzip command may also be used to decompress a ZIP file. We have seen how to zip various files and directories in this tutorial and various available zip command options in Linux.
People are also reading: