Next: , Previous: extracting archives, Up: extract


2.8.2 Extracting Specific Files

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. tar --extract --file=bfiles.tar birds will fail, because there is no member named birds. To extract the member named ./birds, you must specify tar --extract --file=bfiles.tar ./birds. If you don't remember the exact member names, use --list (-t) option (see list). You can also extract those members that match a specific globbing pattern. For example, to extract from bfiles.tar all files that begin with ‘b’, no matter their directory prefix, you could type:

     $ tar -x -f bfiles.tar --wildcards --no-anchored 'b*'

Here, --wildcards instructs tar to treat command line arguments as globbing patterns and --no-anchored informs it that the patterns apply to member names after any ‘/’ delimiter. The use of globbing patterns is discussed in detail in See wildcards.

You can extract a file to standard output by combining the above options with the --to-stdout (-O) option (see Writing to Standard Output).

If you give the --verbose option, then --extract will print the names of the archive members as it extracts them.


Footnotes

[1] This is only accidentally true, but not in general. Whereas modification times are always restored, in most cases, one has to be root for restoring the owner, and use a special option for restoring permissions. Here, it just happens that the restoring user is also the owner of the archived members, and that the current umask is compatible with original permissions.