Next: , Previous: extracting files, Up: extract


2.8.3 Extracting Files that are Directories

Extracting directories which are members of an archive is similar to extracting other files. The main difference to be aware of is that if the extracted directory has the same name as any directory already in the working directory, then files in the extracted directory will be placed into the directory of the same name. Likewise, if there are files in the pre-existing directory with the same names as the members which you extract, the files from the extracted archive will replace the files already in the working directory (and possible subdirectories). This will happen regardless of whether or not the files in the working directory were more recent than those extracted (there exist, however, special options that alter this behavior see Writing).

However, if a file was stored with a directory name as part of its file name, and that directory does not exist under the working directory when the file is extracted, tar will create the directory.

We can demonstrate how to use --extract to extract a directory file with an example. Change to the practice directory if you weren't there, and remove the files folk and jazz. Then, go back to the parent directory and extract the archive music.tar. You may either extract the entire archive, or you may extract only the files you just deleted. To extract the entire archive, don't give any file names as arguments after the archive name music.tar. To extract only the files you deleted, use the following command:

     $ tar -xvf music.tar practice/folk practice/jazz
     practice/folk
     practice/jazz

If you were to specify two --verbose (-v) options, tar would have displayed more detail about the extracted files, as shown in the example below:

     $ tar -xvvf music.tar practice/folk practice/jazz
     -rw-r--r-- me user     28 1996-10-18 16:31 practice/jazz
     -rw-r--r-- me user     20 1996-09-23 16:44 practice/folk

Because you created the directory with practice as part of the file names of each of the files by archiving the practice directory as practice, you must give practice as part of the file names when you extract those files from the archive.