Hey there!
It’s time for another installment of UNIX command line tips. In the previous newsletter, we talked about the tree
command, which is useful for viewing the structure of a project’s directory.
This week we’ll be looking at a quick way to archive and unravel the contents of a directory using the popular zip
and unzip
utilities.
A QUICK START FOR USING ZIP AND UNZIP
Per the man pages, the zip
command is used to package and compress (archive) files, so let’s look at an example to help us clarify how to use it.
Let’s say I want to create a compressed archive of my Downloads directory, with it currently containing the following files:
$ ls -l ~/Downloads
total 8312
-rw-r--r--@ 1 screencast staff 9642 Jul 30 15:32 Hemisu-Dark.terminal
-rw-r--r--@ 1 screencast staff 4239836 Aug 26 21:46 tk8.6.0-src.tar.gz
Using zip
to create the archive, your first argument supplied should be the name of the archive, followed by what you want to include in it:
$ zip laptop ~/Downloads/*
As, zip
progresses through the compression process, it will provide nice feedback like the following:
adding: Users/screencast/Downloads/Hemisu-Dark.terminal (deflated 82%)
adding: Users/screencast/Downloads/tk8.6.0-src.tar.gz (deflated 0%)
And you can verify that it indeed created the archive using the name we specified earlier, but it automatically appends a .zip
extension to the filename:
$ ls -l laptop.zip
-rw-r--r-- 1 screencast staff 4238869 Aug 26 21:55 laptop.zip
To verify the contents of the laptop.zip
file, we can use the complementary utility to zip
, which is not surprisingly called unzip
. Per the man pages, the unzip
command is used to list, test and extract compressed files in a ZIP archive.
To verify the contents of the zip
archive using unzip
, simply pass the -l
option and specify the zip
archive file:
$ unzip -l laptop.zip
Archive: laptop.zip
Length Date Time Name
------ ---- ---- ----
9642 07-30-13 15:32 Users/screencast/Downloads/Hemisu-Dark.terminal 4239836 08-26-13 21:46 Users/screencast/Downloads/tk8.6.0-src.tar.gz
-------- -------
4249478 2 files
This is a really convenient way to see size of the files that you’ll be unraveling, just so you know roughly how long the process might take. In this example, un-packaging the archive looks fairly harmless, so I’ll do so by specifying the archive file name, the -d
option, which indicates you’ll be specifying a destination directory where the files will be placed, followed by the name of that directory:
$ unzip laptop.zip -d /tmp
Archive: laptop.zip
inflating: /tmp/Users/screencast/Downloads/Hemisu-Dark.terminal
inflating: /tmp/Users/screencast/Downloads/tk8.6.0-src.tar.gz
As you can see, those files were indeed placed into the /tmp
directory as I had preferred.
This is just the tip of the iceberg on what zip
and unzip
can do, so I encourage you to review the man pages for the other options that are available.
For more UNIX tips, please visit UnixCasts.com.