tar xvf blah.tar > /some/dir
but that was plain old silly. Then I peeked at the manual and couldn't find anything; the closest seemed to be -C flag, but I couldn't quite get it to do what we wanted.
Next I tried hacking the tar header, but that failed too.
Suggesting moving the tar file to some directory, extracting , and moving stuff back up to the new directory wasn't a good solution for him either.
When reading the manual, I noticed the -O flag, which extracts a tarfile to stdout. Mingled among the extracted data are the filenames that are being extracted. Voila, tar_mv.pl was born.
I doubt this can handle binary files very well (haven't tested yet) but it works well on multi-level tarfiles that are text-only.
#!/usr/bin/perl BEGIN { sub usage { print "usage: tar xvfO tarfile.tar |& $0 ", "/path/to/newdir\n"; exit 1; } if ( $^O ne 'linux' ) { print "tar on $^O doesn't support O flag\n\n"; usage(); } -p STDIN or usage(); # make sure we're in a pipe @ARGV == 1 or usage(); $main::newdir = shift(@ARGV); -d $main::newdir or mkdir $main::newdir; } use strict; use warnings; my $fh = (); my $orig_dir = (); while (<>) { chomp; # grab original root dirname from tar header if ( not defined $orig_dir ) { #($orig_dir) = m {^([^/]*)/$} ; # Remove /$ to allow handling sun- & alpha-made tarballs ($orig_dir) = m {^([^/]*)} ; } else { # change root dirname in fileheader to new dirname if ( s{^$orig_dir/([^/]*?)}{$main::newdir/$1}x ) { $fh = get_newfh($_); next unless defined $fh; # Justin Case } # print extracted data else { print $fh $_, $/; } } } sub get_newfh { my ($file) = shift(@_); # silly "open or do {...}" to handle subdirs in tar file open OUT, ">$file" or do { if ( $! =~ /is a directory/i) { mkdir $file unless -d $file } else { die "$file: $!\n"; } }; \*OUT; }
Suggestions/glaring bugs?
UPDATE
blyman
setenv EXINIT 'set noai ts=2'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: tar_mv: move tar data as it's being extracted
by mojotoad (Monsignor) on Jul 26, 2002 at 22:44 UTC | |
by mem (Acolyte) on Jul 30, 2002 at 07:11 UTC | |
by belden (Friar) on Jul 31, 2002 at 20:52 UTC | |
|
Re: tar_mv: move tar data as it's being extracted
by Anonymous Monk on Jul 29, 2002 at 06:37 UTC |