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:
The tar command creates a new archive named backup.tar.
The files and directories specified are added to the archive.
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:
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.