How to compress a file in UNIX?

zip syntax is

zip archivefile1 file1 file2 file3

for compression,

unzip archivefile1

for decompression.

So in your case, for compression:

zip CW3.zip CW3

See here.


If you are flexible about the format, you can compress a file in Unix doing just that :-)

 compress some_file

downside: you need to specify every single file to compress.

You can also use zip, not always available on UNIX platforms, or gzip. While compress and gzip just compress a single file, zip is a tool that handles packaging of one or many files or directories and compression all in one go. The traditional UNIX philosophy is to have one tool for one job, making it easier for the tool's creator to make the tool excel at that one job, explaining why compress and gzip only compress, and do not package. For you as a user that means you will use use one tool for packaging, and then another to compress the package (or "archive"), if you are not using zip, but UNIX native tar, the tape archiver, to package first:

tar -c -f archive_name.tar some_file some_dir

will pack some_file and some_dir, recursively, in the archive archive_name.tar.

now compress it:

gzip archive_name.tar

will give you the compressed file archive_name.tar.gz (and delete the original archive_name.tar).

But wait, there is more: it is actually so common to compress archives, that tar can call gzip, in one command:

tar -c -z -f archive_name.tar.gz some_file some_dir

will give you archive_name.tar.gz, containing some_file and some_dir, packaged recursively, and compressed.

For more info:

man 1 tar

zip is usually not installed by default. It is more of an archiving tool than a single file compression tool.

Either gzip or compress should be installed. They are files compression tools which add a .gz or .Z extension to the compressed file. The bzip2 program may also be installed.

gzip and bzip2 are usually install with a suite of programs to cat, diff, or grep files. These would be zcat, zdiff, and zgrep or bzcat, bzdiff, and bzgrep respectively.