Unix zip and gzip

Zip and gzip are handy Unix programs to compress files. Start by creating a file called “something” on your Desktop that has “I like cats” 1,000 times.

File.open("something", "w") do |f|
  1000.times do
    f.puts "I like cats"

To zip the file, type zip followed by the name of the zipped file, followed by the name of the file that is to be zipped. The following command will create a something.zip file:

$ zip something.zip something

To unzip a zip file, type unzip followed by the filename:

$ unzip something.zip

To gzip a file, type gzip and the filename:

$ gzip something

This creates a file with a .gz file extension that can be unzipped with the gunzip command:

$ gunzip something.gz

zip and gunzip use different compression methodologies and gunzip produces a smaller file size in this case.

Desktop $ ls -ahlF
-rw-r--r--   1 powers  staff    12K Jul 15 20:37 something
-rw-r--r--@  1 powers  staff    86B Jul 15 20:37 something.gz
-rw-r--r--   1 powers  staff   226B Jul 15 20:50 something.zip

zip support password protection (gzip does not), and this is a nifty feature to take advantage of when transmitting sensitive information.

$ zip -e something.zip something

After entering this command, a prompt will request a password and password_confirmation. The -e flag stands for encrypt, which might help you remember 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s