How can I create and extract zip archives from the command line?
Typically one uses
tar to create an uncompressed archive and either
bzip2 to compress that archive. The corresponding
bunzip2 commands can be used to uncompress said archive, or you can just use flags on the
tar command to perform the uncompression.
If you are referring specifically to the Zip file format, you can simply use the
zip squash.zip file1 file2 file3
or to zip a directory
zip -r squash.zip dir1
this unzips it in your current working directory.
There are a truly vast number of different ways to compress and uncompress under UNIX derivatives so I'm going to assume you meant "zip" in the generic sense rather than a specific file format.
You can zip files up (in compressed format) with the GNU
tar -zcvf myfile.tgz .
which will do the current directory. Replace
. with other file names if you want something else.
To unzip that file, use:
tar -zxvf myfile.tgz
That's assuming of course that you have a
tar capable of doing the compression as well as combining of files into one.
If not, you can just use
tar cvf followed by
gzip (again, if available) for compression and
gunzip followed by
For specific handling of ZIP format files, I would recommend downloading
7zip and using that - it recognises a huge variety of file formats, including the ZIP one.
Well, when it comes to distributing files for a variety of operating systems, I'd recommend 7-zip.
Usually in the package
p7zip, you'll get the
7za command, with which you can create your own 7z archives.
7za can also decompress standard (pkzip) zip archives (and create them as well with the
7za a archive.7z file1 file2 directory/
7za x archive.7z
It can also create self-extracting archives with the
7za a -sfx archive.exe files1 file2 dir
I recommend this method in case Windows users can't open 7z archives (in case you want to advice a tool for that: PeaZip).
If you want to use the same compression algorithm with your tarballs, use the
-J switch with
tar cJf archive.tar.xz file1 file2 dir
xz is a UNIX tool, that uses LZMA2 for compression, but works the way
bz2, etc works. It even works as a filter.
7z doesn't create archives with full filesystem information on UNIX, so you'd need to use
tar before using 7z (but since 7z stores other information about the
tar file, I'd recommend using
xz, as it is designed for it):
tar cf - file1 file2 dir | 7za z -si archive.tar.7z
This covers only extraction.
For that, I've started to use
The whole point of it is to remove complexity. So you just pass in any archive to it and it figures out what needs to be done with it. Zip, tar, gz... Use is always the same —
dtrx archive.zip. It asks a few questions and you're done.
dtrx is available in most repositories, so you can
yum install it.
I often find myself trying to zip the modified files in my work directory -- either to copy all the modified files to another sandbox work directory or to preserve them for a short while because I want to undo some of the changes.
In that case, I copy the list of modified files to a temporary file and use
zip with the option
Let's say I modified the files Foo.h, Bar.cc, and TestBar.cc.
Let's say I create a file called modified-files.txt whose contents are:
Foo.h Bar.cc TestBar.cc
I execute the following command to zip those files:
cat modified-files.txt | zip modified-files.zip -@