Linux zip Command Tutorial for Beginners (5 Examples)

As the name suggests, the zip command lets you create archives. In this tutorial, we will discuss the basics of zip using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux zip command

The zip command line Linux lets you package and compress (archive) files. Following is its syntax:

zip [OPTIONS] archive_name list_of_files

And here's how the tool's man page explains it:

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. 

It is analogous to a combination of the Unix commands tar(1) and compress(1) and is  compatible 
with  PKZIP  (Phil Katz's ZIP for MSDOS systems).

Following are some Q&A-styled examples that should give you a better idea on how the zip command works.

Q1. How to use the zip command?

Basic usage is fairly easy - just provide the zip file name and files to be compressed as input. Here's one example:

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

So the idea behind this command is to compress the three .txt files into a .zip file. Here's the output this command produced:

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

And a file named 'files.zip' was produced in the current working directory.

Q2. How to delete a file from an archive (compressed) file?

This can be done using the -d command line option. For example, to remove file3.txt from the files.zip file, I executed the zip command in the following way:

zip -d files.zip file3.txt

The tool notified me of the delete operation through the following message:

deleting: file3.txt

Q3. How to add new files to existing compressed (archive) file?

Just like the deletion operation is carried out through -d, in case you want to add new files to an existing compressed file, you can do that using the -u command line option.

Here's an example:

zip -u files.zip file3.txt file4.txt

Here are the messages this command produced in output:

adding: file3.txt (stored 0%)
adding: file4.txt (stored 0%)

Q4. How to make zip delete original files after archiving?

By default, the original files don't get deleted even after zip creates a compressed file. However, if you want, you can force the tool to delete original files. This can be done using the -m command line option.

Here's an example:

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

This command created files.zip in output while deleting all the .txt files in the process.

Q5. What are some other useful zip command options?

One command line option you'll likely require is -x. It is used when you want to leave some files from being compressed. So what you have to do is, you have to specify -x followed by name of the compressed file followed by the names of files you want to exclude.

For example:

zip files.zip file2.txt

This command will compress all files present in the current working directory, except file2.txt.

Moving on, the other command line option you may require is -r. It lets you recursively compress, something which is required when you want to compress directories (including of course their contents).

Conclusion

We've just scratched the surface here as the zip command offers a lot of other features as well. Once you are done practicing the examples we've discussed here, you can head to the tool's man page to learn more about it.

Share this page:

3 Comment(s)