Unix Pax to the Rescue - Extracting Absolute Path Names from tar Archives
Dealing with tar archives containing absolute path names can range from just annoying to potentially dangerous - unwittingly uncompressing the archive may range from silently growling at your simpleton co-worker who created the archive to sudden gut wrenching realization that you just overwrote some important files in unintended locations. Needless to say that creating tar archives with absolute path names is just a bad practice. Solution to this practice is usually to yell at the poor dope who built the archive and to teach him not to do it again. But what if you really need to get to the contents of the doggone archive without going though the contortions of creating dummy directory hierarchies to accommodate the files that are being extracted? The solution to this conundrum is to resort to a powerful but vastly under appreciated Unix tool called pax. The Unix pax (pax means 'peace' in Latin by the way) utility can support a number of archive format (tar and cpio included) and can perform one notably wonderful function of manipulating the paths of the restored files, which as you can imagine is our way of the ugly tar conundrum. All you have to is to provide a sed type expression to the -s command line switch of pax command, which will perform the path name transformation for you. For instance you may have a tar archive that has '/usr/local' as the part of file name that is getting in the way and you would really prefer not to overwrite things under /usr/local while extracting this archive, pax makes it very easy:
# pax -r -s ':^/usr/local::' < doggone_archive.tar
All we have done above is provided a sed type expression that modified all paths starting with /usr/local (aka ^/usr/local) and replaced them with null string (empty space between the ':' delimiters). Solving problems is easy when you have the right tools.
Pax vobiscum

1 comments:
tar: how to exclude subdirectories?
If i have a bunch of directories that i normally backup with this:
tar cvhf /dev/rmt/0 /export/home
How can i exclude certain subdirectories under the /export/home?
tar cvhf /dev/rmt/0 /export/home | grep -v 'test' ? will that exclude anything named test, and any subdirectories under test?
===============================================
You'll have to read the man page to be sure, merlin.
If you have GNU tar, you should be able to use the "-X" (uppercase) option. If you are using a different tar, such as in HP-UX, you probably won't have that option.
You might be able to do it the hard way though...
Code:
find /etc /usr /blah -print > /tmp/tar.list 2>/dev/null
vi /tmp/tar.list # remove the files manually...
tar cf /dev/rmt/0m `cat /tmp/tar.list`
naturebios.com
Post a Comment