tar - Tape ARchiver

tar is a long standing Unix utility, and as such its sytax is a bit
special. The syntax is:

  tar [options] [files...]

  tar -cvf file.tar /etc       - backs up /etc to file.tar
  tar -cvf file.tar /etc /var  - backs up /etc and /var to file.tar
  tar -xvf file.tar            - restores file.tar with path
  tar -tvf file.tar            - displays an 'index' of file.tar

  tar -cvf - /etc | gzip -9c > file.tar.gz
  gunzip -c file.tar.gz | tar -xvf -
  tar -cvfz file.tar.gz /etc    - tars and gzips /etc to file.tar.gz
  tar -xvfz file.tar.gz         - unzips and untars file.tar.gz

  tar -cvfzM /dev/fd0  - makes a complete backup of system
                         (-M is multivolume)
  tar -xvfzM /dev/fd0  - restores the above backup operation


Now, here is a list of options. Note that all of them have an equivalent
long option, but you will have to refer to the manual page for this as
they won't be listed here. And of course, not all options will be listed
either. Note: the initial dash (-) of short options is not now deprecated
with tar, except after a long option.

  c        - This option is used in order to create new archives.
  x        - This option is used in order to extract files from an existing
             archive.
  t        - List files from an existing archive.
  v        - This will simply list the files are they are added to an archive
             or extracted from an archive, or, in conjunction with the t
             option (see above), it outputs a long listing of file instead
             of a short one.
  f  - Create archive with name , extract from archive  or
             list files from archive . If this parameter is not given,
             the default file will be /dev/rmt0, which is generally the
             special file associated to a streamer. If the file parameter
             is - (a dash), the input or output (depending on whether you
             create an archive or extract from one) will be associated to
             the standard input or standard output.
  z        - Tells tar that the archive to create should be compressed with
             gzip, or that the archive to extract from is compressed with
             gzip.
  y        - Same as z, but the program used for compression is bzip2.
  p        - When extracting files from an archive, preserve all file
             attributes, including ownership, last access time and so on.
             Very useful for filesystem dumps.
  r        - Append the list of files given on the command line to an
             existing archive. Note that the archive to which you want to
             append files should not be compressed!
  A        - Append archives given on the command line to the one submitted
             with the f option. Similarly to r, the archives should not be
             compressed in order for this to work.

There are many, many, many other options, you may want to refer to the
tar(1) for a whole list. See, for example, the d option. Now, on for a
little practice. Say you want to create an archive of all images in
/shared/images, compressed with bzip2, named images.tar.bz2 and located in
your home directory. You will then type:

$ cd /shared
$ tar cyf ~/images.tar.bz2 images/

As you can see, we have used three options here: c told tar that we wanted
to create an archive, y told it that we wanted it compressed with bzip2,
and f /images.tar.bz2 told it that the archive was to be created in our
home directory, with name images.tar.bz2. We may want to check if the
archive is valid now. We can just check this out by listing its files:

$ cd
$ tar tyvf images.tar.bz2

Here, we told tar to list (t) files from archive images.tar.bz2
(f images.tar.bz2), warned that this archive was compressed with bzip2 (y),
and that we wanted a long listing (v). Now, say you have erased the images
directory. Fortunately, your archive is intact, and you now want to extract
it back to its original place, in /shared. But as you don't want to break
your find command for new images, you need to preserve all file attributes:

$ cd /shared
$ tar yxpf ~/images.tar.bz2


Now, let's say you want to extract the directory images/cars from the
archive, and nothing else. Then you can type this:

$ tar yxf ~/images.tar.bz2 images/cars

In case you would worry about this, don't: no, if you try to back up special
files, tar will take them as what they are, special files, and will not dump
their contents. So yes, you can safely put /dev/mem in an archive :) Oh, and
it also deals correctly with links, so do not worry for this either. For
symbolic links, also look at the h option in the manpage.