How to Zip Files and Directories from Linux Command Line

Written by: Linuxopsys   |   Last updated: May 14, 2022

Zipped files are compressed formats of the actual content, which makes the files or directories take less data space on the device hosting them. Zipping makes files easily transferable from one device to the other. 

In Linux system, zipping one or more files might seem tricky with the GUI of the operating system, but with just a line of commands through the Linux terminal, zipping your desired files or folders can be faster and more straightforward. 

In this tutorial, we learn how to zip files and directories in Linux using the zip command.

Zip Command Linux

zip command is used to package a set of files in the system. It will put one or more compressed files into a single zip archive file with the .zip extension. You can easily create an entire directory into zip archives using a single command.

Using the zip command can make file upload, emailing, or in fact, downloading files faster and more compact regardless of the number of files and their size.

Zip files work by encoding into fewer bites by removing redundant data. It supports lossless data compression and the cross-compatibility of devices and operating systems. Zip compression works betters for text files than MP3 or JPG files.

By default, zip uses the deflation compression method. Zip cannot guarantee is compress all files if cannot it uses the store method to keep it as it is. You may use bzip2 compression as option to replace deflate compression method.

You can use unzip command to extract zip files and by default the extracted files are stored in your current directory.

Why we use the zip command

The zip program puts multiple files in a single zip archive to achieve the following:

  • Reduce file size.
  • To transfer files faster between two servers.
  • Compress files into single zip archive files.
  • Saving disk space.
  • Temporarily compressing unused files.

Syntax:

The syntax of the Zip command:

zip OPTIONS ARCHIVE_NAME FILES

The “OPTIONS” refer to the action that the zip command will follow through such as recursive (-r), quiet (-q), removal (-d), etc.

ARCHIVE_NAME or ARCHIVE is the name you want to save the zip as, and FILES or INPATH is the name of the files or directories you are attempting to zip.

Install Zip Package on Linux Distributions

When you initiate a zip command most commonly you may end up seeing the error "zip command not found". This is because zip package is not installed on your Linux distribution.

Let's first check how to install zip on Linux.

Install Zip on Ubuntu

To install zip utility on Ubuntu and Debian, type:

sudo apt install zip

The command will prompt you to input your password in order to grant root user privilege.

Install Zip on Fedora

On the Fedora distribution of Linux, open your terminal and install zip using the following command:

sudo dnf install zip

Install Zip on CentOS and Redhat

On Redhat (RHEL) or CentOS Stream, installing zip will be through the following command:

sudo yum install zip

Zip Files and Directories

Let's check how to zip files and directories using the zip command.

To zip a single file named filename1 to a zip file named archive_output_file.zip, type:

zip archive_output_file.zip filename1
zip a single file

To confirm the zip file created, you can use ls command:

list zip file using ls command

You can see that archive_output_file.zip the zipped file now exists in your current directory.

To zip multiple files, use the following command:

zip archive_output_file.zip filename1 filename2 filename3
zip multiple files

In the above output, deflated means the file is compressed using deflate compression method and store means stored to archive but not compressed.

To zip an entire directory structure use -r option. This will enable the zip command to zip the directory recursively i.e zipping the folder along with all the files in that same directory.

To zip a directory structure recursively, run the following command:

zip -r archive_output_file.zip directory1

You may specify multiple directories to get it all compressed in the zip file:

zip -r archive_output_file.zip directory1 directory2 directory3
zip multiple directories

To archive all the files including the hidden files in a specified directory, type:

zip archive_output_file.zip .* *

To suppress zip output which is by default showing file names that are being added to the archive and compression method, use -q option.

Bzip2 Compression Support

Zip allows support for bzip2. When bzip2 is enabled, the default compression deflate is replaced.

Use -Z option to specify the compression method:

zip -r -Z bzip2 archive_output_file.zip directory1
zip to add bzip2 compression method

Level of Compression to Files

Zip command allows to regulate the speed of compression. The levels range from 0 to 9, where level 0 ( fastest compression speed ) stores files without compression and level 9 which is the optimal compression level (slowest compression speed).

Zip automatically chooses level 6 as the default compression level when archiving files.

For example, to zip all the txt files in the current working directory without compressing them by using level 0 compression, type:

zip -0 archive_output_file.zip *.txt

Higher compression levels would give more stress on the CPU and time-consuming.

Zip Command Examples

Let’s look at some zip command examples showing functionalities for manipulating an archive.

Password Protected Zip file

You can protect your files in the archive file using the -e option. This keeps your files encrypted.

You will be prompted to enter a password to protect the archive file.

zip -e archive_output_file.zip file1 file2 file3
protect zip file with a passwod

To set the password for an existing archive file, named archived_file.zip, run the following command:

zipcloak archived_file.zip

Split Zip Files

There are certain conditions where your zip file might be very large and it’s restricting you from transferring the file or emailing the file due to size restrictions, then you can split the file into the specified sizes.

To split archives into multiple files, use -s option. For example, the following command split the compressed file named archive_output_file.zip into 10MB for each new archives.

zip -r -s 10m archive_output_file.zip directory1

You need to use -F option to combine the split zip files into a single zip archive.

zip -F archive_output_file.zip --out combine_archive.zip

You should combine all split files then you are able to unzip it.

An alternative command to split zip archive is zipsplit.

Remove Files From the Zip File

To delete unwanted files from the zip archive file, use -d option.

In the following example, files named file1 and file2 are removed from the myarchive.zip file.

zip -d myarchive.zip file1 file2
remove files named file1 and file2 from the myarchive.zip zip file

Adding New files to an Existing Zip File

Use -u option to add new files to an existing zip archive. For example, let’s add three new files named file1, file2, and file3 to an already existing file named myarchive.zip file.

zip -u myarchive.zip file4 file5
Add more files to existing zip file

Excluding Files, While Creating a Zip File

Use -x option to exclude specific files while creating a zip file.

In the following example, we zip everything in my current directory except file1 and file4.

zip mybackup.zip .* * -x file1 file2
exclude specified file while performing zip operation

Verbose Mode When Creating a Zip File

The verbose mode prints the diagnostic screen. This shows which file got compressed and how much space each file saved and the total % of compression.

zip -v mybackup.zip *
zip verbose mode output

Zip Command Options

Some of the useful zip command options are: 

OptionsDescription
-rZip recursively through the directory structure
-uAdd new files to the existing zip file
-dDelete entries from a zip file
-xExclude files or folders while zipping
-mDeletes the original files after making the specified zip archive
-0Compression level - archives your file without compressing
-9 Highest compresses level
-qQuiet operation whereby output will not be displayed
-VVerbose mode to print diagnostic information
-jThis option stores only file names and deletes or junks (don't record) directory names
-eEncrypt with a password
-ZSet to a specific compression method
-DDo not add entries for directory in zip archive
-@ Read the list of input files from the standard input.
-fUpdates existing files only
-TTest zip file integrity
-FTo fix the offsets
 -iTo include only the listed filenames
-y Stores symbolic links as such instead of the referenced file
 -nExclude compression to files with given suffixes

Conclusion

In this tutorial, we learned how to zip files and directories in Linux. The zip command is a very handy command for Linux users that deals with files and directories. This enables Linux users to move, store, or share their files easily.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment