How to tar a bunch of files

Suppose you have a bunch of files and/or directories and want to email them or transport them on a floppy disk. You obviously want to waste as little space as possible and to save the directory structure along with the files. To do this you need to create a tar (tape archive) file. This old command can be used to put directories and files onto magnetic tape---hence the name.

Let's say that the files you want to send are in your home directory under the directory named Lab01. You want to create a tar file of Lab01 and all files and subdirectories that it contains.

Here's what you do:

  1. Change directories to the directory that contains the files. In this case you want to change to your home directory: cd ~
  2. Create a compressed archive named foo.tgz containing Lab01 and its contents: tar zcf foo.tgz Lab01. You can send this archive, put it on a floppy etc. Under Windows use Winzip to extract the contents of the file.
  3. The file in your home directory named foo.tgz contains a compressed tar archive. If you want to verify which files are in it just type: tar ztf foo.tgz to see a list.
  4. If you want to uncompress/untar the archive type tar zxf foo.tgz. Warning!: uncompressing the archive will overwrite any indentically named files and directories. You can create a separate directory named tmp (mkdir tmp and uncompress/untar the archive to the directory via tar zxf foo.tgz -C tmp.