Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

tar - file archiver

&pagelevel(4)&pagelevel

The tar command is used to process archive files. Archive files can only be placed on the POSIX file system, not on magnetic tapes and cartridges.

The actions to be performed are defined by the mainkey operand.

The tar command has been withdrawn from the XPG4 standard and replaced by the command pax.

Consequently, only the pax command should be used in the future, since tar is now supported only for compatibility reasons.


Syntax


tar mainkey[ modifier...][ file]
Main keys

mainkey consists of a “function character”, which may be specified with or without a hyphen (-).

It may be immediately followed by one or more additional options called modifiers (see "tar file archiver").

mainkey can be one of the following characters:

[- ] c

(c- create)

A new archive is created. The specified file or files are written at the start of the archive. 

[- ] r

(r - replace)

The specified file or files are appended to the end of the archive.

[- ] u

(u - update)

The specified file or files are added to the archive if not already present in it or if the last modification time of the file to be archived is later than that of the archived file. 

[- ] t

(t - table)

tar prints a table, showing the contents of the archive.

If one or more existing files from the archive files are specified, the names of the specified file or files are written to standard output.

If no files are specified, the names of all files located in the archive are written to standard output.

[- ] x

(x - extract)

The specified file or files are extracted from the archive.

Files that are stored in the archive with relative pathnames are extracted to thecurrent directory.

Files that are stored in the archive with absolute pathnames are extracted to the corresponding directory (if such a directory exists).

If the specified file is a directory, all subdirectories contained in it are extracted recursively.

If the specified file does not exist in the directory in which the extracted file is to be placed, it is created as a new file with the following attributes:

  • The user ID and group ID of the owner and the last modification time are taken from the archived file.

  • Access permissions are set as defined with umask.
    The s-bit is taken into account only if tar is called by a user with system administrator privileges.

If the file already exists, the access permissions are not changed.

If one or more files of the same name already exist in the archive, the last extracted file will overwrite all such files.

Modifiers

The characters listed below can be directly appended to the main key as a modifier, provided the following rules are observed:

  • First enter all modifiers without intervening blanks.

  • Then enter the individual arguments, which must be separated by blanks. The order of the arguments is determined by the order in which the associated modifiers were entered.


+v

(v - verbose)

The name of each processed file is written to standard error. Each name is preceded by one of the characters below, which indicates the type of processing:

  • a for the main keys c, r and u, or

  • x for the main key x.

The individual file names are then followed by the corresponding file size (in blocks).

If the modifier v is used with the mainkey [-]t , the access permissions and the owner and group of the file are also displayed.

w

(w -what)

For each file, tar waits for a confirmation before executing the corresponding function specified with mainkey. tar displays the following:

mainkey filename:

and expects you to enter your input after the colon. It is only after you enter your confirmation (with y or j, depending on the language) that the action will be executed.

If w is not specified, the requested actions are performed without confirmation.

f

(f - file)

The first specified file operand (or the second if b has already been specified) is interpreted as the name of the archive to be processed. 

If the specified name is '-' (a hyphen), the archive will be written to standard output or read from standard input. This makes it possible to use tar at both ends of a command pipe:

  • If f is specified with the main keys -c, -r or -u and the associated file operand is '-', the archive is written to standard output.

  • If f is specified in connection with the mainkey -t or -x and the associated file operand is '-' (a hyphen), the archive is read from standard input.

If f is not specified, the default archive file defined by the shell variable TAPE is used.

h

tar follows symbolic links.

If h is omitted, tar will process only the symbolic link, but not the associated file.

l

(l - link )

tar issues a message when a reference to another file cannot be resolved.

If l is omitted, no messages displayed.

m

(m - modification date/time)

(modification date/time) On copying the specified file from the archive, tar sets the time of last modification for the copy to the current date and time.

If m is omitted, the original date and time that was recorded in the archive are retained.

o

(o - owner)

The file copied from the archive is assigned the same owner as the user who called tar. The file is also assigned the group of that user.

If o is omitted, the original owner and group that was recorded in the archive are retained.

file

Pathname of a file or directory that is to be written to the archive (mainkey -c,-r or -u), read from an archive (mainkey -x), or listed (mainkey -t).

If file is the name of a directory, the corresponding actions are applied (recursively) on all files and subdirectories of that directory.

If the modifier f is specified, the first file operand is interpreted as the archive name.

Variable

TAPE

Determines the name of the archive to be used if the modifier f is not specified.

Locale

The following environment variables affect the execution of tar:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_COLLATE

Determine the collating sequence.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

Create an archive file:
All files contained in the current directory (.) and its subdirectories are to be written to the archive file $HOME/archive/example1.TAR.

$ tar cvf $HOME/archive/example1.TAR .
a ./.sh_history 8 Blocks

a ./.profile 1 Blocks

a ./test.proc 1 Blocks

a ./pos_examp/file1 1 Blocks

a ./pos_examp/file2 1 Blocks

a ./pos_examp/file3 8 Blocks

tar: ./example1.TAR : same as archive file

Example 2

List the contents of an archive file on standard output:

$ tar tvf $HOME/archive/example1.TAR

Tar: blocksize = 20

USTAR format archive

rw------- 632/-993   3866 May 26 18:12 2008, ./.sh_history

rwxr-xr-x 632/-993     48 Mar 20 12:40 2008, ./.profile
rw-r--r-- 632/-993     29 Apr 23 13:31 2008, ./test.proc

rw-r--r-- 632/-993     29 May 26 18:05 2008, ./pos_examp/file1

rw-r--r-- 632/-993     52 May 26 18:07 2008, ./pos_examp/file2
rw------- 632/-993   3602 May 26 18:07 2008, ./pos_examp/file3

Example 3

Read files from an archive file of another user into the current directory:

The files stored in the archive file have a different user ID and group ID than that of the user who calls the tar command. On reading the files from the archive, this user also wants the file ownership to be changed to his/her user ID and group ID, respectively:

(1) $ id
    uid=3010(VSC0) gid=3030(VSCG0)
(2) $ tar tvf /home/vsx/vsx1/km/example.TAR
    Tar: blocksize = 20
    USTAR format archive
    rw-r--r-- 3001/3002    883 May 28 13:29 2008, ./pos_examp/file1 
    rw-r--r-- 3001/3002   1766 May 28 13:30 2008, ./pos_examp/file2
    rw-r--r-- 3001/3002   2649 May 28 13:30 2008, ./pos_examp/file3
(3) $ tar xvfo    /home/vsx/vsx1/km/example.TAR
    Tar: blocksize = 20
    USTAR format archive
    tar: . : exists - no update
    x ./pos_examp/file1, 883 bytes, 2 tape blocks
    x ./pos_examp/file2, 1766 bytes, 4 tape blocks
    x ./pos_examp/file3, 2649 bytes, 6 tape blocks
(4) $ ls -lR
    total 8
    drwxr-xr-x   2 VSC0     VSCG0       2048 May 28 13:42
    pos_examp
    ./pos_examp:
    total 24
    -rw-r--r--   1 VSC0     VSCG0        883 May 28 13:29 file1 
    -rw-r--r--   1 VSC0     VSCG0       1766 May 28 13:30 file2
    -rw-r--r-- 1 VSC0 VSCG0 2649 May 28 13:30 file3

(1)

The user VSC0 begins by displaying his/her own user ID and group ID.

(2)

A table of contents of the archive file example.TAR, which is owned by some other user, is displayed. This indicates that the files and directories in the archive have a different user ID and group ID than that of the user VSC0.

(3)

The user VSC0 sets the -o option in the tar command to have his/her user ID and group ID transferred on unpacking the archive file to the current directory.

(4)

To verify that the new user ID and group ID have actually been entered, the user VSC0 issues the ls command.

Hint: Exchanging tar archives between Unix systems and BS2000

In Unix systems, tar archives are created in ASCII format. Since POSIX processes files in EBCDIC format by default and no normal ASCII-EBCDIC conversion of archive files is possible, the exchange of archive files can only occur via ASCII file systems. The following two variants briefly explain how this can be achieved:

Variant 1 (with NFS)

The tar archive is stored on the Unix system and can be accessed in POSIX (mounted via NFS). If the shell variable IO_CONVERSION is set to YES, the tar archive can be upacked with automatic conversion to the POSIX file system.

Variant 2 (without NFS)

An ASCII file system must be created in POSIX (see the description for POSIX filesystem marker = n under Install POSIX subsystem in the manual "POSIX Basics for Users and System Administrators" [1]. Setting the shell variable IO_CONVERSION=YES causes the files in such a file system to be treated as ASCII files. The tar archive must now be copied from the Unix system to the POSIX file system by means of a binary transfer. It can then be automatically converted to a POSIX EBCDIC file system when it is unpacked.

See also

ar, cpio, pax