UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 19.6 GNU tar Sampler Chapter 19
Creating and Reading Archives
Next: 19.8 Problems with Verbose tar
 

19.7 Extracting Files from a Compressed Archive

Article 19.5, discussed creating and unpacking compressed tar archives with gzip, gunzip (24.7), and tar. This article explains how to simplify those two-step processes. The first command line below creates a gzipped archive; the second extracts it:

% tar cf - pathnames | gzip > archive.tar.gz
% gzcat archive.tar.gz | tar xf - pathnames

gzcat uncompresses a file (the gzip format, as well as older compress and pack formats), sending the result to standard output. With the f option, and - (13.13) listed as a filename, tar writes to standard output when creating an archive and reads from standard input when extracting. You don't need to create the larger, uncompressed file; you can store the archive permanently in its compressed form. To extract only some of the files in the archive, give the pathnames on the command line exactly as they're stored in the archive. Otherwise, tar will extract all the files. (For a list of the exact pathnames, use tar tf -.)

GNU tar makes this even easier. As article 19.6 shows, the GNU z option creates or extracts a gzipped archive directly. Here are the two examples above using the z option:

% tar czf archive.tar.gz pathnames
% tar xzf archive.tar.gz pathnames

Warning! Instead of cf, you can use cvf so tar will list each file as it's processed.

NOTE: If you extract files from an archive that you didn't create, the files you extract may not belong to you. Here's why. On many non-BSD systems, when tar extracts a file, the file will be owned by the same UID (38.3) that owned the file when the archive was created. If that UID isn't yours, tar may extract directories you can't modify and files you can't edit. On systems with that problem, you can add the o option (for example, tar xof) to be sure that files extracted will belong to you.

- ML, JIK, JP


Previous: 19.6 GNU tar Sampler UNIX Power ToolsNext: 19.8 Problems with Verbose tar
19.6 GNU tar Sampler Book Index19.8 Problems with Verbose tar

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System