Managing Disk Space   «Prev  Next»
Lesson 6 Creating an archive
Objective Use tar cvf to create an archive.

Use tar cvf to create an archive

To create an archive using `tar cvf` in Unix, you use the following command syntax:
tar cvf archive_name.tar file1 file2 directory1 ...

Explanation of the options:
  • `c`: Stands for "create" and tells tar to create a new archive.
  • `v`: Stands for "verbose" and displays the files being archived.
  • `f`: Stands for "file" and specifies the name of the archive file to create.

Example: To create an archive named `backup.tar` that includes the files `file1.txt`, `file2.txt`, and the directory `documents/`:
tar cvf backup.tar file1.txt file2.txt documents/

What Happens:
  1. The tar command creates a new archive named backup.tar.
  2. The files and directories specified are added to the archive.
  3. The v option displays each file as it is added.

Result: You now have an archive file named `backup.tar` in the current directory containing `file1.txt`, `file2.txt`, and all the contents of `documents/`.
To create an archive with the tar command, use the following general form:
% tar cvf archive files

Unix tar Options

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 diagram shows how to create a tar file:
tar crate
danielg % tar cvf backup.tar project
a project/ OK
a project/docs/ OK
a project/docs/index.html 1K
a project/docs/doc_plan.html 121K
a project/mktg/ OK
a project/mktg/index.html 1K
a project/mktg/requirements.html 81K
a project/qa/ OK
a project/qa/test_plan.html 41K
a project/sw/ OK
a project/sw/index.html 1K
a project/sw/product_spec.html 17K
a project/index.html 1K
danielg %

This script shows the use of the `tar` command to create a tar archive (`backup.tar`) of the `project` directory, listing the files and directories being added to the archive. The `cvf` options stand for:
  • c: Create a new archive.
  • v: Verbosely list files processed.
  • f: Specify the name of the archive file (backup.tar).
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.

SEMrush Software 6 SEMrush Banner 6