Although we have already seen a use for tar in the chapter "Building and installing free software", we haven't explained how it works. This is what this section is here for. As for find, tar is a long standing Unix utility, and as such its syntax is a bit special. The syntax is:
tar [options] [files...] |
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 :-)
![]() | the initial dash (-) of short options is 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 files instead of a short one;
f <file>: create archive with name <file>, extract from archive <file> or list files from archive <file>. If this parameter is omitted, 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;
j: 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. Similar to r, the archives should not be compressed in order for this to work;
There are many, many, many other options, so you may want to refer to the tar(1) manual page 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:
# # Note: you must be in the directory from which # you want to archive files! # $ cd /shared $ tar cjf ~/images.tar.bz2 images/ |
As you can see, we have used three options here: c told tar that we wanted to create an archive, j 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:
# # Get back to our home directory # $ cd $ tar tjvf 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 (j), 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 to the directory where you want to extract # $ cd /shared $ tar jxpf ~/images.tar.bz2 |
And here you are!
Now, let's say you want to extract the directory images/cars from the archive, and nothing else. Then you can type this:
$ tar jxf ~/images.tar.bz2 images/cars |
In case you would worry about this, don't. 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.