12.3 Backing Up, Compressing, and Restoring Files
12.3.1 Backing up files with tar
The UNIX operating system has several integrated utilities that allow multiple files to be backed up and compressed. The tar (tape archive) command enables you to back up single or multiple files in a directory hierarchy. The tar command is standard with all versions of the UNIX operating system. Although it was originally developed for use with tape drives, tar can copy files to other locations on the hard disk or to a floppy disk and other removable media. The tar command can create an archive from a single file, but it is primarily used to combine multiple files, such as the contents of a directory, into a single file and then extract them later if they are needed. By itself, it does not compress the files as it bundles them like the zip/winzip programs for PC's. The most frequently used options available with the tar command are c, t, and x. It is not necessary (but accepted) to precede these with a dash (-) as with other UNIX command options. The command syntax is shown below. Roll over Figure for an example with an explanation of the syntax. Figure shows the functions (letters and modifiers) that are used with the tar command.
Command Format:
tar function [modifier] [output file] filename(s)/directory(s)

Tar Functions:

  • c (create) - used to archive one or more files or directories to create an archive file. 
    Note
    : When specifying the name of the output file to be created, be sure to specify a .tar extension so tar files can be easily distinguished from others.
  • t (table of contents) - used to see a table of contents of the tar file. This is a listing of the files that were combined to make the one tar file.
  • x (extract) - used to extract the file(s) from the archive and separate them into multiple files again. The tar file still exists after this is done.

Function Modifiers:

  • f (file) - allows you to specify a tar file to Create (c), Extract from (x) or see the Table of contents of (t).
  • v (verbose) - modifier executes the command in verbose mode which allows you to see the detailed results of the tar command as they occur.

Figure illustrates some examples using the tar command. These are explained in greater detail below.

  • Example 1 - Archives all files in the user1 directory to tape. If no file name (f) option is specified, the default output device is tape (/dev/rmt/0 - raw magnetic tape 0 is the first tape drive).
  • Example 2 - Archives all files and subdirectories in the dir2 to a tar file called dir2.tar and places it in the /home/user2/backup directory. If you refer to the class file tree, you will see that beans and recipes are subdirectories whereas notes is a file. Any files in subdirectories will also be included in the resulting tar file.
  • Example 3 - Combines several files to create an archive file called files.tar in the default directory of the hard drive (usually your home directory).
  • Example 4 - Views the table of contents of the tar file in verbose mode which displays the detail of the files including permission and size, etc.
  • Example 5 - Archives three files to the floppy disk for backup.
Interactive Media Activity (Flash, 95 kB)
  Archiving files with tar

In this media activity, you are logged in as user2 and your current working directory is: /home/user2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives.
Note: Be sure to press enter after each one. Click on step 1 to begin.