How to Zip a File in Linux? – Hostbillo 


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

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 –

$ zip -r <name of the zip file>.zip <directory or file(s) you want to compress>
.zip files
.zip files

.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 -cvf <name of archive file>.tar <directory to archive or files to archive>
.tar files

.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 – 

$ tar -zcvf <archive name>.tar.gz /directory/you/want/to/compress


$ tar -zcvf <archive name>.tar.gz ‘*.jpg’
.tar.gz files

.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 – 

$ tar -jcvf <archive name>.tar.bz2 /directory/to/compress


$ tar -jcvf <archive name>.tar.bz2 ‘*.jpg’
.tar.bz2 files

Basic Commands for Zipping a File in Linux

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:

touch file{1..4}.txt

This command constructs four empty text files.

Construct files for archiving

2. Make use of the zip command to archive the files:

zip files file1.txt file2.txt file3.txt file4.txt 

This command displays the actions taken in the output and forms a archive.

Make use of the zip command to archive the files

3. List the contents of the ZIP File:

zip -sf

This command lists all the contents of an archive. 

List the contents of the ZIP File

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 – 

zip files *.pdf

This command appends all files possessing the .pdf extension to the archive.

Add particular File Types to the ZIP Archive

5. Add a Directory to the ZIP Archive

zip -r files <directory>

This command first adds the empty directory, then settles it with the files.

6. Compress Multiple Files on Linux:

$ zip file1 file2 file3 file4 file5

Alternatively, you can employ a wildcard in case you can group your files by extension: 

$ zip *.pdf
Compress Multiple Files on Linux

– 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 ModeDescription
-u–updateModeFor updating and adding new files. For constructing a new archive if not found.
-f–freshenModeUpdating files without adding new ones. Creating a new archive if not found.
-d–deleteModePicking entries in an existing archive & removing them.
-U–copy-entriesModePicking entries from an existing archive and copying them into a new archive.
-e–encryptOptionEncrypting ZIP archive contents with a password.Beginning a password entry prompt.
-i <files>–include <files>OptionIncluding only particular files.
-R–recurse-patternsOptionArchiving files recursively.
-sf–show-filesOptionListing files and then, exiting
-x <files>–exclude <files>OptionExcluding particular files.
-<number>OptionRegulating compression speed (0-9). 

Also Read: How to Save a File in Vim/Vi Editor?

Advanced Commands With tar

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 – 

tar cf <archive name>.tar <file(s) or location(s)>
Creating an Archive

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 – 

tar cf <archive> <file(s) or location(s)> –remove-files
Removing Files After Creation

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:

tar xf <archive> <Optional file(s) or location(s)> –overwrite
Overwriting files in the working directory

2. Not overwriting files in the working directory:

tar xf <archive> <Optional file(s) or location(s)> –keep-old-files
Not overwriting files in the working directory

3. Extracting files only when they are newer than the current files:

tar xf <archive> <Optional file(s) or location(s)> –keep-newer-files
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 –

tar tf <archive>
Listing Archive Contents

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:

tar tf <archive> <file(s)>
Discovering a File in an Archive

2. Using the grep command to filter the output:

tar tf <archive> | grep <file(s)>
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:

tar czf backup-$(date +%Y%m%d).tar.gz files find backup* -mtime +1 -delete
Constructing Daily Backups

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?


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. 

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *