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

Replies are listed 'Best First'.
Re: tar sytem command
by grantm (Parson) on Oct 29, 2002 at 05:49 UTC

    I think you just need to do a chdir($pathname) where $pathname is the directory the archive is in - tar unpacks into your current working directory.

Re: tar sytem command
by abell (Chaplain) on Oct 29, 2002 at 08:25 UTC
    You can also use tar -C destination_dir -xzvf source_dir/file.tgz

    The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket
Re: tar sytem command
by graff (Chancellor) on Oct 29, 2002 at 05:48 UTC
    This is not really a perl question, but the answer is:

    "tar x..f tarfile" will unpack the contents of the named tar file in the current working directory -- period. Where the tar file contents are placed after tar extracts them is fully and only dependent on what the current working directory is when the tar command is executed. (The actual path/filename of the tar file given on the command line can be anything/anywhere, so long as the shell running the tar command can locate that path and file, but the "cwd" will be where the contents end up.)

    So, controlling the location of unpacked contents involves the "cd" command (or the "chdir" function, if unpacking a tar file from within a perl script).

Re: tar sytem command
by tadman (Prior) on Oct 29, 2002 at 05:57 UTC
    It's pretty simple, like grantm said:
    my $archive = "/some/dir/archive.tar.gz"; # Split the filename off of the directory using a regular expression my ($dir, $filename) = $archive =~ m#^(.*)/([^/]+)$#; # Switch in to the directory with the archive chdir($dir) || die "Invalid path $dir\n"; # Extract the archive using 'tar' system("tar","-xvzf",$filename);
    That's generic, and should do the job for you.

      I'd be inclined to use:

      use File::Basename; my $archive = "/some/dir/archive.tar.gz"; my ($filename, $dir) = fileparse($archive);

      Of course you could argue that it's fairly safe to assume a system that has 'tar' installed probably also uses '/' for a directory separator :-)

        Good point. I was thinking there was a module, but couldn't make the mental leap past File::Path which is not what I was looking for.

        By the way, how do you find out what the directory separator is? Check $/ and guess?
Re: tar sytem command
by aseidas (Beadle) on Oct 29, 2002 at 05:37 UTC
    Well to untar it to the directory the archive is in just do
    tar -xvf filename.ext
    I think that is what you mean???? I am not sure though, seems to obvious, what do you mean by " the directory the script is in? Do you mean tar is being executed from a script? Please clarify. -aseidas