Creating a zip file of a directory in command line
What is the ideal compressed file format when distributing multiple files (e.g. via email) with reasonable compression to your friends? I think creating a zip file is one of the most reasonable choice over other possible formats such as .tgz, or .rar etc. This is because zip is, I believe, the most widely supported compressed file format including in Windows.
To create a zip file for a directory mydir in command line, you may do
$ zip -r mydir.zip mydir
This will create mydir.zip in which all files and sub-directories inside mydir are zipped together recursively.
On the other hand, to extract a zip file, use unzip command as
$ unzip mydir.zip
Appendix 1: What if .tgz ?
In the Unix world, however, the zipped tar archive format .tgz (or sometimes .tar.gz) is dominant.
To archive a directory mydir into a file mydir.tgz, use:
$ tar cvfz mydir.tgz mydir
,where the option flags cvfz stands for "compress as a zipped file verbosely".
To extract an archive file mydir.tgz into a directory mydir, use:
$ tar xvfz mydir.tgz
,where the option flags xvfz stands for "extract a zipped file verbosely".
For more precise meanings of the option flags, please consult the manual page of tar command by
$ man tar
Appendix 2: What if .7z ?
Days after writing the main part of this article, one of my colleagues sent a file with .7z archive file format. I did a small survey to deal with .7z files under the command line in Ubuntu. Here are my findings.
One of the commands that can handle .7z format is p7zip command. To install the package containing the command, do
$ sudo apt-get install p7zip
To archive a directory mydir into a file mydir.7z:
$ p7zip mydir
To extract an archive file mydir.7z into a directory mydir:
$ p7zip -d mydir.7z
Appendix 3: What if .rar ?
We can handle .rar archives by rar command. To install the command, do
$ sudo apt-get install rar
To archive a directory mydir into a file mydir.rar:
$ rar a mydir
To extract an archive file mydir.rar into a directory mydir:
$ rar x mydir.rar