How can I create and extract zip archives from the command line?
9 Answers
Typically one uses tar
to create an uncompressed archive and either gzip
or bzip2
to compress that archive. The corresponding gunzip
and 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
and unzip
commands.
To compress:
zip squash.zip file1 file2 file3
or to zip a directory
zip -r squash.zip dir1
To uncompress:
unzip squash.zip
this unzips it in your current working directory.
-
89I personally only ever use the
zip -r9 archive.zip files...
syntax (-r=recursive -9=max compression). Jan 31, 2011 at 12:29 -
3Just as a side note:
zip
can create many different flavors of .ZIP archives, as well as use many different compressors. On the other hand, there are a number of other compression tools named similarly: 7-Zip, gzip, bzip2, rzip, etc. which are not interchangeable.– polemonMay 4, 2014 at 14:34 -
8In case you don't want the parent directory to be included:
cd dir1; zip -r ../squash.zip *
Nov 30, 2016 at 18:11 -
I'm confused, I did this and get a long list of inflations and extractions but when I check the directory it is empty, save for the original zip file? Where is it all going? Jan 15, 2017 at 1:43
-
20For the record, use
unzip -d myfolder squash.zip
to extract into a separate new directory (myfolder
) instead of putting all the files directly into the current directory, which can be annoying if the zip file contains many files and directories in its top level. This mimics how most UI unzip tools work. Mar 8, 2017 at 5:01
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
program:
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 tar xvf
.
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.
-
Or use
tar jcvf file.tar.bz2...
to compress in bzip2 format ortar Jcvf file.tar.xz ...
for xz compression.– OldTimerMar 11, 2016 at 0:04 -
14I downvoted your answer because the question is about how to deal with zip archives not tar files.– StarfishDec 18, 2016 at 3:35
-
4@Starfish, that's certainly within your rights, thanks for at least explaining why, so many people don't take the time to do that :-) I took "zip" in the more generic sense (compression) rather than a specific format, so I'll make that more clear.– user14408Dec 18, 2016 at 7:52
-
2Be aware that tar archives may not work correctly when moving between different platforms, mac to linux or vice versa. Zip is much safer, as it works correctly on all systems. Jul 5, 2018 at 1:21
-
1I suppose technically this is gzip, so that's fine. I guess. Pretty obviously not what the question asked for though.– OwlNov 29, 2018 at 10:01
If you don't have zip and unzip packages installed and you have java, you can use jar
to unzip:
jar -xf file.zip
-
7Not my server, no unzip, zip, 7z, nothing... And this worked. Thanks!– DariuszMay 31, 2016 at 8:12
-
5
-
1
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 7z
and 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 -tzip
switch).
Compressing:
7za a archive.7z file1 file2 directory/
Decompressing:
7za x archive.7z
It can also create self-extracting archives with the -sfx
switch:
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
:
tar cJf archive.tar.xz file1 file2 dir
xz
is a UNIX tool, that uses LZMA2 for compression, but works the way gz
, 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
-
2I should perhaps append this answer with
atool
, it's a nice little tool that makes using many different compressors more comfortable. Bind that toa
and creating an archive becomesa arc.7z folder/
or extracting a simplea arc.7z
– polemonMay 2, 2014 at 23:17
unzip <filename>
and
zip -r archive.zip <filename> [filename]
You'll need to make sure these commands are installed via your package manager. It's no harder than using anything else on the command line. It's certainly simpler than creating archives with tar.
-
4I know this is an old thread but this is the best answer. Doesn't talk about 7z or tar, its concise and straight to the point– KyleDec 14, 2016 at 22:23
BSD Tar can also do this
bsdtar -a -c -f sunday.zip sunday.jpg
Or:
bsdtar -x -f sunday.zip
-
1BSD
tar
has the benefit of supporting quite a few additional compression formats compared to GNUtar
and othertar
s. The greatest benefit is, perhaps, that it does so while using atar
-like syntax. You can surely use7z
,unrar
,unzip
, etc from the commandline, but they use a different syntax.– njsgAug 31, 2017 at 12:09
This covers only extraction.
For that, I've started to use dtrx
.
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 apt
|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 -@
gzip *
python -m zipfile --help
if you don't want to install zip/unzip and have python.