157

I'd like to compress and package everything, including files and folders in current directory, into a single ZIP file on Ubuntu.

What would be the most convenient command for this (and name of the tool needed to be installed if any)?

Edit: What if I need to exclude one folder or several files?

2 Answers 2

216

Install zip and use

zip -r foo.zip .

You can use the flags -0 (none) to -9 (best) to change compressionrate

Excluding files can be done via the -x flag. From the man-page:

-x files
--exclude files
          Explicitly exclude the specified files, as in:

                 zip -r foo foo -x \*.o

          which  will  include the contents of foo in foo.zip while excluding all the files that end in .o.  The backslash avoids the shell filename substitution, so that the name matching
          is performed by zip at all directory levels.

          Also possible:

                 zip -r foo foo -x@exclude.lst

          which will include the contents of foo in foo.zip while excluding all the files that match the patterns in the file exclude.lst.

          The long option forms of the above are

                 zip -r foo foo --exclude \*.o

          and

                 zip -r foo foo --exclude @exclude.lst

          Multiple patterns can be specified, as in:

                 zip -r foo foo -x \*.o \*.c

          If there is no space between -x and the pattern, just one value is assumed (no list):

                 zip -r foo foo -x\*.o

          See -i for more on include and exclude.
0
32

I guess many people who come via Google to this question mean "archive and compress" when they write "zip". An alternative to the zip format is tar:

tar -czf copy.tar.gz whatever/

where the compressed archive file will be copy.tar.gz and the contents will be everything in the folder whatever.

 -c, --create
       create a new archive
 -z, --gzip, --gunzip --ungzip
 -f, --file ARCHIVE
       use archive file or device ARCHIVE
2
  • 1
    For xz, the option is --J or --xz
    – Anwar
    Jun 10, 2017 at 17:39
  • 1
    for anyone using the answer, do tar -tf <archive.tar.gz> to peek at the contents to verify that your tarball archive is how you expect it to be!. Jan 5 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.