Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

path in tar file...please help

by advait (Beadle)
on Oct 10, 2007 at 20:17 UTC ( #644079=perlquestion: print w/replies, xml ) Need Help??

advait has asked for the wisdom of the Perl Monks concerning the following question:

Hi All
$cmd ="tar -cvvf $UploadDir/results.tar $UploadDir/en_ran.out $UploadD +ir/en-ave.out $UploadDir/en_ave_ran.out $UploadDir/gc-ave.out $Upload +Dir/score_ran.out $UploadDir/score-ave_ran.out $UploadDir/score-ave.o +ut ";
when i unzip this tar file in windows it shows me all the folders like /var/www/web/tmp/output/nu98382139012783/en_ave_ran.out. What I mean it is storing the full makes nested folder similar as UploadDir I don't know how to avoid these folder. I am running the script from cgi-bin folder of apache and these output files are located in some other directory Please help

Replies are listed 'Best First'.
Re: path in tar file...please help
by gamache (Friar) on Oct 10, 2007 at 21:04 UTC
    The answer is in tar, not perl. One of the following lines will do what you want: using tar's -C (change directory) option:
    $cmd = "tar vvcCf $UploadDir $UploadDir/results.tar en_ran.out en-ave. +out ...";
    or using shell tricks:
    $cmd = "cd $UploadDir && tar vvcf results.tar en_ran.out en-ave.out .. +.";
Re: path in tar file...please help
by johngg (Canon) on Oct 10, 2007 at 21:02 UTC
    It looks like your tar archive was written with absolute paths. AFAIK you are going to be stuck with those paths if you are using the tar executable. I have had a quick look at the Archive::Tar module and there are some methods shown in the documentation that might help you out. However, I haven't used that module so I might be wrong. If it is possible, it might be quicker to re-create the tar archive with relative paths.

    I hope this is of use.



    Update: I didn't explain that last very well. What I mean is change $UploadDir in your script so that it contains a relative path rather than an absolute one and do a chdir to where that relative path starts before running the tar command.

Re: path in tar file...please help
by philcrow (Priest) on Oct 10, 2007 at 21:04 UTC
    Can you have the script chdir $UploadDir prior to issuing the shell out to tar? Then omit the repeated use of $UploadDir from the command:
    my $cmd = 'tar -cvf results.tar en_ran.out...'; chdir $UploadDir; my $result = `$cmd`;
    Otherwise, you'll need to closely read the man page for your tar command to see if there is a flag in your version to strip path elements either on the way into or out of the tar. GNU tar (in some versions) has --strip-components NUMBER, others have --strip-path NUMBER, these can remove path elements during extraction.


    The Gantry Web Framework Book is now available.
Re: path in tar file...please help
by djp (Hermit) on Oct 11, 2007 at 00:49 UTC
    Answers above hint at the simplest solution, without actually giving it. Try this:
    $cmd ="tar -cvvCf $UploadDir $UploadDir/results.tar en_ran.out en-ave. +out en_ave_ran.out gc-ave.out score_ran.out score-ave_ran.out score-a +ve.out";
    (Later) Oops, gamache gave the correct solution.
Re: path in tar file...please help
by snopal (Pilgrim) on Oct 10, 2007 at 21:05 UTC

    I don't quite understand what you are trying to accomplish, but if it is that you only want the files, with no directory tree in front of them then you need to chdir to the $UploadDir first.

    chdir $UploadDir; $cmd = "tar -cvvf results.tar en_ran.out en-ave.out en_ave_ran.out en_ +ave_ran.out gc-ave.out score_ran.out score-ave_ran.out score-ave.out" +; system ($cmd);
Re: path in tar file...please help
by perlhaq (Scribe) on Oct 11, 2007 at 00:28 UTC
    If the machine in question has the pax utility, you can use its -s option to manipulate the path that gets stored (and even strip it completely, leaving just each file's basename). The POSIX man page is here (your implementation may differ, but it probably won't matter for simple stuff):
    The "tar -C" thing will get the job done in this case, but pax is more powerful, and thus worth knowing about, IMO.
Re: path in tar file...please help
by ilcylic (Scribe) on Oct 10, 2007 at 21:05 UTC
    Does $UploadDir == "/var/www/web/tmp/output/nu98382139012783/" ? If so, that's why the tar file is storing the full path.

    To avoid this, you need to change the current working directory of where you are calling tar from, to the value of $UploadDir, and eliminate $UploadDir from the tar command, so it will operate on the local directory.

    Is that what you want tar to do?

    Check out the perl function "chdir" to effect that change within your script.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://644079]
Approved by ikegami
Front-paged by ikegami
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2023-12-07 15:48 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (32 votes). Check out past polls.