tar will simply check every entry in it's archive to see if it matches the pattern you supplied.
If your pattern contains wildcards (* or ?), then you probably want to enclose it in single quotes to prevent the shell from expanding it before passing it to the tar command.
ie
say I have a file 'abc.txt' in the current directory and I want to retrieve all *.txt files from a tar archive.
tar -xvf mytar.tar *.txt will make the shell expand *.txt into "abc.txt" before tar gets to see it. So tar will only look for files that are called "abc.txt" in the archive.
Instead use:
tar -xvf mytar.tar '*.txt'
The single quotes prevent the shell filename expansion.
Secondly, you can put any pattern you like, regardless if it's a directory or just a file.
For instance:
a pattern like '*/*/*.txt' is also allowed and only matches the .txt files that are 3 levels deep.
If you're not sure what you want to extract, do a
Code:
tar -tvf yourtar.tar
first.
tar also extracts the folders by default, which means that no file clobbering can occur.
To extract files/dirs from a large tar, you'll need to be patient. tar was created to read/write on tapes and hence works sequentially. It simply looks through the content list of the entire archive to find any matches of the pattern you specified and will extract all matches found.
If you would put let's say just "abc.txt" (a filename) as last parameter, then tar may extract both
dir1/abc.txt and dir2/abc.txt. But it would create the dir1 and dir2 directories, so you get the 2 extracted files. The first file isn't overwritten by the second one.
Notice also that tar paths are usually relative, unless the -P (or --absolute-names) option was used.
See tar --help for info, especially the section on "local file selection" options.