A Quick Guide to Using Tar 2004 Nov wfw@sdsu The unix tar command is very useful, for not only making tape archives, but also for combining many files into one file to make life easier. For example, you can save all the files in a directory (or even directory tree) into one single tar file (sometimes called a "tarball"). While the tar command has many options and can do many things, the basic useage is very simple. All you need to do is write to, and read from, a tar file. To Create A tar File ____________________ To save files into a tar file, use the following syntax: unix> tar cvf output.tar input Note that on some flavors of unix, you have to put in the "-" character explicitly in front of the "cvf" flags: unix> tar -cvf output.tar input For example, the following combines three files into one tar file called "combine.tar": unix> tar cvf combine.tar input_file1 input_file2 input_file3 Since you can use wildcards, you can do things like: unix> tar cvf my-data.tar *.fits to save all files with the extension ".fits" into a single file. The most extreme case is to safe everything in the current directory and all its subdirectories: unix> tar cvf my-run.tar . Don't forget the dot "." at the end! It is better to use the "." than the "*" wildcard symbol for tar, since you do NOT want to preseve the full directory path. To Extract Files from a tar File ________________________________ To read a tar file is very easy. Just type the following: unix> tar xvf file.tar This will unpack the tar file and write all the files to the current directory. If there are subdirectories in the tar file, those are created in the current directory. * Caution: If you extract a file from a tar file and it has the same name as a file you already have, it will overwrite your file! So as is usual with unix, be careful. When opening up a tar file, it is best to do so in a clean, empty directory. Also, always use the extension .tar for a tar file. Compressing a tar File ______________________ Tar simply combines files into a new file. It does not compress them. To compress the tar file - with no loss of information! - use the gnu zip command: unix> gzip file.tar This will add the suffix .gz to the file, denoting it as a gzipped file. To uncompress use: unix> gunzip file.tar.gz On linux and some flavors of unix, you can combine the compress and tar operations into one command. The following creates a compressed tar file containing all fortran files: unix> tar zcvf mycode.tar *.for *.f Notice the "z" before the "cvf" flags. The following uncompresses and extracts the tar files: unix> tar zxvf mycode.tar.gz Again, be careful that you don't overwrite existing files. Writing to a Tape _________________ To the unix operating system, a tapedrive is just like a file. So to write to a tape, all you have to do is specify the tapedrive name. For example, to save all files in the current directory and below it: unix> tar cvf /dev/nrst13 . where "/dev/nrst13" is the name of the tapedrive. The tapedrive name will change as it depends on the computer you are on. For example, on vega the name is: "/dev/nrst13", but if could be called "/dev/st0" on another computer. To read a tape, go to an empty directory and type: unix> tar xvf /dev/nrst13 To make a listing of what is on a tape, but not actually write the files to disk: unix> tar tvf /dev/nrst0 . * Note: The above is the barebones usage of tar. For a real archive (not just a backup), you want to make very sure the tape is ok. Then you should us the "verify" options to rewind and compare each file on the tape with the files on disk. See the "info" or "man" file for help. * Caution: Putting multiple tar files on a single tape is a bad idea. Some tapedrives can't read a second tar file on a tape (something about an end-of-file character being treated as the end-of-tape). So put one and only one tar file per tape. It is a bit of a nuisance when making archive tapes, but its much better than losing your files!! There are several commands to control the tapedrive. The most useful are: unix> mt -f /dev/st0 status unix> mt -f /dev/st0 rewind unix> mt -f /dev/st0 offline Use the first to check the health of the tape/tapedrive. If there are errors, don't trust the tar file. The last "mt" command will rewind and eject the tape. And don't forget to set the "write protect" tab on the back of the tape shell. Unlike other O/S or programs, you cannot allocate a tapedrive.