My friend asked me today how to change the destination directory of extracted tar data. At first I suggested

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'


In reply to tar_mv: move tar data as it's being extracted by belden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.