[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
tar
OperationsNow 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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
tar
OperationsIn 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’, 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’. The archive ‘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 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:
Add new entries to an archive that already exists.
Add more recent copies of archive members to the end of an archive, if they exist.
Add one or more pre-existing archives to the end of another archive.
Delete items from an archive (does not work on tapes).
Compare archive members to their counterparts in the file system.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 Updating an Archive.)
If you use ‘--append’ 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 number 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 data modification times, owners, etc.
Other operations don’t deal with these members as perfectly as you might
prefer; if you were to use ‘--extract’ to extract the archive,
only the most recently added copy of a member with the same name as
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 replace a file of
the same name which existed in the directory already, and tar
will not prompt you about this(10). Thus, only
the most recently archived member will end up being extracted, as it
will replace the one extracted before it, and so on.
There exists a special option that allows you to get around this
behavior and extract (or list) only a particular copy of the file.
This is ‘--occurrence’ option. If you run tar
with
this option, it will extract only the first copy of the file. You
may also give this option an argument specifying the number of
copy to be extracted. Thus, for example if the archive
‘archive.tar’ contained three copies of file ‘myfile’, then
the command
tar --extract --file archive.tar --occurrence=2 myfile
would extract only the second copy. See section —occurrence, for the description of ‘--occurrence’ option.
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 truly “replace” one member with another. (Replacing one member with another will not work on certain types of media, such as tapes; see Removing Archive Members Using ‘--delete’ and Tapes and Other Archive Media, for more information.)
4.2.2.1 Appending Files to an Archive | ||
4.2.2.2 Multiple Members with the Same Name |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 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-r--r-- me/user 28 1996-10-18 16:31 jazz -rw-r--r-- me/user 21 1996-09-23 16:44 blues -rw-r--r-- me/user 20 1996-09-23 16:44 folk -rw-r--r-- me/user 20 1996-09-23 16:44 rock
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 Updating an Archive, for more information.
We describe this use of ‘--append’ here for the sake of
completeness.) 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 replace 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 replaced 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-r--r-- me/user 28 1996-10-18 16:31 jazz -rw-r--r-- me/user 21 1996-09-23 16:44 blues -rw-r--r-- me/user 20 1996-09-23 16:44 folk -rw-r--r-- me/user 20 1996-09-23 16:44 rock -rw-r--r-- 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 replaced by the newer version. You can confirm this by extracting the archive and running ‘ls’ on the directory.
If you wish to extract the first occurrence of the file ‘blues’ from the archive, use ‘--occurrence’ option, as shown in the following example:
$ tar --extract -vv --occurrence --file=collection.tar blues -rw-r--r-- me/user 21 1996-09-23 16:44 blues
See section Changing How tar
Writes Files, for more information on ‘--extract’ and
see –occurrence, for a description of
‘--occurrence’ option.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In the previous section, you learned how to use ‘--append’ 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’).
Unfortunately, you cannot use ‘--update’ with magnetic tape drives. The operation will fail.
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. See section Multiple Members with the Same Name, for a detailed discussion.
4.2.3.1 How to Update an Archive Using ‘--update’ |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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).
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 that writing to the middle of a section of tape is a difficult
process. Tapes are not designed to go backward. See section 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 Performing Backups and Restoring Files.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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’, give the first archive with
‘--file’ option and name the rest of archives to be
concatenated on the command line. The members, and their member
names, will be copied verbatim from those archives to the first
one(11).
The new, concatenated archive will be called by the same name as the
one given with the ‘--file’ option. As usual, if you omit
‘--file’, tar
will use the value of the environment
variable TAPE
, or, if this has not been set, the default archive 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 rock $ 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-r--r-- melissa/user 105 1997-01-21 19:42 blues -rw-r--r-- melissa/user 33 1997-01-20 15:34 rock $ tar -tvf jazzfolk.tar -rw-r--r-- melissa/user 20 1996-09-23 16:44 folk -rw-r--r-- melissa/user 65 1997-01-30 14:15 jazz
We can concatenate these two archives with tar
:
$ tar --concatenate --file=bluesrock.tar jazzfolk.tar
If you now list the contents of the ‘bluesrock.tar’, you will see that now it also contains the archive members of ‘jazzfolk.tar’:
$ tar --list --file=bluesrock.tar blues rock folk jazz
When you use ‘--concatenate’, the source and target archives must
already exist and must have been created using compatible format
parameters. Notice, that tar
does not check whether the
archives it concatenates have compatible formats, it does not
even check if the files are really tar archives.
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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can remove members from an archive by using the ‘--delete’
option. Specify the name of the archive with ‘--file’
(‘-f’) and then specify the names of the members to be deleted;
if you list no member names, nothing will be deleted. The
‘--verbose’ option will cause tar
to print the names
of the members as they are deleted. As with ‘--extract’, 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 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 $ tar --delete --file=collection.tar blues $ tar --list --file=collection.tar folk jazz rock
The ‘--delete’ option has been reported to work properly when
tar
acts as a filter from stdin
to stdout
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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
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 latter goal, see Verifying Data as It is Stored.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] |
This document was generated on August 23, 2023 using texi2html 5.0.