Linux zip folder | 16 practical Linux zip command examples


Written by - Deepak Prasad

Linux Zip Command Examples

In this tutorial I will share various Unix and Linux zip command examples and scenarios to understand how to zip a folder and how to create a zip file.

zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows 9x/NT/XP, Minix, Atari, Macintosh, Amiga, and Acorn RISC OS.

 

Pre-requisite:

Before we zip a folder and create a zip file in Linux or Unix, we must install the zip and unzip utility.

NOTE:
On RHEL system you must have an active subscription to RHN or you can configure a local offline repository using which "yum" package manager can install the provided rpm and it's dependencies.

Install zip

In some Linux and Unix variants, zip is installed by default with operating system. You can also manually install zip utility by using yum

# yum install zip

Install unzip

Similarly to be able to unzip files and folders in Linux or Unix you will need unzip utility

# yum install zip

 

1. How to zip a folder with multiple files Linux

In this Linux zip command example you will learn, how to zip a folder with multiple files or all files from the current working directory and store the archive at the same location. List the files which you wish to zip using ls command

# ls -lh
total 332K
-r--r--r--. 1 root root 129K Oct 19 00:14 bc-1.07.1-5.el8.x86_64.rpm
-r--r--r--. 1 root root  45K Oct 19 00:14 biosdevname-0.7.3-2.el8.x86_64.rpm
-r--r--r--. 1 root root 150K Oct 19 00:14 blktrace-1.2.0-9.el8.x86_64.rpm

Let's take this Linux zip command example. Here we will zip multiple files or rather all the files from current working directory.

# zip rpms.zip *
  adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

Verify the newly created zip file (rpms.zip)

# ls -lh rpms.zip
total 620K
-rw-r--r--. 1 root root 287K Oct 19 00:18 rpms.zip

Now in the last zip command example we archived all the files present inside /tmp/files directory. Now I wish to zip all files except one or some selected files. In this case you must provided the name of the files which you wish to archive so zip will only consider those files for compression and archive.

# zip rpms.zip bc-1.07.1-5.el8.x86_64.rpm biosdevname-0.7.3-2.el8.x86_64.rpm
  adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)

So now our archive only contains two files which we provided as input and the archive was stored in the same path.

 

2. How to create a zip file by excluding some files

Use -x or --exclude to zip all files except some files to exclude files from being compressed and archived in Linux or Unix

Below Linux zip command example will show how to create a zip file with all files except some files.

# zip /tmp/test_archive.zip * -x bc-1.07.1-5.el8.x86_64.rpm
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

To create zip a folder with all files except some files using a regex, here all file names starting with bc will be excluded

# zip /tmp/test_archive.zip * -x bc*.rpm
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

To create a zip folder by excluding multiple files

# zip /tmp/test_archive.zip * -x bc*.rpm biosdevname-0.7.3-2.el8.x86_64.rpm
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

Similarly you can also zip all folders and exclude some directories using -r to zip directories

 

3. How to zip a folder by compressing directory and sub-directories

By default zip command will only compress files. if you attempt to compress directory, then you may get below output

# zip /tmp/test_archive.zip  /tmp/files
  adding: tmp/files/ (stored 0%)

As you see the stored content was 0%. So how to zip a folder or directory?

Use -r or --recurse-paths to zip a folder in Linux or Unix and compress directory structure recursively using Linux zip command example:

# zip /tmp/test_archive.zip -r /tmp/files
updating: tmp/files/ (stored 0%)
  adding: tmp/files/blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)
  adding: tmp/files/biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: tmp/files/bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)

 

4. How to display zip archive content

We can use -dc or --display-counts to display running zip file count of entries. Check below Linux zip command example to list zip archive content without extracting.

# zip -dc rpms.zip *
  0/  3   adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  1/  2   adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  2/  1   adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

# ls -lh rpms.zip
-rw-r--r--. 1 root root 287K Nov  2 12:41 rpms.zip

 

5. How to zip a folder with password protection

You can zip a folder and also password protect the zip file using -e or --encrypt. With this argument you create a zip file with an encrypted password. Let us use this Linux zip command example to understand this better:

# zip -e rpms.zip *
Enter password:
Verify password:
  adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

Now how to unzip files? If you try to unzip to directory, the zip file will prompt for password. Without password you will not be able to unzip to directory.

# unzip rpms.zip
Archive:  rpms.zip
[rpms.zip] bc-1.07.1-5.el8.x86_64.rpm password:
  inflating: bc-1.07.1-5.el8.x86_64.rpm
  inflating: biosdevname-0.7.3-2.el8.x86_64.rpm
  inflating: blktrace-1.2.0-9.el8.x86_64.rpm

 

6. How to update or modify existing file in zip archive

You can use -f or --freshen to update or modify zip file content. With -f you replace (freshen) an existing entry in the zip archive only if it has been modified more recently than the version already in the zip file, unlike the update option (-u) this will not add files that are not already in the zip archive.

You can modify zip file content here.

# touch test_file.txt

# echo "First Line" >> test_file.txt

# cat test_file.txt
First Line

# zip /tmp/test_archive.zip test_file.txt
  adding: test_file.txt (stored 0%)

Now add some more content to the existing file

# echo "Second Line" >> test_file.txt

# zip /tmp/test_archive.zip -f test_file.txt
freshening: test_file.txt (deflated 13%)
IMPORTANT NOTE:
This command should be run from the same directory from which the original zip command was run to update or modify zip file content, since paths stored in zip archives are always relative.

 

7. How to add files to existing zip archive

How to add files to the existing zip archive, instead of creating a new zip archive. If this operation fails, zip attempts to restore the archive to its original state. If the restoration fails, the archive might become corrupted. This option is ignored when there's no existing archive or when at least one zip archive member must be updated or deleted.

In my earlier Linux zip command example I had created a zip archive /tmp/test_archive.zip. Here we will will add new files to the same zip archive

# zip /tmp/test_archive.zip -g *.rpm
  adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)

 

8. How to list zip archive contents without extracting

You can also list zip archive contents without unzip to directory using -sf. For instance you have a requirement to check if a file exists in the archive so without unzip to directory you can list the zip file contents on the output and use additional regex and tools to grep the file you are looking for.

# zip -sf /tmp/test_archive.zip
Archive contains:
  test_file.txt
  bc-1.07.1-5.el8.x86_64.rpm
  biosdevname-0.7.3-2.el8.x86_64.rpm
  blktrace-1.2.0-9.el8.x86_64.rpm
Total 4 entries (330459 bytes)

 

9. How to create a zip file without complete path (Store only the file name)

By default when we create a zip archive, it stores the relative path for the archived content. Lets take this Linux zip command example which stores the relative path of files:

# zip /tmp/test_archive.zip /tmp/files/*
  adding: tmp/files/bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: tmp/files/biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: tmp/files/blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)
  adding: tmp/files/test_file.txt (deflated 13%)

Here list the content of test_archive, as you see the relative path is stored along with the filename

# zip -sf /tmp/test_archive.zip
Archive contains:
  tmp/files/bc-1.07.1-5.el8.x86_64.rpm
  tmp/files/biosdevname-0.7.3-2.el8.x86_64.rpm
  tmp/files/blktrace-1.2.0-9.el8.x86_64.rpm
  tmp/files/test_file.txt
Total 4 entries (330459 bytes)

So to create a zip file in Linux without complete path and only store the filename in zip archive, we use -j or --junk-paths. This will create a zip archive without relative path of the files and store just the name of file (junk the path), and do not store directory names. For Example:

# zip /tmp/test_archive.zip -j /tmp/files/*
  adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)
  adding: test_file.txt (deflated 13%)

List and verify the zip archive content.

# zip -sf /tmp/test_archive.zip
Archive contains:
  bc-1.07.1-5.el8.x86_64.rpm
  biosdevname-0.7.3-2.el8.x86_64.rpm
  blktrace-1.2.0-9.el8.x86_64.rpm
  test_file.txt
Total 4 entries (330459 bytes)

 

10. How to create a zip file and delete original files after archiving

  • By default when you create a zip file, it leaves the original files and folders after creating archive,
  • But you can also make zip delete original files after archiving is complete.
  • Instead of copy, Move the specified files into the zip archive
  • Actually, this deletes the original 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 so it is recommended to use it in combination with -T to test the archive before you make zip delete original files after archiving. In this we create a zip archive, and then delete the original files are we zip the folder.

# zip /tmp/test_archive.zip -mT /tmp/files/*
  adding: tmp/files/bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: tmp/files/biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: tmp/files/blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)
  adding: tmp/files/test_file.txt (deflated 13%)
test of /tmp/test_archive.zip OK

After you zip a folder, verify the content stored under /tmp/files. So as expected the original files are deleted from the source location after archiving.

# ls -l /tmp/files/
total 0

Similarly to store the entire content of /tmp/files including the source directory use -r directive

# zip /tmp/test_archive.zip -rmT /tmp/files
updating: tmp/files/bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
updating: tmp/files/biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
updating: tmp/files/blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)
updating: tmp/files/ (stored 0%)
test of /tmp/test_archive.zip OK

As expected our source directory is also deleted after archiving

# ls -l /tmp/files
ls: cannot access '/tmp/files': No such file or directory

 

11. How to zip a folder with higher compression level

When we zip a folder, the default zip compression level is -6. zip command supports compression level from [0-9] where 0 means no compression and -9 means highest (maximum) compression. We will perform basic compression and monitor the time difference

IMPORTANT NOTE:
The Compression Speed depends on various factor including but not limited to system CPU type, available processor and memory, disk read write speed, free disk IO available, number and size of individual files to be archived. I am using a RHEL 8 virtual machine with 2GB RAM and single core processor (virtual).

With the default compression level, when we create a zip folder, the compressing of 1668 rpms the zip tool took ~32 min.

# time zip -r /tmp/files/archive.zip  BaseOS/*

real    0m32.177s
user    0m28.786s
sys     0m1.715s

The original size of the source location was

# du -sch /mnt/BaseOS
907M    /mnt/BaseOS
907M    total

which after compression is 842M so we saved ~65M

# du -sch /tmp/files/archive.zip
842M    /tmp/files/archive.zip
842M    total

We increase the compression level to -9 and create a zip folder which is considered as the highest or maximum compression level.

# time zip -q -r -9 /tmp/files/archive.zip  BaseOS/*

real    0m35.621s
user    0m32.265s
sys     0m1.081s

Now with higher level of compression, zip took additional 3 seconds to complete the compression but we only save 1MB overall in our archive so the advantage was not very high compared to the time we invested.

# du -sch /tmp/files/archive.zip
841M    /tmp/files/archive.zip
841M    total

 

12. How to copy files between two zip archives

You can also copy files from one zip archive to another using -U or --copy-entries argument without extracting.

Also requires the --out option to specify a different output file than the input archive.

Here in this Linux zip command example we copy files between two zip archives, I have an input archive with below content

# zip -sf /tmp/input_archive.zip
Archive contains:
  blktrace-1.2.0-9.el8.x86_64.rpm
  bc-1.07.1-5.el8.x86_64.rpm
  biosdevname-0.7.3-2.el8.x86_64.rpm
Total 3 entries (330436 bytes)

I will copy one of the rpm to another new archive

# zip  /tmp/input_archive.zip "bc-1.07.1-5.el8.x86_64.rpm" --copy  --out /tmp/output_archive.zip
 copying: bc-1.07.1-5.el8.x86_64.rpm

Verify the content of the output_archive

# zip -sf /tmp/output_archive.zip
Archive contains:
  bc-1.07.1-5.el8.x86_64.rpm
Total 1 entries (132040 bytes)

If no input files appear on the command line and --out is used, copy mode is assumed:

# zip /tmp/input_archive.zip --out /tmp/output_archive.zip
 copying: blktrace-1.2.0-9.el8.x86_64.rpm
 copying: bc-1.07.1-5.el8.x86_64.rpm
 copying: biosdevname-0.7.3-2.el8.x86_64.rpm

 

13. Linux zip directory and remove files from zip archive

In Linux zip directory and using -d or --delete directive you can delete or remove files from zip archive.

Let us take this Linux zip command example. Here I have a Linux zip archive with below content

# zip -sf /tmp/test_archive.zip
Archive contains:
  blktrace-1.2.0-9.el8.x86_64.rpm
  bc-1.07.1-5.el8.x86_64.rpm
  biosdevname-0.7.3-2.el8.x86_64.rpm
Total 3 entries (330436 bytes)

So to remove files from zip archive , I will remove two files from the existing zip archive without unzip to directory

# zip -d /tmp/test_archive.zip  bc-1.07.1-5.el8.x86_64.rpm blktrace-1.2.0-9.el8.x86_64.rpm
deleting: blktrace-1.2.0-9.el8.x86_64.rpm
deleting: bc-1.07.1-5.el8.x86_64.rpm

Verify the content of the archive

# zip -sf /tmp/test_archive.zip
Archive contains:
  biosdevname-0.7.3-2.el8.x86_64.rpm
Total 1 entries (45268 bytes)

 

14. Linux zip folder and preserve symbolic links

For UNIX and VMS (V8.3 and later) use -y or --symlinks, create a zip folder and store and preserve symbolic links in zip archive.

This can avoid multiple copies of files being included in the archive as zip re curses the directory trees and accesses files directly and by links.

Let's take this Linux zip command example. Here I have created a dummy file inside my root's home folder and added some content.

# echo "This is a test file" >> /root/test_file

Create a symlink to this dummy file

# ln -s /root/test_file test_link

Verify the symlink

# ls -l
total 332
-r--r--r--. 1 root root 132040 Nov  2 13:50 bc-1.07.1-5.el8.x86_64.rpm
-r--r--r--. 1 root root  45268 Nov  2 13:50 biosdevname-0.7.3-2.el8.x86_64.rpm
-r--r--r--. 1 root root 153128 Nov  2 13:50 blktrace-1.2.0-9.el8.x86_64.rpm
lrwxrwxrwx. 1 root root     15 Nov  2 14:33 test_link -> /root/test_file

Next create a zip folder including symlink using -y or --symlinks argument to make zip preserve symbolic links

# zip /tmp/test_archive.zip  -y *
  adding: bc-1.07.1-5.el8.x86_64.rpm (deflated 10%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (deflated 24%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (deflated 9%)
  adding: test_link (stored 0%)

I will create a dummy location to extract the archive and verify the symlink

# cd /tmp/test

# mv /tmp/test_archive.zip .

 

Unzip to directory using unzip command

Unzip to directory the newly created archive using unzip to make sure zip was able to preserve and store symbolic links

# unzip test_archive.zip
Archive:  test_archive.zip
  inflating: bc-1.07.1-5.el8.x86_64.rpm
  inflating: biosdevname-0.7.3-2.el8.x86_64.rpm
  inflating: blktrace-1.2.0-9.el8.x86_64.rpm
    linking: test_link               -> /root/test_file
finishing deferred symbolic links:
  test_link              -> /root/test_file

So with -y we make zip preserve symbolic links

# ls -l
total 620
-r--r--r--. 1 root root 132040 Nov  2 13:50 bc-1.07.1-5.el8.x86_64.rpm
-r--r--r--. 1 root root  45268 Nov  2 13:50 biosdevname-0.7.3-2.el8.x86_64.rpm
-r--r--r--. 1 root root 153128 Nov  2 13:50 blktrace-1.2.0-9.el8.x86_64.rpm
-rw-r--r--. 1 root root 293196 Nov  2 14:34 test_archive.zip
lrwxrwxrwx. 1 root root     15 Nov  2 14:36 test_link -> /root/test_file

Next verify the symlink content.

# cat test_link
This is a test file

 

15. How to create a zip file and compress with bzip2

You can create a zip file with bzip2 compression. Set the default compression method using -Z or --compression-method. Currently the main methods supported by zip are store and deflate. Compression method can be set to:

  • store - Setting the compression method to store forces zip to store entries with no compression. This is generally faster than compressing entries, but results in no space savings. This is the same as using -0 (compression level zero).
  • deflate - This is the default method for zip. If zip determines that storing is better than deflation, the entry will be stored instead.
  • bzip2 - If bzip2 support is compiled in, this compression method also becomes available.
IMPORTANT NOTE:
Only some modern unzip utility in Linux and Unix currently support the bzip2 compression method, so test the unzip you will be using before relying on archives using this method.
# zip -Z bzip2 /tmp/test_archive.zip *
  adding: bc-1.07.1-5.el8.x86_64.rpm (bzipped 9%)
  adding: biosdevname-0.7.3-2.el8.x86_64.rpm (bzipped 22%)
  adding: blktrace-1.2.0-9.el8.x86_64.rpm (bzipped 7%)

 

Extract content using unzip command

Extract the content using unzip to directory

# unzip test_archive.zip
Archive:  test_archive.zip
 bunzipping: bc-1.07.1-5.el8.x86_64.rpm
 bunzipping: biosdevname-0.7.3-2.el8.x86_64.rpm
 bunzipping: blktrace-1.2.0-9.el8.x86_64.rpm

 

16. How to zip a folder and later split zip archive

  • How to zip a folder and enable creating a split zip archive and set the split size using -s or --split-size.
  • A split zip archive in Linux and Unix is an archive that could be split over many files.
  • As the zip archive is created, if the size of the archive reaches the specified split size, that split is closed and the next split opened.
  • In general all splits but the last will be the split size and the last will be whatever is left.
  • If the entire archive is smaller than the split size a single-file archive is created.
  • Split size is a number optionally followed by a multiplier.
  • Currently the number must be an integer.
  • The multiplier can currently be one of k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes).
  • As 64k is the minimum split size, numbers without multipliers default to megabytes

Let's use this Linux zip command example to create a zip folder with split enable. I have Packages directory with files having size of 902MB. Here I will create zip archive

# zip -q -s 200m /tmp/test_archive *

The last file as you see is of 36MB while all the other archive is of 200MB which we had used as the split size

# ls -lh /tmp/test_archive.z*
-rw-r--r--. 1 root root 200M Nov  2 15:03 /tmp/test_archive.z01
-rw-r--r--. 1 root root 200M Nov  2 15:03 /tmp/test_archive.z02
-rw-r--r--. 1 root root 200M Nov  2 15:03 /tmp/test_archive.z03
-rw-r--r--. 1 root root 200M Nov  2 15:04 /tmp/test_archive.z04
-rw-r--r--. 1 root root  36M Nov  2 15:04 /tmp/test_archive.zip

 

Lastly I hope the steps from the article to compress and archive using zip command and cover scenarios life zip multiple files and folders, zip all files except some files and folders, compress directory and preserve symbolic links, remove files from zip archive, zip delete original files in Linux and Unix was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:

Zip Man Page

 

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

X