Forum Navigation
Please to create posts and topics.

Linux - Zip (archive and compress) multiple files

How to archive and compress multiple files on a Linux system?

In Linux we can use two different approaches to zip multiple files.

The first one is by using the zip command but, if it is not found on the system we can install it easily by using apt-get (Debian/Ubuntu) or yum (CentOS/Fedora):

$ sudo apt-get install zip
$ sudo yum install zip

Using the zip command we can zip multiple files by simply appending all the filenames that we need to include in the resulting archive:

$ zip file1 file2 file3

adding: file1 (stored 0%)
adding: file2 (stored 0%)
adding: file3 (stored 0%)

If we have multiple files with the same file extensions (same file type) we can also use a wildcard to group them and pass them to the zip command to create the archive:

$ zip *.txt
adding: file1.txt (stored 0%)
adding: dummy.txt (stored 0%)
adding: file2.txt (stored 0%)

Another very useful activity, involving the zip command, is to zip multiple directories; the following example shows how to do it:

$ zip directory1 directory2
adding: directory1/ (stored 0%)
adding: directory2/ (stored 0%)

The following options can be used with the zip command to better manage multiple files included in a zip archive:

-d Option: Removes the file from the zip archive. After creating a zip file, you can remove a file from the archive using the -d option.

$zip –d file.txt

-u Option: Updates the file in the zip archive. This option can be used to update the specified list of files or add new files to the existing zip file. Update an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.

$zip –u file.txt

-m Option: Deletes the original files after zipping. Move the specified files into the zip archive actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous removing all input files.

$zip –m file.txt

-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

$zip –r directory_name

-x Option: Exclude the files in creating the zip. Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

$zip –x file_to_be_excluded

The default compression method of Zip is deflate. If the zip utility determines that a file cannot be compressed, it simply stores the file in the archive without compressing it using the store method.

zip supports bzip2 compression if the bzip2 library is included when zip is compiled but we need to specify it using the -Z option:

zip -r -Z bzip2 directory_name

The zip command allows you to specify a compression level using a number prefixed with a dash from 0 to 9. The default compression level is -6. When using -0, all files will be stored without compression. -9 will force the zip command to use an optimal compression for all files.

For example, to use the compression level -9, you would type something like this:

zip -9 -r directory

The higher the compression level, the more CPU-intensive the zip process is, and it will take more time to complete.


The second approach that we can use is by using 7zip on Linux systems, usually provided by the p7zip, p7zip-full and p7zip-rar for Debian/Ubuntu systems and by p7zip and p7zip-plugins for RHEL/CentOS/Fedora systems:

$ sudo apt-get install p7zip-full (Debian/Ubuntu)
$ sudo yum install p7zip p7zip-plugins (CentOS/RHEL/Fedora)

A quick overview about the available commands for the 7z utility are below (the additional switches are not shown here, but can be seen on the official man pages:

a: Add files to archive
b: Benchmark
d: Delete files from archive
e: Extract files from archive (without using directory names)
h: Calculate hash values for files
i: Show information about supported formats
l: List contents of archive
rn: Rename files in archive
t: Test integrity of archive
u: Update files to archive
x: eXtract files with full paths

To add multiple files we can use the following approach (creating a simple zip file):

7z a -tzip file1.txt file2.txt file3.txt

Where a adds files to archive and -tzip specifies the type of archive (we are creating a zip archive).

Another way to include files, on different locations, to an archive is by using a list files (files containing lists of files). The filenames in such list file must be separated by new line symbol(s). For list files, 7-Zip uses UTF-8 encoding by default. You can change encoding using -scs switch. Multiple list files are supported.

For example, if the file "listfile.txt" contains the following:


Then the command:

7z a -tzip @listfile.txt

The above command will add to the archive "" all "*.conf" files from directories "configurations" and "source", but also a the specific file info.txt from another directory.

After adding files we would like to check if actually all the files were successfully added to the new archive: we can use "l" (list) function, which will displays the type of archive format, method used, files in the archive and other info:

7z l

Talking about compression and available compression methods you can define them also, obviously. There are a total of 5 compression level and are shown below:

-mx0: Don't compress at all. This is called copy mode.
-mx1: Low compression. This is called fastest mode.
-mx3: Fast compression mode. Will automatically set various parameters.
-mx5: Same as above, but normal"
-mx7: This means "maximum" compression.
-mx9: This means "ultra" compression. The most common used.

Note: to improve the compression time, usually, is better to use also the mt=on at the command line, that specifies the multithread mode (for multi-processor/multi-core systems).

The compression methods available are, mostly used only on 7z archives, are LZMA, LZMA2, PPMd, BZip2, Deflate, Delta, BCJ, BCJ2, Copy. Each compression level will adopt, by default, a specific compression method, but it can be also specified by using the -m0 switch, for example -m0=lzma to specify that the archive will be compressed using the LZMA algorithm (if supported by the archive type chosen.

Obviously the 7z utility has a lot more switches that can be user for archives to use more complex compressing algorithms and create different types of archives.