打印

Still more ways to extract from tar files

Still more ways to extract from tar files

Still more ways to extract from tar files
By Sandra Henry-Stocker

In last week's column, I described some methods for extracting files
from uncooperative tar files (in which files are stored with full path
names) along with some of the differences between the way that the tar
command works on various Unix systems. Since that time, a number of
readers have written to suggest some other commands - commands that
allow them to extract the files they want into the directory where they
want them without awkward commands or "tricks" to extract files into
temporary locations and then move them into permanent locations.

The first suggestion is to add the GNU tar command to all systems where
the native tar command does not strip the leading slash. Since many of
us routinely add a selection of GNU commands (such as gzip and gcc) to
the systems we manage, adding one more will probably not seem like much
of a change. In fact, the tar command on some Unix systems (such as
RedHat Linux) is the GNU tar command. Also, if you manage several types
of Unix systems, having the same tar command available on all of them
will make them a bit easier to manage. Just make sure that your GNU bin
is located early in your search path so that you are using the tar
command of choice when you type the command.

The second suggestion is to use the chroot command which changes the
active root of the Unix file system to another directory for the extent
of a particular command. For example, the man page for chroot suggests
that you can copy the tar command into the directory that you would like
to use in extracting your files. Let's say you want to use /var/tmp. If
you first copy the tar command from /usr/sbin/static/tar to your target
directory (this version of the tar command is statically built, so it
will not require libraries from /usr/lib when it is run) and then use a
command of this variety:

# /usr/bin/dd if=file.tar | /usr/bin/chroot /var/tmp ./tar xf -

You will be extracting your files into /var/tmp if the pathnames begin
with slashes. One disadvantage to using chroot is that you have to be
root. The other is that you have to copy the static version of the tar
command and then remove it afterwards if it might get in your way.

If you don't like using dd and piping to the chroot command, you can
move both the tar command and the tar file into your intended directory
and then extract the file's contents with a tar command of this form:

# chroot /var/tmp ./tar rural_photos.tar

Confused about the syntax? In each of these commands, the chroot
command has the form:

chroot <directory> <command>

In the command "chroot /var/tmp ./tar rural_photos.tar", the command
that is run with /var/tmp as its root is "./tar rural_photos.tar".
Viewed in this way, the syntax of the command is fairly easy to
understand and to remember.

The third solution involves a command called pax. Pax stands for
"portable archive interchange" and it provides a surprisingly
straightforward alternative to tar. Here is an example of how files can
be extracted from a tar file while removing the leading slashes at the
same time:

/usr/bin/pax -r -s ',^/,,' -f rural_photos.tar

In this command, we have used the -r (read), -f (file) and -s
(subsitute) arguments to specify that we are extracting the contents of
the rural_photos.tar file. The ',^/,,' argument specifies that each
leading slash (^/) is replaced with nothing (i.e., is removed).

The pax command doesn't require copying files or moving the root
directory and it doesn't require you to be root. The pax command is
available on Solaris and RedHat Linux and may be available on other Unix
systems as well.

=====

Thanks to Paco Benitez, Aidan Doyle, Frank Huang, Nils-Olavi O'Monaghan,
Navrooz Irani, Tim Lavin, William Korb, Gerard Tromp, Ken Smith and
David Hough for comments and suggestions.

________________________________________________________________________________

SPONSORED LINK

EMERGING THREATS: UNDERSTAND THREAT FORECASTING AND PATTERNS OF ATTACK

FREE Webcast shows how to recognize patterns of attacks from around the
world, evaluate vulnerabilities that impact your network, and transfer
threat forecasting knowledge into useable action items and alerts.

http://ui.unixinsider.com/GoNow/a14724a79389a83181667a0

________________________________________________________________________________


About the author(s)
-------------------
Sandra Henry-Stocker has been administering Unix systems for nearly 18
years. She describes herself as "USL" (Unix as a second language) but
remembers enough English to write books and buy groceries. She
currently works for TeleCommunication Systems, a wireless
communications company, in Annapolis, Maryland, where no one else
necessarily shares any of her opinions. She lives with her second
family on a small farm on Maryland's Eastern Shore. Send comments and
suggestions to mailto:sstocker@itworld.com.
________________________________________________________________________________

ITWORLD.COM SERVICES

8 FREE WHITE PAPERS AND E-MAIL UPDATES FROM DELL

Sign up for Dell Small Business email updates and get these FREE White
Papers: Wireless Security in 802.11 (Wi-Fi) Networks, Windows Server
2003 InfoBrief, Securing Linux, Server consolidation, plus 4 more. Go
now.

http://ui.unixinsider.com/GoNow/a14724a79389a83181667a2
________________________________________________________________________________

CUSTOMER SERVICE

CHANGE YOUR E-MAIL ADDRESS
To change your e-mail, go to:
http://reg.itworld.com/servlet/Frs.frs?Script=change_email_v4&Context=LOGIN

For instruction on how to change your e-mail address, go to:
http://www.itworld.com/response/site_support.html#email

NEWSLETTER FAQS
For commonly asked newsletter questions, go to:
http://www.itworld.com/response/site_support.html

UNSUBSCRIBE
For instruction on how to unsubscribe, go to:
http://www.itworld.com/response/site_support.html#unsubnl
________________________________________________________________________________

CONTACTS

* Editorial: Jodie Naze, Director, ITworld.com Site Network,
jodie_naze@accelacommunications.com
* Advertising: Clare O'Brien, Vice President of Sales,
clare_obrien@accelacommunications.com

________________________________________________________________________________

PRIVACY POLICY

ITworld.com has been TRUSTe certified
http://www.itworld.com/Privacy/

ITworld.com is a product of Accela Communications, Inc.

Copyright 2003 Accela Communications, Inc., All Rights Reserved.
http://www.accelacommunications.com

________________________________________________________________________

TOP