674

How can I create and extract zip archives from the command line?

5
  • 3
    @joachim, you should ask the question in a question and provide an answer in an answer. There's no problem with answering your own questions but not as part of the question itself, since that breaks down the whole community-votes-on-the-best-answer advantage of SO. In addition, this is probably better suited for superuser since it's not programming related.
    – paxdiablo
    Jan 28, 2011 at 16:02
  • 2
    Where will you document how to get online and check SO if you forget your login?
    – Doug Chamberlain
    Jan 28, 2011 at 16:05
  • @rlb.usa: I work in a lot of different places on a lot of different servers. Using SA for a note like this helps other people too, as I see it.
    – joachim
    Jan 28, 2011 at 16:51
  • For quick ziping file, I usually use gzip *
    – igonejack
    Apr 27, 2021 at 2:42
  • python -m zipfile --help if you don't want to install zip/unzip and have python. Nov 8, 2021 at 13:15

9 Answers 9

812

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.

6
  • 89
    I personally only ever use the zip -r9 archive.zip files... syntax (-r=recursive -9=max compression). Jan 31, 2011 at 12:29
  • 3
    Just 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.
    – polemon
    May 4, 2014 at 14:34
  • 8
    In case you don't want the parent directory to be included: cd dir1; zip -r ../squash.zip *
    – Marinos An
    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
  • 20
    For 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
79

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.

5
  • Or use tar jcvf file.tar.bz2... to compress in bzip2 format or tar Jcvf file.tar.xz ... for xz compression.
    – OldTimer
    Mar 11, 2016 at 0:04
  • 14
    I downvoted your answer because the question is about how to deal with zip archives not tar files.
    – Starfish
    Dec 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.
    – user14408
    Dec 18, 2016 at 7:52
  • 2
    Be 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
  • 1
    I suppose technically this is gzip, so that's fine. I guess. Pretty obviously not what the question asked for though.
    – Owl
    Nov 29, 2018 at 10:01
41

If you don't have zip and unzip packages installed and you have java, you can use jar to unzip:

jar -xf file.zip
3
  • 7
    Not my server, no unzip, zip, 7z, nothing... And this worked. Thanks!
    – Dariusz
    May 31, 2016 at 8:12
  • 5
    This is a great tip.
    – Starfish
    Dec 18, 2016 at 3:50
  • 1
    We need jdk in advance to execute this command. Jan 22, 2021 at 14:47
29

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
1
  • 2
    I should perhaps append this answer with atool, it's a nice little tool that makes using many different compressors more comfortable. Bind that to a and creating an archive becomes a arc.7z folder/ or extracting a simple a arc.7z
    – polemon
    May 2, 2014 at 23:17
28
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.

1
  • 4
    I 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
    – Kyle
    Dec 14, 2016 at 22:23
5

BSD Tar can also do this

bsdtar -a -c -f sunday.zip sunday.jpg

Or:

bsdtar -x -f sunday.zip
1
  • 1
    BSD tar has the benefit of supporting quite a few additional compression formats compared to GNU tar and other tars. The greatest benefit is, perhaps, that it does so while using a tar-like syntax. You can surely use 7z, unrar, unzip, etc from the commandline, but they use a different syntax.
    – njsg
    Aug 31, 2017 at 12:09
3

The most standard answer is pax, which is recommended over cpio and tar.

Unlike cpio and POSIX tar, but like GNU tar, pax is able to both archive files and compress the archive. This behavior is different from zip, which compresses each file before putting it in the archive.

2
  • 1
    Does pax support zip archives?
    – Starfish
    Dec 18, 2016 at 3:50
  • Nope and it doesn't support pax archives either!
    – roblogic
    Feb 1, 2018 at 4:45
2

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.

1

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 -@

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .