-->
Previous Table of Contents Next


It also has a few disadvantages:

  For some versions of tar, the archive must reside on one disk or tape, which means that if a portion of the medium fails—from a bad sector on a disk or bad block on a tape, for example—the entire backup may be lost.
  tar can’t back up special files, such as device files.
  On its own, tar can perform only complete backups. If you want to do incremental backups, you have to do a little shell programming.


See “Working with Shell Scripts,” p. 365

Table 11.1 lists some options that are commonly used with tar. You can use many other command parameters with tar; refer to the man page for a complete list.

Table 11.1 Common Options for the tar Command

Option Description

c Creates an archive.
x Extracts or restores files from the archive that’s on the default device or on the device specified by the f option.
f name Creates the archive or reads the archive from name, where name is a filename or a device specified in /dev, such as /dev/rmt0.
Z Compresses or decompresses the tar archive.
z Compresses or decompresses the tar archive with gzip.
M Creates a multivolume tar backup.
t Creates an index of all files stored in an archive and lists on stdout.
v Uses verbose mode.

Consider some examples of the use of tar in backing up and restoring files. The following command copies the directory /home to the floppy drive /dev/fd0:


tar -cf /dev/fd0 /home

In this case, the f option specifies that the archive is created on the floppy drive device /dev/fd0.

The following command also archives the directory /home:


tar -cvfzM /dev/fd0 /home | tee homeindex

The v option indicates verbose mode, the z option indicates that the archive should be compressed to save space, and the M option tells tar to create a multivolume backup. When one floppy disk is full, tar prompts you for another. A list of the copied files is directed to homeindex. It’s a good idea to look at that file to see what was copied.

The find command is useful for locating files that have been modified within a certain time period so that they can be scheduled for incremental backups. The following example uses the command find to create a list of all files that have been modified in the last day:


find /home -mtime -1 -type f -print > bkuplst tar cvfzM /dev/fd0

‘cat bkuplst’ | tee homeindex

To use the list as input to the tar command, place the command cat bkuplst in back quotes (backward single quotation marks, also known as grave accents‘cat bkuplst’). This tells the shell to execute the command as a subshell and place the output from the command on the command line in the location of the original back-quoted command.

The following command restores the /home/dave/notes.txt file from the device /dev/fd0 (note that you have to give the complete filename to restore it):


tar xv /usr2/dave/notes.txt


TIP:  You can automate any of these commands by putting them in root’s crontab file. For example, you could put the following entry in the root’s crontab file to perform a backup of /home every day at 1:30 a.m.:

30 01 * * * tar cvfz /def/fd0 /home > homeindex

If you need to do more complicated backups, you can create shell scripts to control your backups. These shell scripts can also be run via cron.

See “Scheduling Commands with cron and crontab,” p. 388


You also can use the tar command to create archive files in the Linux file system rather than write to a backup device. This way, you can archive a group of files along with their directory structure in one file. To do this, simply give a file name as the argument to the f option instead of a device name. The following is an example of archiving a directory and its subdirectories with the tar command:


tar cvf /home/backup.tar /home/dave

This creates the file /home/backup.tar, which contains a backup of the /home/dave directory and all files and subdirectories below /home/dave.


NOTE:  The tar command by itself doesn’t perform any file compression. To compress the resulting tar file, either specify the z option with the tar command or use a compression program, such as gzip, on the final tar file.

When you use tar to make archive files, it’s usually a good idea to try to make the top-level entry in the tar file a directory. This way, when you extract the tar file, all the files in it are placed under a central directory in your current working directory. Otherwise, you could end up with hundreds of files in your directory if you extract a tar file in the wrong place.

Suppose that you have below your current directory a directory named data, which contains several hundred files. There are two basic ways to create a tar file of this directory. You can change directories to the data directory and create the tar file from there, as in this example:


$ pwd

/home/dave

$ cd data

$ pwd

/home/dave/data

$ tar cvf ../data.tar *

This creates a tar file in /home/dave that contains just the contents of data without containing an entry for the directory. When you extract this tar file, you don’t create a directory to put the files in—you just get several hundred files in your current directory.


Previous Table of Contents Next