UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: talkChapter 2
Unix Commands
Next: Reference: tbl
 

tar

tar [options] [files]

Copy files to or restore files from tape (tape role=boldarchive). If any files are directories, tar acts on the entire subtree. (See also cpio and pax.)

Options are supplied as one group, with any arguments placed afterward in corresponding order. Originally, tar did not even accept a leading - on its options. Although the Solaris version allows one, it does not require it. On many other Unix systems, you may use conventional option notation, with each option preceded by a dash and separated from the other options with whitespace. Some systems actually require the use of separate options. Check your local documentation for the final word.

Notes

For the following reasons, tar is best used as a way to exchange file or source code archives over a network. A system administrator performing system backups is advised to use the vendor-supplied backup program (typically called dump or backup; see your local documentation) for backups instead of tar. (Many of these same points apply to cpio and to pax as well.)

  • Most Unix versions of tar preserve the leading / from an absolute filename in the archive. This makes it difficult or impossible to extract the files on a different system.

  • The tar archive format was designed when Unix file and directory names were short (14 characters maximum). Modern Unix systems allow individual filenames to be up to 255 characters in length, but the tar archive header has a limit of 100 characters for the entire pathname. This makes it difficult or impossible in practice to archive a typical Unix filesystem.

  • In general, Unix versions of tar cannot recover from data errors, which are particularly common with tapes. An early tape error can render an entire tar tape useless.

  • While tar does checksum the header information describing each archived file, it does not checksum the actual data blocks. Thus, if a data block becomes corrupted on a tape, tar will never notice.

The GNU version of tar has extensions to get around many of these problems, at the cost of portability of the archive format to non-GNU versions. Source code can be obtained from the Free Software Foundation (http://www.gnu.org).

Control Options (Solaris)

-C dir files

Change directory to dir before adding files to the archive. Use relative pathnames. This option makes it possible to archive files that don't share a common ancestor directory.

-I file

Read a list of filenames to be archived, one filename per line, from file. Useful when there are too many files to name on the command line.

-X

Exclude files. The corresponding file argument is read for a list of relative pathnames, one per line, of files that should not be archived. This option may be provided multiple times with multiple files. Filenames that appear here are excluded even if the same name was provided in a file used with -I.

Function Options (choose one)

c

Create a new archive.

r

Append files to archive.

t

Table of contents. Print the names of files if they are stored on the archive (if files not specified, print names of all files).

u

Update. Add files if not in archive or if modified.

x

Extract files from archive (if files not specified, extract all files).

Options

b n

Use blocking factor n (default is 1; maximum is 20). Different Unix systems often allow larger blocking factors.

B

Continue reading until logical blocks are full. For use across Ethernet connections with rsh. On by default when reading standard input. Solaris only, but also common on many other Unix systems.

e

Exit immediately upon unexpected errors. Solaris only.

E

Use an extended header that allows longer filenames, larger files, and other extensions. Not portable. Solaris only.

f arch

Store files in or extract files from archive arch; arch is usually a device name (default varies from system to system). If arch is -, standard input or output is used as appropriate (e.g., when piping a tar archive to a remote host).

F, FF

With F, do not archive SCCS and RCS directories. With FF, also exclude files named a.out, core, errs, and all .o files. Solaris only.

i

Ignore directory checksum errors. Solaris only.

k size

Specify the archive size in kilobytes. Archives that are larger than size are split across volumes. Useful for fixed-size media, such as floppy disks. Solaris only.

l

Print error messages about links that can't be found.

L

Follow symbolic links. SVR4 only.

m

Do not restore file modification times; update them to the time of extraction.

n

Archive is not a tape device. This allows tar to seek, instead of doing sequential reads, which is faster. Solaris only.

o

Change ownership of extracted files to that of user running program. This is the default for nonprivileged users.

p

Preserve permissions of extracted files. Solaris ACLs are restored if recorded in the archive and are added to the archive when used with c.

P

Do not add a trailing / to directory names in the archive. Solaris only.

v

Print function letter (x for extraction or a for archive) and name of files. With t, print a listing similar to that of ls -l.

w

Wait for user confirmation (y).

n[c]

Select tape drive n and use speed c. n is 0-7 (default is 0); c is l (low), h (high), or m (medium, the default). Used to modify arch. (These are highly system-specific and non-portable: it is much better to always just specify the arch explicitly.)

Examples

Create an archive of /bin and /usr/bin (c), show the command working (v), and write on the tape in /dev/rmt/0:

tar cvf /dev/rmt/0 /bin /usr/bin

List the archive's contents in a format like ls -l:

tar tvf /dev/rmt/0

Extract the /bin directory:

tar xvf /dev/rmt/0 /bin

Create an archive of the current directory, and store it in a file /tmp/backup.tar on the system. (Backing up a directory into a file in that directory almost never works.)

tar cvf /tmp/backup.tar .

Similar, but compress the archive file:

tar cvf - . | compress > /tmp/backup.tar.Z

(The - tells tar to store the directory on standard output, which is then redirected through the pipe.)

Copy a directory tree from one location to another:

# cd olddir; tar cf - . | (cd newdir; tar xvpf -)


Previous: Reference: talkUNIX in a Nutshell: System V EditionNext: Reference: tbl
Reference: talkBook IndexReference: tbl

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System