Skip Navigation
 Search | Directories | Reference Tools
UW Home > Discover UW > IT Connect 

Using tar

List of Topics Covered


The purpose of the tar (Tape ARchive) command is to manage an archive of files on tape or disk. Tar stores the structure of directories so that archives of entire directories, complete with sub-directories and files can be created, extracted or a table of contents produced. Tar is used primarily as a backup and transfer tool. Single files or entire directories along with the original structure, can be written to a file or directly to a tape.

Note: *Only* the tar command, or another utility which can read the special tar archive format, can be used to extract or restore files from a tar archive.

This description of tar focuses on saving directories on tape and does not discuss all of the tar options. For details see the tar manual page, man tar (see related topics below).


The syntax of tar command is:

      Host% tar -?vf archive_name files_to_archive
         where ? is *one* of:
         -c  create an archive
         -x  extract an archive
         -t  table of contents
         and optionally:
         -v  verbose diplays a list of files or with -t more detail.
         where archive_name is:
            a  disk filename, usually of the form "name.tar"
            a  tape device, usually  $RMT
         where  files_to_create is:
            .  all files, sub-directories from this part of tree on
               the name of a directory or sub-directory
               a list of files, directories or sub-directories

The - can be omitted (tar cf $RMT .) but is used here in conformity with the syntax for the majority of Unix commands.

The commands above retrieve files from the tar tape and store them according to the original file structure starting in the current directory. The first command retrieves all of the files, the second command retrieves only those in the specified subdirectory, and the third command retrieves only the single specified file.

Note that all tape operations take place within a shell which exists when you invoke tmr and terminates (and releases the tape) when you type exit . Also note that the tape is a special type of file which is represented by the environment variable $RMT (see man tmr for details).

Annotated Examples

These examples use a subdirectory named /Fortran which contains subdirectories: /Loading, /Make, and others.

  1. Create a tape archive of subdirectory /Fortran.
         Command                                     Comments
         ------------------------------    ---------------------------
         tmr -v mytape -r                 "mytape" is the name (vsn)
                                           of your tape, and -r says
                                           to mount it with the WRITE
                                           ring; otherwise the tape is
                                           mounted in write protect mode.
                                           Now you must wait for operator
                                           message: "Tape assigned to Userid"
                                           which indicates that the tape is
                                           ready to use.
         cd ~/Fortran                      Make /Fortran your
                                           current directory (.).
         tar -cf $RMT .                   $RMT is a variable which
                                           represents the tape device
                                           /dev/rmt* which writes at
                                           the drive's highest density.
                                           It writes end of tape and
                                           rewinds after a write operation.
                                         .  The period (.) denotes
                                           the current directory and all
                                           sub-directories and files.
         exit                              Exit the -tmr- shell (this
                                           automatically dismounts
                                           your tape from the drive)
  2. Store all files in the current directory onto tape:
       tmr -v mytape -r
       tar -cf $RMT .
  3. List the contents of your tar file:
       tmr -v mytape                   tape is mounted 'read only'.
       tar -tf $RMT                     List all of the files
                                        on the tar tape.
                                        Notice that the directory
                                        Structure is retained.
                                        The files will be listed
                                        in the format
                                        ./Loading/... etc.
       mt -f $RMT rewind                Rewind the tape

    (Note: rewind is not needed if previous operation used $RMT.)

         tar -tvf $RMT | more             List all of the
                                           files with more extensive (v)
                                           information and pipe the
                                           listing through -more-.
         tar -tf $RMT | grep /Make        List all files in the
                                           /Make subdirectory
         tar -tvf $RMT > mytape.tarlist  Write the contents to a file.
         exit                              Exit the  tmr  shell
                                           and release the tape
  4. Extract all or part of your files:
         tmr -v mytape                     Mount the tape
         tar -xf $RMT                    Extract all files and
                                           directories and write
                                           them to current directory.
         mt -f $RMT rewind               Rewind the tape
           (rewind not needed if last tape operation used $RMT)
         cd Exp                            Move to another
         tar -xf $RMT ./Loading             Extract all
                                           files and directories
                                           contained in the
                                           subdirectory /Loading
                                           and write them to the
                                           current directory (which
                                           is Exp).  The files are
                                           stored in subdirectories
                                           as follows:
                                           In Exp:     /Fortran
                                           In Fortran: /Loading
                                           In Loading:  all the files
                                           and sub-directories
                                           in Loading

Using tar on Disk

You can also create and test tar files on disk, for example:

     cd ~/Fortran
     tar -cf ~/Exp/my.tar .             Create a tar file from
                                       the current subdirectory
                                       (~/Fortran) and write the
                                       tarred file to file "my.tar"
                                       in directory ~/Exp.
     cd ~/Exp

     tar -tf my.tar | more              List the contents of my.tar.

     tar -xpf my.tar ./Make             Extract the subdirectory

Adding Tar Archives To a Tape

The tape drives on some Unix computers do not support the update or addition of files to a tar archive stored on tape. If you want to add files you must add a new archive to the tape.

To add files to a tape which already contains an archive, use the mt command and the no-rewind tape device ($RMT_NR) to position the tape past the archives already stored:

the syntax of the mt command as used here is:

     host% mt -f tape_device  fsf  N

where N is the number of files to be passed:

to move past the first file:

      host% mt -f $RMT_NR fsf 1

to move past a second file to write a third:

      host% mt -f $RMT_NR fsf 2

Example starting from tape mount:

      host% tmr -v mytape -r
      host% mt -f $RMT_NR fsf 1
      host% cd Fortran
      host% tar -cf $RMT .
      host% exit

Here the mt fsf 1 command moves the tape past file one. Then a second tape archive is written using the tape device $RMT which will write end of tape and rewind the tape. The tape now has two separate tar archives. To create the third archive later:

      host% tmr -v mytape -r
      host% mt -f $RMT_NR fs 2
      host% cd Fortran
      host% tar -cf $RMT .
      host% exit

the no rewind tape device $RMT_NR leaves the tape positioned where the mt fsf n command puts it. It writes end of file after a write operation and operates at high density.

Reading from a second archive file on tape:

The mt command is also used to postion the tape to read from the second archive:

      host% tmr -v mytape
      host% mt -f $RMT_NR fsf 1
      host% tar -xf $RMT ./Fortran
      host% exit

See the manual page for the mt command for details on its use:

      host% man mt

TAR Script And Background Processing

You can create a file or script containing tape commands and run the job in the background so you can do other work or logout. Here is an example of a short tar script, called "tarit" to mount a tape, cd to the chosen directory, archive the files and exit.

create the script using any editor:

      host% pico tarit

The file 'tarit' has these lines:

      tmr -v mytape -r -I <<endofjob
      cd Fortran
      tar -cvf \$RMT . > mytape.tarlist

mounts the tape 'mytape' and runs the shell until the word 'endofjob' is encountered. The backslash '\' is used to 'escape' the $ so that it will be interpreted by the tape shell and not when the job is submitted. When running a job in the background its advisable to get a listing of what it did by adding -v (verbose) to the command and piping (>) the output to a file (mytape.tarlist).

Change the permission of the file 'tarit' to be executable:

      host% chmod +x tarit

Then submit the command by typing its name; the ampersand (&) causes the job to run in the backgroun.

       host% tarit &

You can submit a script such as this and logout. The script will wait for a drive if they are busy.

TAR and Wildcard Characters (*) in Creating And Restoring Archives

Wildcards can *not* be used directly in tar commands. The following commands will *fail*:

      host% tar -cf $RMT *.log
      host% tar -xf $RMT *.log

Wildcard functionality can be obtained by combining Unix features with tar.


GNU tar has capabilites not featured in the version of tar which comes with the operating system (/bin/tar). GNU tar is located in: /usr/local/gnu/bin/tar as in:

      host% /usr/local/gnu/bin/tar -cvf $RMT .

Short help is available by typing: host% /usr/local/gnu/bin/tar --help

Related Topics

Help files:

       The tar manual  :  man tar         The tape archive command details
       The mt  manual  :  man mt          the magnetic tape command details

See also the manual pages for other tape backup/archive utilities: man pax, man cpio