Managing Disk Space   «Prev  Next»
Lesson 6 Creating an archive
Objective Use tar cvf to create an archive.
To create an archive with the tar command, use the following general form:
% tar cvf archive files

Options of tar

Note that the cvf options must be supplied as a single group, with no spaces in between. A preceding hyphen (-) is allowed but not required .

Specifying options with tar

The tar command is unusual because you do not need to put a hyphen (-) before the options. In the earliest UNIX versions, you were not supposed to use a - before the options; otherwise, you would generate an error. As UNIX evolved, users felt that the commands needed to be more consistent. Newer versions of UNIX therefore began allowing the - with tar options, but it wasn’t required.
The tar command allows a hyphen only before the first option. In addition, all options must be listed together, without spaces between them. The following table summarizes the basic syntax for the tar command. Notice that the first example is incorrect because it contains multiple hyphens and includes spaces between the options.

Command Comment
tar –c –v –f bigfile.tar my_dir Not allowed
tar –cvf bigfile.tar my_dir Allowed
tar cvf bigfile.tar my_dir Allowed
The c option stands for create. When you create an archive, c is required and must go first. The v stands for verbose. It’s optional but recommended. The v causes tar to list file names as they are being archived. The f option is used along with the archive argument, which is explained below.

Where to archive

The archive argument describes the output destination for the files you are archiving. The destination is either a tape device or a tar file. System administrators usually handle archiving to tape. They have access to the tapes and their associated devices. Administrators also know how to specify the devices, which have unusual names such as /dev/rmt0 or /dev/rst8. As a typical user, you will usually archive to a file.

What to archive

The files argument describes what you want to archive. For example, you can archive several files, a directory, or several directories. tar archives the entire hierarchy of any specified directory.
The following MouseOver shows how to create a tar file:
tar crate
tar create
  1. The cvf options are supplied as a group. c means create, and v means list the files. The f is needed when you supply an archive name.
  2. tar creates an archive named backup.tar. This single file will contain the directory hierarchy beginning at the project directory.
  3. The project directory and all of its subdirectories will be archived into a file named backup.tar.
  4. When you create an archive, the “verbose” output marks the first column with the letter a, for archive.
  5. With “verbose” output, tar reports the names and sizes of the files it is archiving

When you archive a directory, always supply the name as a relative pathname. Later, if you want to restore the archive, you will be able to place the directory tree in your current directory. In the next lesson, you will learn to list the file names in an archive.
[1]tape device: A tape device is any hardware that contains a tape drive for use with storage media.
[2]tar file: A tar file is an archive created by the tar command.