Using tar
List of Topics Covered
- Purpose
- Syntax
- creating an archive of files on tape:
- table of contents of a tar file
- extracting files from a tar tape
- Annotated Examples
- Additional Topics
- Using Tar on Disk
- Adding Tar Archives to a Tape
- Tar Script and Background Processing
- Tar and Wildcards (*) in Creating and Extracting Archives
- Gnu Tar
- Related Topics
Purpose
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).
Syntax
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.
- CREATING an archive of files on tape:
Host% tar -cf $RMT . c create the archive f file ($RMT) where archive is to be written . files and sub-directories in current directory
Example beginning with the tape mount:
host% tmr -v mytape -r host% tar -cf $RMT . host% exit
tmr (tape mount request) asks for a tape named (-v) 'mytape' to be mounted write enabled (-r). When mounted tar creates (-c) and archive on file (-f) tape drive ($RMT). All files and sub-directories below this point in the tree are copied (.) along with the directory structure. Note that if you forget the "." you won't get any error messages but tar will fail.
CAUTION: Use relative file names.
Tar restores files with the exact filename stored. Be careful when archiving files from a temporary disk or disk you may not own or have access to in the future! Make sure to use relative path names such as 'my.file' rather than absolute pathnames such as '/sm4/joeuser/my.file' when you archive the files!The cd command: If you store files from a temporary (e.g.chkout) directory use the cd change directory command to move to the directory to be archived, before issuing the tar command:
host% tmr -v mytape -r host% cd /sm3/myid host% tar -cvf $RMT . host% exit
The files will be named something like './any.file' rather than '/sm3/myid/any.file' and tar will be able to create the file.
OR the tar -C option: You can use the -C option (case important) to change directories when creating an archive:
tar cf $RMT -C /usr/include File1 File2 -C /etc File3 File4
Below "joe" backs up all the files (.) in his home directory (~joe) and chkout directory (/sm4/joe):
Host% tar -cvf $RMT -C ~joe . -C /sm4/joe/ .
- TABLE of contents: getting a table (t) of contents of
a tar archive
Host% tar -tvf $RMT t table of contents v verbose (with t produces a more detailed list) f file ($RMT) which contains archive
Example beginning with tape mount:
host% tmr -v mytape host% tar -tvf $RMT
or to write tape archive contents to a file:
host% tar -tvf $RMT > mytape.tarlist host% exit
This lists all of the tar files on tape device $RMT.
- EXTRACTING files from a tar tape:
Host% tar -xf $RMT or Host% tar -xf $RMT <subdirectory> or Host% tar -xf $RMT <fully-specified-file-name>
Examples:
host% tmr -mytape host% tar -xf $RMT
or
host% tar -xf $RMT ./myprograms
or
host% tar -xf $RMT ./my.file host% exit
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.
- 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)
- Store all files in the current directory onto tape:
tmr -v mytape -r tar -cf $RMT . exit
- 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
- 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 directory 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 /Make
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:
#tarit tmr -v mytape -r -I <<endofjob cd Fortran tar -cvf \$RMT . > mytape.tarlist endofjob
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.
- Creating a tar archive of a sub-set:
A command placed between back graves (`) will expand on the command line of another command. The ls and find commands will accept wildcards and list sub-sets of files. For example, these two tar commands:
host% tar -cf t.tar `ls *.log` host% tar -cf t.tar `find *.log -print`
both expand to become:
host% tar -cf t.tar gtestit.log test.log
The ls or find command, placed between back graves, replaces the file specification on the tar command and expands to a list of files.
Sample session:
unix:~> ls *.log gtestit.log test.log unix:~> tar -cvf t.tar `ls *.log` a gtestit.log 4 blocks. a test.log 22 blocks. unix:~> tar -cvf t.tar `find *.log -print` a gtestit.log 4 blocks. a test.log 22 blocks. unix:~> tar -tvf t.tar -rw-r--r-- 4018 204 1985 Jan 13 11:58:52 1995 gtestit.log -rw-r--r-- 4018 204 10819 Jan 11 13:43:41 1995 test.log
- Extracting a tar archive sub-set:
Extracting a sub-set of files from tape requires two steps:
- Get a list of files (tar -t) filtered using the
grep command for file pattern matching and redirect
the results to a file.
host% tar -tf $RMT | grep '.log' > a.a
- Write (cat) the list of files in place of the
file specication on the tar command using back grave
expansion:
host% tar -xvf $RMT `cat a.a`
A sample script file to do this:
#mount a tape, list it through grep to select files, then extract tmr -v mytape <<endofjob cd /sm4/mytape tar -tf \$RMT | grep '\.log' > a.a tar -xvf \$RMT \`cat a.a\` endofjob
Note that the back slash (\) is used to protect Unix special characters ($) and (.) from expansion until the script executes, not when submitted.
- Get a list of files (tar -t) filtered using the
grep command for file pattern matching and redirect
the results to a file.
Gnu 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