Make a zip archive from the command line

  • October 7, 2007
  • Avatar for peter
    Peter
    Upfold

There's only really one universally interchangeable archiving and compression format that works across all major platforms, and that's the Zip format. Like it or not, that's what you are stuck with using if you need to exchange files with other operating systems.

Creating a Zip archive using a GUI is usually a very simple process which involves right-clicking a group of files or a folder and choosing the relevant option.

From a command line, it's a little more complex, but it's still very achievable and it is always useful to know how to do it from the command line.

Unzipping is easy, simply get yourself a suitable Zip file, and do the following:

$ unzip archive.zip

You will see some messages about deflating the files and then you should end up with the archive decompressed to your current working directory.

Compressing is slightly more complex and works something like this:

$ zip -b . stuff *

Let's take a look at the anatomy of that command in more detail. First of all, you run zip. Pretty simple, right?

The -b . switch indicates the directory in which you want the Zip archive file to be created. In this example, I've said . (i.e. the current working directory), but you can specify any directory which will be where your .zip file ends up.

stuff in this example is the name of the archive. Your filename will be this, with .zip added to the end.

Finally, * indicates that I want to zip all the files that are in this directory. That is probably the easiest way to work as you can just set up a directory containing your files, cd to it and use the * wildcard.

Of course, it is possible to be more advanced, so if you want to take your command line zip-fu a little further, then go ahead and read the man page.

Just remember - Zip files don't support Unix permissions, ownership or anything special like that. If you need to preserve that sort of information, you'll need to use tar instead!

Avatar for peter Peter Upfold

Home » Articles »