Introduction
ZIP is considered the most prevalent and widely utilized archive file format for compressing files as well as directories. With the help of ZIP, you can readily compress files into an archived format in order to save space & network bandwidth. Although, on Linux systems, the tape archive (tar) format is more typical. However, ZIP is still employed often because of its popularity. The Linux systems enable you to use the zip command to compress files into the ZIP format. On the other hand, you can also construct ZIP files through the GUI. Under this guide, we have provided insights into the most effective ways that explain how to zip a file in Linux. Dip into the sections below and get all the essential details.
System Requirements
Before we proceed toward learning how to create a zip file in Linux terminal, you must assure to meet the following prerequisites –
- Accessibility to the terminal.
- Commands for constructing example files and directories.
Types of File Compression in Linux
While there is an availability of various forms for data compression, the mentioned following are the most prevalent and typical in Linux. Take a quick glance at the points below and learn how to compress a file in Linux!
.zip files
This is the well-recognized form employed for archiving or compressing files. .zip serves the accessibility of cross-platform. You get the possibility to open as well as access a zip file in Linux and on any OS platform. This is because all the Linux, MacOS®, and Windows® operating systems provide default support for zip files. The fundamental syntax to compress a file in Linux with zip is –
.tar files
In contrast with other archive prospects, the tar command is not supposed to compress .tar files. The tar is known to bundle up the files into a single archived file. Thus, when you encounter a file having a ‘.tar’ extension, you must know that the archive method involved no compression of the files comprised within that archive. The fundamental syntax of the tar command for creating an archive is –
.tar.gz files
The tar.gz makes involvement of compression to the archive function of the tar command by making use of gzip function. You are only required to append the -z option to the basic tar command for adding compression. The fundamental syntax for implementing this is –
OR
.tar.bz2 files
Suppose you have an extra-large directory that you are required to compress to the maximum extent. In case tar.gz marks it in an oversized file, you can employ tar.bz2 rather. This method involves the addition of only one new element, which is -j. Here is the fundamental syntax for executing the same –
OR
Basic Commands for Zipping a File in Linux
Following are some basic commands that you should learn to know how to Zip a file in Linux.
– Using the zip command and compressed archive file
1. Construct files for archiving:
This command constructs four empty text files.
2. Make use of the zip command to archive the files:
This command displays the actions taken in the output and forms a files.zip archive.
3. List the contents of the ZIP File:
This command lists all the contents of an archive.
4. Add particular File Types to the ZIP Archive:
You need to employ a wildcard and give the filetype extension for adding only particular file types to a ZIP file in Linux, for instance –
This command appends all files possessing the .pdf extension to the archive.
5. Add a Directory to the ZIP Archive
This command first adds the empty directory, then settles it with the files.
6. Compress Multiple Files on Linux:
Alternatively, you can employ a wildcard in case you can group your files by extension:
– Syntax and options of the zip command
The zip command provides diverse work modes and alternatives to let you know how to ZIP file in unix. The following table shows a brief outline of the available options.
Tag | Option or Mode | Description |
-u–update | Mode | For updating and adding new files. For constructing a new archive if not found. |
-f–freshen | Mode | Updating files without adding new ones. Creating a new archive if not found. |
-d–delete | Mode | Picking entries in an existing archive & removing them. |
-U–copy-entries | Mode | Picking entries from an existing archive and copying them into a new archive. |
-e–encrypt | Option | Encrypting ZIP archive contents with a password.Beginning a password entry prompt. |
-i <files>–include <files> | Option | Including only particular files. |
-R–recurse-patterns | Option | Archiving files recursively. |
-sf–show-files | Option | Listing files and then, exiting |
-x <files>–exclude <files> | Option | Excluding particular files. |
-<number> | Option | Regulating compression speed (0-9). |
Also Read: How to Save a File in Vim/Vi Editor?
Advanced Commands With tar
1. Creating an Archive
For creating a tar archive also known as a tarball, employ tar with the -c or –create operation as –
For creating a tar.gz, tar.bz2, or tar.xz compressed archive, append the -z tag, -j tag, or -J tag respectively.
2. Removing Files After Creation
For removing the files from the disk after archiving, employ the –remove-files option at the end as –
3. Overwrite Control
With the tar overwrite control, you can handle the circumstances where file names in the archive tend to overlap with files in the working directory.
The three likely overwrite measures are:
1. Overwriting files in the working directory:
2. Not overwriting files in the working directory:
3. Extracting files only when they are newer than the current files:
4. Listing Archive Contents
Make use of the below command for listing the contents of an archive –
5. Discovering a File in an Archive
There exist two different methods to locate explicit content –
1. Using the -t tag by adding the file name (or names) after the command:
2. Using the grep command to filter the output:
6. Constructing Daily Backups
For automating daily backups, you have to build a bash script and add the below lines:
You can change the +1 parameter to +7 for weekly backups or +31 for monthly backups.
Also Read: How to Use the xargs Command in Linux?
Conclusion
ZIP serves as a compression & file packaging utility. It stores every file in a single .zip {.zip-filename} file that further possesses an extension .zip. With the zip, you can effectively compress the files in order to reduce the size of the file. Many times people own limited bandwidth between two servers but wish to transfer files at a faster speed. In such cases, they can simply zip the files and attain their objectives. Apart from Linux, zip is available and accessible under other operating systems as well, such as Windows, Unix, etc. Moreover, after reading this guide, you must have apprehended different practices that illustrate how to Zip a file in Linux terminal. Further, you can connect with our expert team if you get any relevant queries.