Philip K.
Philip K.

Reputation: 41

Perl: How to get new directory made while unziping tar.gz files

I want to make a perl script that extracts a file, then opens the result and dose stuff in there.I have this code:

$res = qx{tar -xvf $tmp -C /tmp/}; #$tmp was specified earlier

but I don't know how to see what new directory was created?

This may not be the most efficient way, but I'm new to Perl and don't know all the modules.

Upvotes: 0

Views: 330

Answers (2)

Schwern
Schwern

Reputation: 164709

One option is to parse the output of tar, which gets messy and can change between versions of tar.

Or you can untar in an empty directory. Then whatever is in the directory after untaring is what came from the tarball. This works even if the tarball is impolite and does not create a directory.

use File::Temp;

my $tempdir = File::Temp->newdir;
qx{tar -xvf $tarball -C $tempdir};

Or you can also use Archive::Tar or Archive::Extract to list the files in the archive. Archive::Extract has the benefit of being a generic interface to all sorts of archive formats.

use Archive::Extract;

my $ae = Archive::Extract->new( archive => $tarball );
my $files = $ae->files;

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207425

You could build a list of the directories you have before extracting the tarball (using readdir()), then regenerate it afterwards and deduce that anything that is in the new list and was not in the original list came from the tarball.

Upvotes: 0

Related Questions