zip command examples

zip command examples

zip – package and compress (archive) files

The simplest example:

zip stuff *

creates the archive stuff.zip (assuming it does not exist) and puts all the files in the current directory in it, in compressed form (the .zip suffix is added automatically, unless the archive name contains a dot already; this allows the explicit specification of other suffixes).

Because of the way the shell on Unix does filename substitution, files starting with “.” are not included; to include these as well:

zip stuff .* *

Even this will not include any subdirectories from the current directory.

To zip up an entire directory, the command:

zip -r foo foo

creates the archive foo.zip, containing all the files and directories in the directory foo that is contained within the current directory.

You may want to make a zip archive that contains the files in foo, without recording the directory name, foo. You can use the -j option to leave off the paths, as in:

zip -j foo foo/*

If you are short on disk space, you might not have enough room to hold both the original directory and the corresponding compressed zip archive. In this case, you can create the archive in steps using the -m option. If foo contains theĀ  subdirectories tom, dick, and harry, you can:

zip -rm foo foo/tom
zip -rm foo foo/jerry
zip -rm foo foo/harry

where the first command creates foo.zip, and the next two add to it. At the completion of each zip command, the last created archive is deleted, making room for the next zip command to function.

Use -s to set the split size and create a split archive. The size is given as a number followed optionally by one of k (kB), m (MB), g (GB), or t (TB). The command

zip -s 2g -r split.zip foo

creates a split archive of the directory foo with splits no bigger than 2 GB each. If foo contained 5 GB of contents and the contents were stored in the split archive without compression (to make this example simple), this would create three splits, split.z01 at 2 GB, split.z02 at 2 GB, and split.zip at a little over 1 GB.

The -sp option can be used to pause zip between splits to allow changing removable media, for example, but read the descriptions and warnings for both -s and -sp below.

Though zip does not update split archives, zip provides the new option -O (–output-file) to allow split archives to be updated and saved in a new archive. For example,

zip inarchive.zip foo.c bar.c --out outarchive.zip

reads archive inarchive.zip, even if split, adds the files foo.c and bar.c, and writes the resulting archive to outarchive.zip. If inarchive.zip is split then outarchive.zip defaults to the same split size. Be aware that outarchive.zip and any split files that are created with it are always overwritten without warning. This may be changed in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *