Compress zip file linux

How to Zip a File in Linux with easy examples

In this tutorial, I will give you a complete guide and every important option listed on «How to Zip a File in Linux». Zipping a file or directory in Linux helps to take backup and save disk space by compressing unused files. I am using Ubuntu Linux 20.04 to showcase all examples in this post.

What is Zip

Zip is basically a file packaging and compression utility for almost every operating system, whether it’s any Linux flavour, Windows, Mac and even some RISC systems.

It is always installed on all Linux or Unix system by default except few cases. It is analogous to a combination of two commands tar and compress.

Generally, zip program puts one or more compress files in Linux into a single zip archive along with meta-information about the file.

The compression ratio of zip files in Linux is around 2:1 or 3:1 are common of text files and it varies for different file types. Single zip command can put an entire directory structure into a zip archive.

In case of large archives greater than 4GB, zip automatically uses zip64 extension. Zip is compatible with Pkzip and Pkunzip utilities as well.

Zip also now supports bzip2 compression utility, if the bzip2 library generally gets compiled with zip.

Unzip and tar are two companion utilities. Unzip unpacks or decompress zip archive and tar is used to take backup.

Pre-requisite of Zip utility

There is no special pre-requisite, the only thing you need to do is to check whether zip is installed in Linux or not.

Run this command. it will install zip and unzip utility if it’s not there. If it’s already installed, it will upgrade or do nothing as shown in the output.

$ sudo apt install zip unzip

It shows zip and unzip utility is already on the newest version. so you are done with installing zip and unzip till this point and you are ready to explore the world of Compress files and folder in Linux.

If you have Centos or Fedora distro. Run below-mentioned commands to install zip and unzip

Читайте также:  Linux mint зашифровать диск

I will explain zipping files in Linux using two options

  1. Zip a file in Linux using Graphical user interface (GUI)
  2. Zip a file in Linux using command line interface (CLI)

Let’s check out first option —

How to zip a file in Linux GUI

Go to the directory where file or folders resides.

Select files and right-click — select compress.

Give archive name. I will give sample-zip as an example.

How to zip a file in Linux terminal

First, have a look at Syntax of zip with explanation —

$ zip options "output_file_name" "target_file" "target_folder"

output_file_name — the name of the output file you want to give. zip extension will automatically add.

target_file or folder — is file or folder path which you want to zip and it may have wildcards.

Let’s see different options and examples of Zip program one by one.

Example 1:- How to compress or zip files in Linux

dev@test:~$ zip test file1 file2

output
adding: file1 (deflated 69%)
adding: file2 (deflated 69%)
dev@test:~$ ls test*
test.zip
dev@test:~$

File name and compression % is reflected in the output. As you can see I have not included .zip extension, still by default command add that into output-file name. In my case «test-zip.zip«.

if you want to suppress this output of zip command, you can use -q option.

dev@test:~$ zip -q test file1 file2

Output

dev@test:~$ ls -ltrh test*
-rw-r—r— 1 dev dev 56K Jun 18 16:30 test.zip
dev@test:~$

There may be the case, if you forgot to give output-file-name in zip command, then it will by-default pick the first file name as the output file and second file into zip. So below mentioned example also show, how to zip multiple files in Linux.

Output
adding: file2 (deflated 69%)
dev@test:~$ ls -l *.zip
-rw-r—r— 1 dev dev 28519 Jun 18 16:33 file1.zip

Example 2:- How to zip folder and subfolders in Linux

Generally, you need to create zip archive to the directory containing files and subdirectories. Zip option -r will be useful to traverse that directory structure recursively.

dev@test:~$ zip -r test sample/

Output
adding: sample/ (stored 0%)
adding: sample/file2 (deflated 69%)
adding: sample/file1 (deflated 69%)
dev@test:~$ ls sample*
test.zip

Compression levels in Zip utility

The default compression level of the zip file is -6 and you have all the freedom to choose a number between 0-9. if you select -0 then there will be no compression , means no compress files in Unix.

if you will select -9 will force zip program to select best optimal compression for all files or folders.

Example 3:- How to use compression level 0 & 9

dev@test:~$ zip -r -0 test.zip sample/

Output
adding: sample/ (stored 0%)
adding: sample/file2 (stored 0%)
adding: sample/file1 (stored 0%)

output shows, if I select 0 as compression level, there is no compression at all during zipping of the files or folder.

dev@test:~$ zip -r -9 test sample/

Output
adding: sample/ (stored 0%)
adding: sample/file2 (deflated 69%)
adding: sample/file1 (deflated 69%)
dev@test:~$

if Zip found that file can’t be compressed, it just archives and stores it as-is without compress file in Linux.

Читайте также:  Linking command in linux

I have talked about bzip2 also in my zip introduction statement. bzip2 is also installed by default in all Linux version. If you want to compress the file using bzip2 method, -Z can be used.

Example 4:- How to zip a file in Linux using bzip2 utility

dev@test:~$ zip -r -Z bzip2 test.zip sample/

Output
adding: sample/ (stored 0%)
adding: sample/file2 (bzipped 74%)
adding: sample/file1 (bzipped 74%)
dev@test:~$ ls test.zip
test.zip

If you will compare the output, compression ratio increased for the same files from 69% to 74% using with bzip2 .

So bzipping files may result in more space-saving but changing compression level or using bzip2 to get more space-saving comes with penality of CPU resources and it will take more time to compress in case of a large number of files in Linux.

But it will definitely save network resources if you are planning to transfer or send it over the internet.

Zip and encrypt a file in Linux

Many times you need to store or need to send/transfer a file to someone and it may contain sensitive or personal data.

To avoid misuse of this file, you can always encrypt or enable password on archive file using -e option.

Example 5:- How to enable password on the zip file in Linux

dev@test:~$ zip -e test.zip sample/

Output asking for password
Enter password:
Verify password:
adding: sample/ (stored 0%)
dev@test:~$ ls sample
sample/ test.zip

Don’t forget to share the password with the recipient , if you are going to send it to someone 🙂

Create a split zip file in Linux

There may be a case when you have a big file to archive and you need to split it due to storage or upload limits on FTP site or file hosting vendor.

You can create a zip file using -s option with providing size. The multiplier can be set as k(kilobytes), m(Megabytes), t(Terabytes).

Example 6:- How to create a split zip file in Linux

I have a zip file of Ubuntu 20.04 iso image with name ubuntu.zip (2.5G) and I will split it into 1g archive files.

dev@test:~/sample$ ls -ltrh ubuntu.zip -rw-r--r-- 1 dev dev 2.5G Jun 18 17:51 ubuntu.zip
dev@test:~/sample$ zip -s 1g ubuntu.zip --out split copying: ubuntu-20.04-desktop-amd64.iso dev@test:~/sample$

Output

dev@test:~/sample$ ls -ltrh split*
-rw-r—r— 1 dev dev 1.0G Jun 18 18:03 split.z02
-rw-r—r— 1 dev dev 504M Jun 18 18:04 split.zip
-rw-r—r— 1 dev dev 1.0G Jun 18 18:04 split.z01

Have a look on split.zip, split.z01 and split.z02

Now when you have split the file using zip and you will be thinking of joining this on target. Don’t worry, I won’t let you scratch your head.

Example 7:- How to add or combine a split zip file in Linux

Go to the directory where you have kept split files

dev@test:~/sample$ ls -ltrh split*

Output
-rw-r—r— 1 dev dev 1.0G Jun 18 18:03 split.z02
-rw-r—r— 1 dev dev 504M Jun 18 18:04 split.zip
-rw-r—r— 1 dev dev 1.0G Jun 18 18:04 split.z01

now Join split zip files.

dev@test:~/sample$ cat split.z* > join.zip

Validate the size in output

dev@test:~/sample$ ls -ltrh join.zip
-rw-r—r— 1 dev dev 2.5G Jun 18 18:13 join.zip

Example 8:- How to update a zip file in Linux

let’s assume you forgot to add one file into a zip or want to add more files to existing zip, then -u option can help. In this example, I will add file4 to existing test.zip file.

dev@test:~$ zip -u test.zip file4

Output
adding: file4 (deflated 69%)

Example 9:- How to exclude a file from a zip archive in Linux

A scenario where you have a lot of files in a directory and you want to exclude some files from being zip archived. Option -x is the saviour here.

Читайте также:  Linux перенести пользователя другую группу

Currently, I have three files in the sample directory

total 264K
-rw-r—r— 1 dev dev 88K Jun 17 21:51 file1
-rw-r—r— 1 dev dev 88K Jun 17 23:21 file2
-rw-r—r— 1 dev dev 88K Jun 18 18:20 file3

let’s exclude file3, only 2 files archived.

dev@test:~/sample$ zip test.zip * -x file3

Output
adding: file1 (deflated 69%)
adding: file2 (deflated 69%)
dev@test:~/sample$

Example 10:- Verbose mode in zipping files in Linux

if you want to see the progress, -v option known as verbose or diagnostic version info can be helpful especially when you are working with a large number of files.

dev@test:~$ zip -v -r test.zip sample/

Output
adding: sample/ (in=0) (out=0) (stored 0%)
adding: sample/file3 (in=90082) (out=28359) (deflated 69%)
adding: sample/file2 (in=90082) (out=28359) (deflated 69%)
adding: sample/file1 (in=90082) (out=28359) (deflated 69%)
total bytes=270246, compressed=85077 -> 69% savings
dev@test:~$

Example 11:- Working with wildcards to compress files in Linux

The mostly used wildcard is an asterisk(*) in this world, in my view, Zip utility supports this helpful wildcard character. No need to type each and every file name.

dev@test:~/sample$ zip file.zip file*

Output
adding: file1 (deflated 69%)
adding: file2 (deflated 69%)
adding: file3 (deflated 69%)
dev@test:~/sample$

Some common issues and error codes during zipping in Linux

0 normal; no errors or warnings detected, means archiving done

2 unexpected end of zip file in Unix.

11 read or seek error, Unable to read the file

12 zip has nothing to do, Some action is missing

13 missing or empty zip file

14 error writing to a file

Video tutorial

To make it easier on «how to zip a file in Linux» for beginners, I have created a video also. Please watch, like and share, if you found it useful.

Conclusion

We learnt all-important functionality of zip utility in this tutorial and hope provided examples are good enough to understand it.

if you still face any issue or want to explore more on zipping files in Linux or compress files in Unix, use «man zip» command or man page in Linux. Probably the best source of information.

Further reading recommendations:-

After this «How to zip a file in Linux» tutorial, Soon I will return with «unzip» post. Till that time, enjoy.

Please leave your comments or feedback. it will motivate to write more helpful posts.

About The Author

Dev

Hi, My Name is Dev and as an Information Technology professional, My Life revolves around Gadgets, Servers, Storage, Software, Operating system, Internet and Cloud computing. My philosophy is “being healthy” and follow your “passion or hobbies” to make your life interesting. Please feel free to contact me for any questions, feedback and suggestions.

Источник

Оцените статью
Adblock
detector