GNU tar - 4. GNU tar Operations

Go to the first, previous, next, last section, table of contents.

# 4. GNU `tar` Operations

## 4.1 Basic GNU `tar` Operations

The basic `tar` operations, `--create` (`-c`), `--list` (`-t`) and `--extract` (`--get`, `-x`), are currently presented and described in the tutorial chapter of this manual. This section provides some complementary notes for these operations.

`--create` (`-c`)
  Creating an empty archive would have some kind of elegance. One can initialize an empty archive and later use `--append` (`-r`) for adding all members. Some applications would not welcome making an exception in the way of adding the first archive member. On the other hand, many people reported that it is dangerously too easy for `tar` to destroy a magnetic tape with an empty archive(5). The two most common errors are:
  1. Mistakingly using `create` instead of `extract`, when the intent was to extract the full contents of an archive. This error is likely: keys `c` and `x` are right next ot each other on the QWERTY keyboard. Instead of being unpacked, the archive then gets wholly destroyed. When users speak about exploding an archive, they usually mean something else :-).
  2. Forgetting the argument to `file`, when the intent was to create an archive with a single file in it. This error is likely because a tired user can easily add the `f` key to the cluster of option letters, by the mere force of habit, without realizing the full consequence of doing so. The usual consequence is that the single file, which was meant to be saved, is rather destroyed.
  So, recognizing the likelihood and the catastrophical nature of these errors, GNU `tar` now takes some distance from elegance, and cowardly refuses to create an archive when `--create` (`-c`) option is given, there are no arguments besides options, and `--files-from=file-of-names` (`-T file-of-names`) option is not used. To get around the cautiousness of GNU `tar` and nevertheless create an archive with nothing in it, one may still use, as the value for the `--files-from=file-of-names` (`-T file-of-names`) option, a file with no names in it, as shown in the following commands:

      tar --create --file=empty-archive.tar --files-from=/dev/null
      tar cfT empty-archive.tar /dev/null

`--extract` (`--get`, `-x`)
  A socket is stored, within a GNU `tar` archive, as a pipe.
`--list` (`-t`)
  GNU `tar` now shows dates as ``1996-11-09'`, while it used to show them as ``Nov 11 1996'`. (One can revert to the old behavior by defining `USE_OLD_CTIME` in ``src/list.c'` before reinstalling.) But preferrably, people you should get used to ISO 8601 dates. Local American dates should be made available again with full date localisation support, once ready. In the meantime, programs not being localisable for dates should prefer international dates, that's really the way to go. Look up http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html if you are curious, it contains a detailed explanation of the ISO 8601 standard.

## 4.2 Advanced GNU `tar` Operations

Now that you have learned the basics of using GNU `tar`, you may want to learn about further ways in which `tar` can help you.

This chapter presents five, more advanced operations which you probably won't use on a daily basis, but which serve more specialized functions. We also explain the different styles of options and why you might want to use one or another, or a combination of them in your `tar` commands. Additionally, this chapter includes options which allow you to define the output from `tar` more carefully, and provide help and error correction in special circumstances.

@FIXME{check this after the chapter is actually revised to make sure it still introduces the info in the chapter correctly : ).}

### 4.2.1 The Five Advanced `tar` Operations

@UNREVISED

In the last chapter, you learned about the first three operations to `tar`. This chapter presents the remaining five operations to `tar`: ``--append'`, ``--update'`, ``--concatenate'`, ``--delete'`, and ``--compare'`.

You are not likely to use these operations as frequently as those covered in the last chapter; however, since they perform specialized functions, they are quite useful when you do need to use them. We will give examples using the same directory and files that you created in the last chapter. As you may recall, the directory is called ``practice'`, the files are ``jazz'`, ``blues'`, ``folk'`, ``rock'`, and the two archive files you created are ``collection.tar'` and ``music.tar'`.

We will also use the archive files ``afiles.tar'` and ``bfiles.tar'`. ``afiles.tar'` contains the members ``apple'`, ``angst'`, and ``aspic'`. ``bfiles.tar'` contains the members ``./birds'`, ``baboon'`, and ``./box'`.

Unless we state otherwise, all practicing you do and examples you follow in this chapter will take place in the ``practice'` directory that you created in the previous chapter; see section 2.4.1 Preparing a Practice Directory for Examples. (Below in this section, we will remind you of the state of the examples where the last chapter left them.)

The five operations that we will cover in this chapter are:

`--append`
`-r`
  Add new entries to an archive that already exists.
`--update`
`-r`
  Add more recent copies of archive members to the end of an archive, if they exist.
`--concatenate`
`--catenate`
`-A`
  Add one or more pre-existing archives to the end of another archive.
`--delete`
  Delete items from an archive (does not work on tapes).
`--compare`
`--diff`
`-d`
  Compare archive members to their counterparts in the file system.

Currently, the listing of the directory using `ls` is as follows:

The archive file ``collection.tar'` looks like this:

    $ tar -tvf collection.tar

The archive file ``music.tar'` looks like this:

    $ tar -tvf music.tar

@FIXME{need to fill in the above!!!}

### 4.2.2 How to Add Files to Existing Archives: `--append`

@UNREVISED

If you want to add files to an existing archive, you don't need to create a new archive; you can use `--append` (`-r`). The archive must already exist in order to use ``--append'`. (A related operation is the ``--update'` operation; you can use this to add newer versions of archive members to an existing archive. To learn how to do this with ``--update'`, see section 4.2.3 Updating an Archive.)

@FIXME{Explain in second paragraph whether you can get to the previous version -- explain whole situation somewhat more clearly.}

If you use `--append` (`-r`) to add a file that has the same name as an archive member to an archive containing that archive member, then the old member is not deleted. What does happen, however, is somewhat complex. `tar` allows you to have infinite numbers of files with the same name. Some operations treat these same-named members no differently than any other set of archive members: for example, if you view an archive with `--list` (`-t`), you will see all of those members listed, with their modification times, owners, etc.

Other operations don't deal with these members as perfectly as you might prefer; if you were to use `--extract` (`--get`, `-x`) to extract the archive, only the most recently added copy of a member with the same name as four other members would end up in the working directory. This is because ``--extract'` extracts an archive in the order the members appeared in the archive; the most recently archived members will be extracted last. Additionally, an extracted member will overwrite a file of the same name which existed in the directory already, and `tar` will not prompt you about this. Thus, only the most recently archived member will end up being extracted, as it will overwrite the one extracted before it, and so on.

@FIXME{ hag -- you might want to incorporate some of the above into the MMwtSN node; not sure. i didn't know how to make it simpler...}

There are a few ways to get around this. @FIXME-xref{Multiple Members with the Same Name}.

If you want to replace an archive member, use `--delete` to delete the member you want to remove from the archive, , and then use ``--append'` to add the member you want to be in the archive. Note that you can not change the order of the archive; the most recently added member will still appear last. In this sense, you cannot truely "replace" one member with another. (Replacing one member with another will not work on certain types of media, such as tapes; see section 4.2.5 Removing Archive Members Using ``--delete'` and section 9. Tapes and Other Archive Media, for more information.)

#### 4.2.2.1 Appending Files to an Archive

@UNREVISED

The simplest way to add a file to an already existing archive is the `--append` (`-r`) operation, which writes specified files into the archive whether or not they are already among the archived files. When you use ``--append'`, you must specify file name arguments, as there is no default. If you specify a file that already exists in the archive, another copy of the file will be added to the end of the archive. As with other operations, the member names of the newly added files will be exactly the same as their names given on the command line. The `--verbose` (`-v`) option will print out the names of the files as they are written into the archive.

``--append'` cannot be performed on some tape drives, unfortunately, due to deficiencies in the formats those tape drives use. The archive must be a valid `tar` archive, or else the results of using this operation will be unpredictable. See section 9. Tapes and Other Archive Media.

To demonstrate using ``--append'` to add a file to an archive, create a file called ``rock'` in the ``practice'` directory. Make sure you are in the ``practice'` directory. Then, run the following `tar` command to add ``rock'` to ``collection.tar'`:

    $ tar --append --file=collection.tar rock

If you now use the `--list` (`-t`) operation, you will see that ``rock'` has been added to the archive:

    $ tar --list --file=collection.tar
    -rw-rw-rw- me user     28 1996-10-18 16:31 jazz
    -rw-rw-rw- me user     21 1996-09-23 16:44 blues
    -rw-rw-rw- me user     20 1996-09-23 16:44 folk
    -rw-rw-rw- me user     20 1996-09-23 16:44 rock

@FIXME{in theory, dan will (soon) try to turn this node into what it's title claims it will become...}

#### 4.2.2.2 Multiple Files with the Same Name

You can use `--append` (`-r`) to add copies of files which have been updated since the archive was created. (However, we do not recommend doing this since there is another `tar` option called ``--update'`; see section 4.2.3 Updating an Archive for more information. We describe this use of ``--append'` here for the sake of completeness.) @FIXME{is this really a good idea, to give this whole description for something which i believe is basically a Stupid way of doing something? certain aspects of it show ways in which tar is more broken than i'd personally like to admit to, specifically the last sentence. On the other hand, i don't think it's a good idea to be saying that re explicitly don't recommend using something, but i can't see any better way to deal with the situation.} When you extract the archive, the older version will be effectively lost. This works because files are extracted from an archive in the order in which they were archived. Thus, when the archive is extracted, a file archived later in time will overwrite a file of the same name which was archived earlier, even though the older version of the file will remain in the archive unless you delete all versions of the file.

Supposing you change the file ``blues'` and then append the changed version to ``collection.tar'`. As you saw above, the original ``blues'` is in the archive ``collection.tar'`. If you change the file and append the new version of the file to the archive, there will be two copies in the archive. When you extract the archive, the older version of the file will be extracted first, and then overwritten by the newer version when it is extracted.

You can append the new, changed copy of the file ``blues'` to the archive in this way:

    $ tar --append --verbose --file=collection.tar blues
    blues

Because you specified the ``--verbose'` option, `tar` has printed the name of the file being appended as it was acted on. Now list the contents of the archive:

    $ tar --list --verbose --file=collection.tar
    -rw-rw-rw- me user     28 1996-10-18 16:31 jazz
    -rw-rw-rw- me user     21 1996-09-23 16:44 blues
    -rw-rw-rw- me user     20 1996-09-23 16:44 folk
    -rw-rw-rw- me user     20 1996-09-23 16:44 rock
    -rw-rw-rw- me user     58 1996-10-24 18:30 blues

The newest version of ``blues'` is now at the end of the archive (note the different creation dates and file sizes). If you extract the archive, the older version of the file ``blues'` will be overwritten by the newer version. You can confirm this by extracting the archive and running ``ls'` on the directory. See section 4.3.2 Changing How `tar` Writes Files, for more information. (Please note: This is the case unless you employ the `--backup` option; @FIXME-ref{Multiple Members with the Same Name}.)

### 4.2.3 Updating an Archive

@UNREVISED

In the previous section, you learned how to use `--append` (`-r`) to add a file to an existing archive. A related operation is `--update` (`-u`). The ``--update'` operation updates a `tar` archive by comparing the date of the specified archive members against the date of the file with the same name. If the file has been modified more recently than the archive member, then the newer version of the file is added to the archive (as with `--append` (`-r`)).

Unfortunately, you cannot use ``--update'` with magnetic tape drives. The operation will fail.

@FIXME{other examples of media on which --update will fail? need to ask charles and/or mib/thomas/dave shevett..}

Both ``--update'` and ``--append'` work by adding to the end of the archive. When you extract a file from the archive, only the version stored last will wind up in the file system, unless you use the `--backup` option (@FIXME-ref{Multiple Members with the Same Name}).

#### 4.2.3.1 How to Update an Archive Using `--update`

You must use file name arguments with the `--update` (`-u`) operation. If you don't specify any files, `tar` won't act on any files and won't tell you that it didn't do anything (which may end up confusing you).

@FIXME{note: the above parenthetical added because in fact, this behavior just confused the author. :-) }

To see the ``--update'` option at work, create a new file, ``classical'`, in your practice directory, and some extra text to the file ``blues'`, using any text editor. Then invoke `tar` with the ``update'` operation and the `--verbose` (`-v`) option specified, using the names of all the files in the practice directory as file name arguments:

    $ tar --update -v -f collection.tar blues folk rock classical
    blues
    classical
    $

Because we have specified verbose mode, `tar` prints out the names of the files it is working on, which in this case are the names of the files that needed to be updated. If you run ``tar --list'` and look at the archive, you will see ``blues'` and ``classical'` at its end. There will be a total of two versions of the member ``blues'`; the one at the end will be newer and larger, since you added text before updating it.

(The reason `tar` does not overwrite the older file when updating it is because writing to the middle of a section of tape is a difficult process. Tapes are not designed to go backward. See section 9. Tapes and Other Archive Media, for more information about tapes.

`--update` (`-u`) is not suitable for performing backups for two reasons: it does not change directory content entries, and it lengthens the archive every time it is used. The GNU `tar` options intended specifically for backups are more efficient. If you need to run backups, please consult section 5. Performing Backups and Restoring Files.

### 4.2.4 Combining Archives with `--concatenate`

Sometimes it may be convenient to add a second archive onto the end of an archive rather than adding individual files to the archive. To add one or more archives to the end of another archive, you should use the `--concatenate` (`--catenate`, `-A`) operation.

To use ``--concatenate'`, name the archives to be concatenated on the command line. (Nothing happens if you don't list any.) The members, and their member names, will be copied verbatim from those archives. If this causes multiple members to have the same name, it does not delete any members; all the members with the same name coexist. For information on how this affects reading the archive, @FIXME-ref{Multiple Members with the Same Name}.

To demonstrate how ``--concatenate'` works, create two small archives called ``bluesrock.tar'` and ``folkjazz.tar'`, using the relevant files from ``practice'`:

    $ tar -cvf bluesrock.tar blues rock
    blues
    classical
    $ tar -cvf folkjazz.tar folk jazz
    folk
    jazz

If you like, You can run ``tar --list'` to make sure the archives contain what they are supposed to:

    $ tar -tvf bluesrock.tar
    -rw-rw-rw- melissa user    105 1997-01-21 19:42 blues
    -rw-rw-rw- melissa user     33 1997-01-20 15:34 rock
    $ tar -tvf folkjazz.tar
    -rw-rw-rw- melissa user     20 1996-09-23 16:44 folk
    -rw-rw-rw- melissa user     65 1997-01-30 14:15 jazz

We can concatenate these two archives with `tar`:

    $ cd ..
    $ tar --concatenate --file=bluesrock.tar jazzfolk.tar

If you now list the contents of the ``bluesclass.tar'`, you will see that now it also contains the archive members of ``jazzfolk.tar'`:

    $ tar --list --file=bluesrock.tar
    blues
    rock
    jazz
    folk

When you use ``--concatenate'`, the source and target archives must already exist and must have been created using compatable format parameters (@FIXME-pxref{Matching Format Parameters}). The new, concatenated archive will be called by the same name as the first archive listed on the command line. @FIXME{is there a way to specify a new name?}

Like `--append` (`-r`), this operation cannot be performed on some tape drives, due to deficiencies in the formats those tape drives use.

It may seem more intuitive to you to want or try to use `cat` to concatenate two archives instead of using the ``--concatenate'` operation; after all, `cat` is the utility for combining files.

However, `tar` archives incorporate an end-of-file marker which must be removed if the concatenated archives are to be read properly as one archive. ``--concatenate'` removes the end-of-archive marker from the target archive before each new archive is appended. If you use `cat` to combine the archives, the result will not be a valid `tar` format archive. If you need to retrieve files from an archive that was added to using the `cat` utility, use the `--ignore-zeros` (`-i`) option. See section Ignoring Blocks of Zeros, for further information on dealing with archives improperly combined using the `cat` shell utility.

@FIXME{this shouldn't go here. where should it go?} You must specify the source archives using `--file=archive-name` (`-f archive-name`) (see section 6.1 Choosing and Naming Archive Files). If you do not specify the target archive, `tar` uses the value of the environment variable `TAPE`, or, if this has not been set, the default archive name.

### 4.2.5 Removing Archive Members Using ``--delete'`

@UNREVISED

You can remove members from an archive by using the `--delete` option. Specify the name of the archive with `--file=archive-name` (`-f archive-name`) and then specify the names of the members to be deleted; if you list no member names, nothing will be deleted. The `--verbose` (`-v`) option will cause `tar` to print the names of the members as they are deleted. As with `--extract` (`--get`, `-x`), you must give the exact member names when using ``tar --delete'`. ``--delete'` will remove all versions of the named file from the archive. The ``--delete'` operation can run very slowly.

Unlike other operations, ``--delete'` has no short form.

This operation will rewrite the archive. You can only use ``--delete'` on an archive if the archive device allows you to write to any point on the media, such as a disk; because of this, it does not work on magnetic tapes. Do not try to delete an archive member from a magnetic tape; the action will not succeed, and you will be likely to scramble the archive and damage your tape. There is no safe way (except by completely re-writing the archive) to delete files from most kinds of magnetic tape. See section 9. Tapes and Other Archive Media.

To delete all versions of the file ``blues'` from the archive ``collection.tar'` in the ``practice'` directory, make sure you are in that directory, and then,

    $ tar --list --file=collection.tar
    blues
    folk
    jazz
    rock
    practice/blues
    practice/folk
    practice/jazz
    practice/rock
    practice/blues
    $ tar --delete --file=collection.tar blues
    $ tar --list --file=collection.tar
    folk
    jazz
    rock
    $

@FIXME{I changed the order of these nodes around and haven't had a chance to fix the above example's results, yet. I have to play with this and follow it and see what it actually does!}

The `--delete` option has been reported to work properly when `tar` acts as a filter from `stdin` to `stdout`.

### 4.2.6 Comparing Archive Members with the File System

@UNREVISED

The ``--compare'` (``-d'`), or ``--diff'` operation compares specified archive members against files with the same names, and then reports differences in file size, mode, owner, modification date and contents. You should only specify archive member names, not file names. If you do not name any members, then `tar` will compare the entire archive. If a file is represented in the archive but does not exist in the file system, `tar` reports a difference.

You have to specify the record size of the archive when modifying an archive with a non-default record size.

`tar` ignores files in the file system that do not have corresponding members in the archive.

The following example compares the archive members ``rock'`, ``blues'` and ``funk'` in the archive ``bluesrock.tar'` with files of the same name in the file system. (Note that there is no file, ``funk'`; `tar` will report an error message.)

    $ tar --compare --file=bluesrock.tar rock blues funk
    rock
    blues
    tar: funk not found in archive

@FIXME{what does this actually depend on? i'm making a guess, here.}Depending on the system where you are running `tar` and the version you are running, `tar` may have a different error message, such as:

    funk: does not exist

@FIXME-xref{somewhere, for more information about format parameters. Melissa says: such as "format variations"? But why? Clearly I don't get it yet; I'll deal when I get to that section.}

The spirit behind the `--compare` (`--diff`, `-d`) option is to check whether the archive represents the current state of files on disk, more than validating the integrity of the archive media. For this later goal, See section 9.8 Verifying Data as It is Stored.

## 4.3 Options Used by `--extract`

@UNREVISED

@FIXME{i need to get dan to go over these options with me and see if there's a better way of organizing them.}

The previous chapter showed how to use `--extract` (`--get`, `-x`) to extract an archive into the filesystem. Various options cause `tar` to extract more information than just file contents, such as the owner, the permissions, the modification date, and so forth. This section presents options to be used with ``--extract'` when certain special considerations arise. You may review the information presented in section 2.6 How to Extract Members from an Archive for more basic information about the ``--extract'` operation.

### 4.3.1 Options to Help Read Archives

@UNREVISED

Normally, `tar` will request data in full record increments from an archive storage device. If the device cannot return a full record, `tar` will report an error. However, some devices do not always return full records, or do not require the last record of an archive to be padded out to the next record boundary. To keep reading until you obtain a full record, or to accept an incomplete record if it contains an end-of-archive marker, specify the `--read-full-records` (`-B`) option in conjunction with the `--extract` (`--get`, `-x`) or `--list` (`-t`) operations. See section 9.4 Blocking.

The `--read-full-records` (`-B`) option is turned on by default when `tar` reads an archive from standard input, or from a remote machine. This is because on BSD Unix systems, attempting to read a pipe returns however much happens to be in the pipe, even if it is less than was requested. If this option were not enabled, `tar` would fail as soon as it read an incomplete record from the pipe.

If you're not sure of the blocking factor of an archive, you can read the archive by specifying `--read-full-records` (`-B`) and `--blocking-factor=512-size` (`-b 512-size`), using a blocking factor larger than what the archive uses. This lets you avoid having to determine the blocking factor of an archive. See section 9.4.2 The Blocking Factor of an Archive.

#### Reading Full Records

@FIXME{need sentence or so of intro here}

`--read-full-records`
`-B`
  Use in conjunction with `--extract` (`--get`, `-x`) to read an archive which contains incomplete records, or one which has a blocking factor less than the one specified.

#### Ignoring Blocks of Zeros

Normally, `tar` stops reading when it encounters a block of zeros between file entries (which usually indicates the end of the archive). `--ignore-zeros` (`-i`) allows `tar` to completely read an archive which contains a block of zeros before the end (i.e. a damaged archive, or one which was created by `cat`-ing several archives together).

The `--ignore-zeros` (`-i`) option is turned off by default because many versions of `tar` write garbage after the end-of-archive entry, since that part of the media is never supposed to be read. GNU `tar` does not write after the end of an archive, but seeks to maintain compatablity among archiving utilities.

`--ignore-zeros`
`-i`
  To ignore blocks of zeros (ie. end-of-archive entries) which may be encountered while reading an archive. Use in conjunction with `--extract` (`--get`, `-x`) or `--list` (`-t`).

#### Ignore Fail Read

@FIXME{Is this in the right place? It doesn't exist anywhere else in the book (except the appendix), and has no further explanation. For that matter, what does it mean?!}

`--ignore-failed-read`
  Do not exit with nonzero on unreadable files or directories.

### 4.3.2 Changing How `tar` Writes Files

@UNREVISED

@FIXME{need to mention the brand new option, --backup}

#### Options to Prevent Overwriting Files

Normally, `tar` writes extracted files into the file system without regard to the files already on the system; i.e., files with the same names as archive members are overwritten when the archive is extracted. If the name of a corresponding file name is a symbolic link, the file pointed to by the symbolic link will be overwritten instead of the symbolic link itself (if this is possible). Moreover, special devices, empty directories and even symbolic links are automatically removed if they are found to be on the way of the proper extraction.

To prevent `tar` from extracting an archive member from an archive if doing so will overwrite a file in the file system, use `--keep-old-files` (`-k`) in conjunction with ``--extract'`. When this option is specified, `tar` will report an error stating the name of the files in conflict instead of overwriting the file with the corresponding extracted archive member.

@FIXME{these two P's have problems. i don't understand what they're trying to talk about well enough to fix them; i may have just made them worse (in particular the first of the two). waiting to talk with hag.}

The `--unlink-first` (`-U`) option removes existing files, symbolic links, empty directories, devices, etc., prior to extracting over them. In particular, using this option will prevent replacing an already existing symbolic link by the name of an extracted file, since the link itself is removed prior to the extraction, rather than the file it points to. On some systems, the backing store for the executable is the original program text. You could use the `--unlink-first` (`-U`) option to prevent segmentation violations or other woes when extracting arbitrary executables over currently running copies. Note that if something goes wrong with the extraction and you did use this option, you might end up with no file at all. Without this option, if something goes wrong with the extraction, the existing file is not overwritten and preserved.

@FIXME{huh?} If you specify the `--recursive-unlink` option, `tar` removes anything that keeps you from extracting a file as far as current permissions will allow it. This could include removal of the contents of a full directory hierarchy. For example, someone using this feature may be very surprised at the results when extracting a directory entry from the archive. This option can be dangerous; be very aware of what you are doing if you choose to use it.

#### Keep Old Files

`--keep-old-files`
`-k`
  Do not overwrite existing files from archive. The `--keep-old-files` (`-k`) option prevents `tar` from over-writing existing files with files with the same name from the archive. The `--keep-old-files` (`-k`) option is meaningless with `--list` (`-t`). Prevents `tar` from overwriting files in the file system during extraction.

#### Unlink First

`--unlink-first`
`-U`
  Try removing files before extracting over them, instead of trying to overwrite them.

#### Recursive Unlink

`--recursive-unlink`
  When this option is specified, try removing files and directory hierarchies before extracting over them. This is a dangerous option!

Some people argue that GNU `tar` should not hesitate to overwrite files with other files when extracting. When extracting a `tar` archive, they expect to see a faithful copy of the state of the filesystem when the archive was created. It is debatable that this would always be a proper behaviour. For example, suppose one has an archive in which ``usr/local'` is a link to ``usr/local2'`. Since then, maybe the site removed the link and renamed the whole hierarchy from ``/usr/local2'` to ``/usr/local'`. Such things happen all the time. I guess it would not be welcome at all that GNU `tar` removes the whole hierarchy just to make room for the link to be reinstated (unless it also simultaneously restores the full ``/usr/local2'`, of course! GNU `tar` is indeed able to remove a whole hierarchy to reestablish a symbolic link, for example, but only if `--recursive-unlink` is specified to allow this behaviour. In any case, single files are silently removed.

#### Setting Modification Times

Normally, `tar` sets the modification times of extracted files to the modification times recorded for the files in the archive, but limits the permissions of extracted files by the current `umask` setting.

To set the modification times of extracted files to the time when the files were extracted, use the `--touch` (`-m`) option in conjunction with `--extract` (`--get`, `-x`).

`--touch`
`-m`
  Sets the modification time of extracted archive members to the time they were extracted, not the time recorded for them in the archive. Use in conjunction with `--extract` (`--get`, `-x`).

#### Setting Access Permissions

To set the modes (access permissions) of extracted files to those recorded for those files in the archive, use ``--same-persmissions'` in conjunction with the `--extract` (`--get`, `-x`) operation. @FIXME{Should be aliased to ignore-umask.}

`--preserve-permission`
`--same-permission`
`--ignore-umask`
`-p`
  Set modes of extracted archive members to those recorded in the archive, instead of current umask settings. Use in conjunction with `--extract` (`--get`, `-x`).

@FIXME{Following paragraph needs to be rewritten: why doesnt' this cat files together, why is this useful. is it really useful with more than one file?}

#### Writing to Standard Output

To write the extracted files to the standard output, instead of creating the files on the file system, use `--to-stdout` (`-O`) in conjunction with `--extract` (`--get`, `-x`). This option is useful if you are extracting files to send them through a pipe, and do not need to preserve them in the file system. If you extract multiple members, they appear on standard output concatenated, in the order they are found in the archive.

`--to-stdout`
`-O`
  Writes files to the standard output. Used in conjunction with `--extract` (`--get`, `-x`). Extract files to standard output. When this option is used, instead of creating the files specified, `tar` writes the contents of the files extracted to its standard output. This may be useful if you are only extracting the files in order to send them through a pipe. This option is meaningless with `--list` (`-t`).

@FIXME{Why would you want to do such a thing, how are files separated on the standard output? is this useful with more that one file? Are pipes the real reason?}

#### Removing Files

@FIXME{the various macros in the front of the manual think that this option goes in this section. i have no idea; i only know it's nowhere else in the book...}

`--remove-files`
  Remove files after adding them to the archive.

### 4.3.3 Coping with Scarce Resources

@UNREVISED

#### Starting File

`--starting-file=name`
`-K name`
  Starts an operation in the middle of an archive. Use in conjunction with `--extract` (`--get`, `-x`) or `--list` (`-t`).

If a previous attempt to extract files failed due to lack of disk space, you can use `--starting-file=name` (`-K name`) to start extracting only after member name of the archive. This assumes, of course, that there is now free space, or that you are now extracting into a different file system. (You could also choose to suspend `tar`, remove unnecessary files from the file system, and then restart the same `tar` operation. In this case, `--starting-file=name` (`-K name`) is not necessary. See section 5.2 Using `tar` to Perform Incremental Dumps, See section 3.7 Asking for Confirmation During Operations, and section 6.4 Excluding Some Files.)

#### Same Order

`--same-order`
`--preserve-order`
`-s`
  To process large lists of file names on machines with small amounts of memory. Use in conjunction with `--compare` (`--diff`, `-d`), `--list` (`-t`) or `--extract` (`--get`, `-x`).

@FIXME{we don't need/want --preserve to exist any more (from melissa: ie, don't want that *version* of the option to exist, or don't want the option to exist in either version?}

@FIXME{i think this explanation is lacking.}

The `--same-order` (`--preserve-order`, `-s`) option tells `tar` that the list of file names to be listed or extracted is sorted in the same order as the files in the archive. This allows a large list of names to be used, even on a small machine that would not otherwise be able to hold all the names in memory at the same time. Such a sorted list can easily be created by running ``tar -t'` on the archive and editing its output.

This option is probably never needed on modern computer systems.

## 4.4 Backup options

GNU `tar` offers options for making backups of files before writing new versions. These options control the details of these backups. They may apply to the archive itself before it is created or rewritten, as well as individual extracted members. Other GNU programs (`cp`, `install`, `ln`, and `mv`, for example) offer similar options.

Backup options may prove unexpectedly useful when extracting archives containing many members having identical name, or when extracting archives on systems having file name limitations, making different members appear has having similar names through the side-effect of name truncation. (This is true only if we have a good scheme for truncated backup names, which I'm not sure at all: I suspect work is needed in this area.) When any existing file is backed up before being overwritten by extraction, then clashing files are automatically be renamed to be unique, and the true name is kept for only the last file of a series of clashing files. By using verbose mode, users may track exactly what happens.

At the detail level, some decisions are still experimental, and may change in the future, we are waiting comments from our users. So, please do not learn to depend blindly on the details of the backup features. For example, currently, directories themselves are never renamed through using these options, so, extracting a file over a directory still has good chances to fail. Also, backup options apply to created archives, not only to extracted members. For created archives, backups will not be attempted when the archive is a block or character device, or when it refers to a remote file.

For the sake of simplicity and efficiency, backups are made by renaming old files prior to creation or extraction, and not by copying. The original name is restored if the file creation fails. If a failure occurs after a partial extraction of a file, both the backup and the partially extracted file are kept.

``--backup'`
  Make backups of files that are about to be overwritten or removed. Without this option, the original versions are destroyed.
``--suffix=suffix'`
  Append suffix to each backup file made with ``-b'`. If this option is not specified, the value of the `SIMPLE_BACKUP_SUFFIX` environment variable is used. And if `SIMPLE_BACKUP_SUFFIX` is not set, the default is ``~'`, just as in Emacs.
``--version-control=method'`
  Use method to determine the type of backups made with `--backup`. If this option is not specified, the value of the `VERSION_CONTROL` environment variable is used. And if `VERSION_CONTROL` is not set, the default backup type is ``existing'`. This option corresponds to the Emacs variable ``version-control'`; the same values for method are accepted as in Emacs. This options also more descriptive name. The valid methods (unique abbreviations are accepted):
  ``t'`
  ``numbered'`
    Always make numbered backups.
  ``nil'`
  ``existing'`
    Make numbered backups of files that already have them, simple backups of the others.
  ``never'`
  ``simple'`
    Always make simple backups.

Some people express the desire to always use the op-backup option, by defining some kind of alias or script. This is not as easy as one may thing, due to the fact old style options should appear first and consume arguments a bit inpredictably for an alias or script. But, if you are ready to give up using old style options, you may resort to using something like (a Bourne shell function here):

    tar () { /usr/local/bin/tar --backup $*; }

## 4.5 Notable `tar` Usages

@UNREVISED

@FIXME{Using Unix file linking capability to recreate directory structures--linking files into one subdirectory and then `tar`ring that directory.}

@FIXME{Nice hairy example using absolute-names, newer, etc.}

You can easily use archive files to transport a group of files from one system to another: put all relevant files into an archive on one computer system, transfer the archive to another system, and extract the contents there. The basic transfer medium might be magnetic tape, Internet FTP, or even electronic mail (though you must encode the archive with `uuencode` in order to transport it properly by mail). Both machines do not have to use the same operating system, as long as they both support the `tar` program.

For example, here is how you might copy a directory's contents from one disk to another, while preserving the dates, modes, owners and link-structure of all the files therein. In this case, the transfer medium is a pipe, which is one a Unix redirection mechanism:

    $ cd sourcedir; tar -cf - . | (cd targetdir; tar -xf -)

The command also works using short option forms:

@FIXME{The following using standard input/output correct??}

    $ cd sourcedir; tar --create --file=- . | (cd targetdir; tar --extract --file=-)

This is one of the easiest methods to transfer a `tar` archive.

## 4.6 Looking Ahead: The Rest of this Manual

You have now seen how to use all eight of the operations available to `tar`, and a number of the possible options. The next chapter explains how to choose and change file and archive names, how to use files to store names of other files which you can then call as arguments to `tar` (this can help you save time if you expect to archive the same list of files a number of times), and how to @FIXME{in case it's not obvious, i'm making this up in some sense based on my imited memory of what the next chapter *really* does. i just wanted to flesh out this final section a little bit so i'd remember to sitck it in here. :-)}

If there are too many files to conveniently list on the command line, you can list the names in a file, and `tar` will read that file. See section 6.3 Reading Names from a File.

There are various ways of causing `tar` to skip over some files, and not archive them. See section 6. Choosing Files and Names for `tar`.

Go to the first, previous, next, last section, table of contents.