[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Creating an archive is only half the job—there is no point in storing files in an archive if you can’t retrieve them. The act of retrieving members from an archive so they can be used and manipulated as unarchived files again is called extraction. To extract files from an archive, use the ‘--extract’ (‘--get’ or ‘-x’) operation. As with ‘--create’, specify the name of the archive with ‘--file’ (‘-f’) option. Extracting an archive does not modify the archive in any way; you can extract it multiple times if you want or need to.
Using ‘--extract’, you can extract an entire archive, or specific files. The files can be directories containing other files, or not. As with ‘--create’ (‘-c’) and ‘--list’ (‘-t’), you may use the short or the long form of the operation without affecting the performance.
2.8.1 Extracting an Entire Archive | ||
2.8.2 Extracting Specific Files | ||
2.8.3 Extracting Files that are Directories | ||
2.8.4 Extracting Archives from Untrusted Sources | ||
2.8.5 Commands That Will Fail |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To extract an entire archive, specify the archive file name only, with no individual file names as arguments. For example,
$ tar -xvf collection.tar
produces this:
-rw-r--r-- myself/user 28 1996-10-18 16:31 jazz -rw-r--r-- myself/user 21 1996-09-23 16:44 blues -rw-r--r-- myself/user 20 1996-09-23 16:44 folk
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To extract specific archive members, give their exact member names as arguments, as printed by ‘--list’ (‘-t’). If you had mistakenly deleted one of the files you had placed in the archive ‘collection.tar’ earlier (say, ‘blues’), you can extract it from the archive without changing the archive’s structure. Its contents will be identical to the original file ‘blues’ that you deleted.
First, make sure you are in the ‘practice’ directory, and list the files in the directory. Now, delete the file, ‘blues’, and list the files in the directory again.
You can now extract the member ‘blues’ from the archive file ‘collection.tar’ like this:
$ tar --extract --file=collection.tar blues
If you list the files in the directory again, you will see that the file
‘blues’ has been restored, with its original permissions, data
modification times, and owner.(1) (These parameters will be identical to those which
the file had when you originally placed it in the archive; any changes
you may have made before deleting the file from the file system,
however, will not have been made to the archive member.) The
archive file, ‘collection.tar’, is the same as it was before you
extracted ‘blues’. You can confirm this by running tar
with
‘--list’ (‘-t’).
Remember that as with other operations, specifying the exact member name is important (See section Commands That Will Fail, for more examples).
You can extract a file to standard output by combining the above options with the ‘--to-stdout’ (‘-O’) option (see section Writing to Standard Output).
If you give the ‘--verbose’ option, then ‘--extract’ will print the names of the archive members as it extracts them.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 section Changing How tar
Writes Files).
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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Extracting files from archives can overwrite files that already exist. If you receive an archive from an untrusted source, you should make a new directory and extract into that directory, so that you don’t have to worry about the extraction overwriting one of your existing files. For example, if ‘untrusted.tar’ came from somewhere else on the Internet, and you don’t necessarily trust its contents, you can extract it as follows:
$ mkdir newdir $ cd newdir $ tar -xvf ../untrusted.tar
It is also a good practice to examine contents of the archive before extracting it, using ‘--list’ (‘-t’) option, possibly combined with ‘--verbose’ (‘-v’).
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are some sample commands you might try which will not work, and why they won’t work.
If you try to use this command,
$ tar -xvf music.tar folk jazz
you will get the following response:
tar: folk: Not found in archive tar: jazz: Not found in archive
This is because these files were not originally in the parent directory ‘..’, where the archive is located; they were in the ‘practice’ directory, and their file names reflect this:
$ tar -tvf music.tar practice/blues practice/folk practice/jazz
Likewise, if you try to use this command,
$ tar -tvf music.tar folk jazz
you would get a similar response. Members with those names are not in the archive. You must use the correct member names, or wildcards, in order to extract the files from the archive.
If you have forgotten the correct names of the files in the archive, use tar --list --verbose to list them correctly.
To extract the member named ‘practice/folk’, you must specify
$ tar --extract --file=music.tar practice/folk
Notice also, that as explained above, the ‘practice’ directory will be created, if it didn’t already exist. There are options that allow you to strip away a certain number of leading directory components (see section Modifying File and Member Names). For example,
$ tar --extract --file=music.tar --strip-components=1 folk
will extract the file ‘folk’ into the current working directory.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] |
This document was generated on August 23, 2023 using texi2html 5.0.