Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [238]

By Root 1469 0
if you use the cf function letters. Otherwise tar will overwrite the first file in your list of files to pack because it will mistake that for the filename!

It is often a good idea to use the v option with tar; this lists each file as it is archived. For example:

rutabaga$ tar cvf mt.tar mt

mt/

mt/st_info.txt

mt/README

mt/mt.1

mt/Makefile

mt/mt.c

mt/mt.o

mt/mt

If you use v multiple times, additional information will be printed:

rutabaga$ tar cvvf mt.tar mt

drwxr-xr-x root/root 0 Nov 16 19:03 2004 mt/

-rw-r--r-- root/root 11204 Sep 5 13:10 2004 mt/st_info.txt

-rw-r--r-- root/root 847 Sep 21 16:37 2004 mt/README

-rw-r--r-- root/root 2775 Aug 7 09:50 2004 mt/mt.1

-rw-r--r-- root/root 24 Sep 21 16:03 2004 mt/Makefile

-rw-r--r-- root/root 6421 Aug 7 09:50 2004 mt/mt.c

-rw-r--r-- root/root 3948 Nov 16 19:02 2004 mt/mt.o

-rwxr-xr-x root/root 9220 Nov 16 19:03 2004 mt/mt

This is especially useful because it lets you verify that tar is doing the right thing.

In some versions of tar, f must be the last letter in the list of options. This is because tar expects the f option to be followed by a filename—the name of the tar file to read from or write to. If you don't specify f filename at all, tar assumes for historical reasons that it should use the device /dev/rmt0 (that is, the first tape drive). In "Making Backups," in Chapter 27, we talk about using tar in conjunction with a tape drive to make backups.

Now, we can give the file mt.tar to other people, and they can extract it on their own system. To do this, they would use the following command:

tar xvf mt.tar

This creates the subdirectory mt and places all the original files into it, with the same permissions as found on the original system. The new files will be owned by the user running the tar xvf (you) unless you are running as root, in which case the original owner is preserved. The x option stands for "extract." The v option is used again here to list each file as it is extracted. This produces:

courgette% tar xvf mt.tar

mt/

mt/st_info.txt

mt/README

mt/mt.1

mt/Makefile

mt/mt.c

mt/mt.o

mt/mt

We can see that tar saves the pathname of each file relative to the location where the tar file was originally created. That is, when we created the archive using tar cf mt.tar mt, the only input filename we specified was mt, the name of the directory containing the files. Therefore, tar stores the directory itself and all the files below that directory in the tar file. When we extract the tar file, the directory mt is created and the files placed into it, which is the exact inverse of what was done to create the archive.

By default, tar extracts all tar files relative to the current directory where you execute tar. For example, if you were to pack up the contents of your /bin directory with the command:

tar cvf bin.tar /bin

tar would give the warning:

tar: Removing leading / from absolute pathnames in the archive.

What this means is that the files are stored in the archive within the subdirectory bin. When this tar file is extracted, the directory bin is created in the working directory of tar—not as /bin on the system where the extraction is being done. This is very important and is meant to prevent terrible mistakes when extracting tar files. Otherwise, extracting a tar file packed as, say, /bin would trash the contents of your /bin directory when you extracted it.[*] If you really wanted to extract such a tar file into /bin, you would extract it from the root directory, /. You can override this behavior using the P option when packing tar files, but it's not recommended you do so.

Another way to create the tar file mt.tar would have been to cd into the mt directory itself, and use a command such as:

tar cvf mt.tar *

This way the mt subdirectory would not be stored in the tar file; when extracted, the files would be placed directly in your current working directory. One fine point of tar etiquette is to always pack tar files so that they have a subdirectory at the top level, as we did in the first example with tar cvf mt.tar mt.

Return Main Page Previous Page Next Page

®Online Book Reader