217

In the Finder, there is this wonderful ability to right click on a file or directory, select compress from the drop-down, and end up with a zipped file.

Is it possible to do the same thing from the terminal?

5 Answers 5

257

It's called zip.

This adds the file file to the archive file.zip:

zip file.zip file 

Of course, to add more files, just add them as arguments to the command. Check out man zip for more options.

Often, you'll want to skip including those pesky .DS_Store files, for example compressing the whole folder folder into folder.zip:

zip -vr folder.zip folder/ -x "*.DS_Store"
10
  • whats the (1) after the word zip? Commented Jul 28, 2014 at 15:43
  • 1
    @JacobRaccuia See superuser.com/questions/297702/… Commented Jul 28, 2014 at 16:33
  • Is it the plain old GNU zip that comes with OS X? Commented Nov 10, 2017 at 16:10
  • 6
    You can use -X to exclude all annoying Mac OS hidden files, not only DS_Store. Commented May 2, 2020 at 16:08
  • 2
    Avoid console clutter zip -rXq f.zip f -r Recursively -X as said @ivanzolotov -q Quiet mode; Commented Nov 7, 2022 at 23:19
63

with considering the above answers, If you want to compress a directory or folder with the zip command:

zip -r directory.zip directory 

Explanation:

zip command for zipping

directory.zip is the destination file that will be created after running the zip command.

direcotry source folder which you want to be compressed.

-r flag will recursively iterate in folders and subfolders.

2
  • 3
    This should be the correct answer Commented Dec 11, 2021 at 11:56
  • This does not preserve symlinks. eg. if you zip a .app then a symlink like Frameworks/App.framework/Versions/Current is just becomes a cloned directory Commented Dec 1, 2023 at 20:45
58

To compress the files exactly as the Finder command would compress them, use:

ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip 

See man ditto for details:

 The command: ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip will create a PKZip archive similarly to the Finder's Compress function- ality. 
1
  • 5
    This is the best answer because it produces an identical zip, whereas CLI zip or tar is different and slightly smaller. A similar question with the same answer: stackoverflow.com/questions/10738505/… Commented May 11, 2017 at 15:18
9

There is tar(1) and gzip (or bzip2 or lzma). Tar is used to roll a number of files into one archive, while the one of the other three is used to compress it.

On a command line, you will call tar with a couple of options to create an archive and gzip it.

E.g.:

tar -c -z -f myarchive.tar.gz -C /home/username Downloads 

This willl -c reate a g -z ipped archive named -f ile from the -C hange-folder-to directory and will contain all files in the folder Downloads. The -C option is optional and the source-file arguments will be taken from the current folder if omitted.

For reference: tar tutorial

2

If you wish to compress specific files you can add multiple file names like so:

zip my_archive_name.zip some_file_a some_file_b some_file_c 

and that results in an archive that only has the files specified

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.