How to extract .tar files in different directory?

D

Dan

Hi, could anyone tell me how to do a perl script, which can
extract .tar files in
different directories?

e.g.

I have ./data/11/aaa/1a.tar, 1b.tar, 1c.tar
./data/22/bbb/2a.tar, 2b.tar, 2c.tar

how can I extract 1*.tar and 2*.tar files under /data directory into a
specified folder.

Since I am pretty new to perl, I dont know how to examine every sub-
directory
in the ./data, if there are any .tar files, then unzip it.
could you give me any examples.

Thanks a lot!
 
M

marora

Hi, could anyone tell me how to do a perl script, which can
extract .tar files in
different directories?

e.g.

I have ./data/11/aaa/1a.tar, 1b.tar, 1c.tar
./data/22/bbb/2a.tar, 2b.tar, 2c.tar

how can I extract 1*.tar and 2*.tar files under /data directory into a
specified folder.

Since I am pretty new to perl, I dont know how to examine every sub-
directory
in the ./data, if there are any .tar files, then unzip it.
could you give me any examples.

Thanks a lot!

You can use File::Find to traverse a directory tree. http://
perldoc.perl.org/File/Find.html
From perl you can execute any unix command using system("cmd").

Regards,
Manish
 
G

gf

Hi, could anyone tell me how to do a perl script, which can
extract .tar files in
different directories?

e.g.

I have ./data/11/aaa/1a.tar, 1b.tar, 1c.tar
./data/22/bbb/2a.tar, 2b.tar, 2c.tar

how can I extract 1*.tar and 2*.tar files under /data directory into a
specified folder.

Since I am pretty new to perl, I dont know how to examine every sub-
directory
in the ./data, if there are any .tar files, then unzip it.
could you give me any examples.

Thanks a lot!

Something to consider...

Rather than do it purely in Perl, you might want to wrap a tar command
in backticks or a system() command and let the tar application handle
the problem. In that case just read up on the tar docs.

Sometimes it's easier to take advantage of Perl's ability to act as
glue, but we forget to do it when we're wearing our Perl colored
glasses.
 
A

au.danji

You can use File::Find to traverse a directory tree. http://
perldoc.perl.org/File/Find.html


Regards,
Manish

Thanks for all reply, actually, I am running my script in windows XP,
so I can not use unix cmd.
 
M

Mumia W.

[...]
From perl you can execute any unix command using system("cmd").

Regards,
Manish

Thanks for all reply, actually, I am running my script in windows XP,
so I can not use unix cmd.

You can install the Perl module Archive::Tar to help you with the tar files.
 
A

au.danji

[...]
From perl you can execute any unix command using system("cmd").
Regards,
Manish
Thanks for all reply, actually, I am running my script in windows XP,
so I can not use unix cmd.

You can install the Perl module Archive::Tarto help you with thetarfiles.



Thank for everyone's reply!!

Below is my perl code, which can unzip all .tar file from
one directory, however, since I have around 20 .tar files
in this directory(each one is 100MB), when the script running,
my pc becomes really slow, I guess that because Perl put them
into the memory, could anyone tell me if I can extract these big
..tar files without using large amount of my PC memory? Thanks!

This is my code:
=============================================================
opendir(DIR,"$localpath") || die("cannot open $localpath");

foreach $direntry (readdir(DIR))
{
if ($direntry =~ /tar/)
{
print "direntry tar is: $direntry\n";
if ($tar->read("$localpath/$direntry",1))
{
print "read $direntry successfully\n";
}

if ($tar->extract())
{
print "extract $direntry successfully\n";
}
}

}


=================================================================

Also I try to put the unziped files into a different folder, but the
$tar->extract_file is not working like below code, what is wrong with
that?

$tar->extract_file("$localpath/1.tar","$localpath/extract/test");


thanks a lot!
 
M

Mumia W.

[...]
From perl you can execute any unix command using system("cmd").
Regards,
Manish
Thanks for all reply, actually, I am running my script in windows XP,
so I can not use unix cmd.
You can install the Perl module Archive::Tarto help you with thetarfiles.



Thank for everyone's reply!!

Below is my perl code, which can unzip all .tar file from
one directory, however, since I have around 20 .tar files
in this directory(each one is 100MB), when the script running,
my pc becomes really slow, I guess that because Perl put them
into the memory, could anyone tell me if I can extract these big
..tar files without using large amount of my PC memory? Thanks!

This is my code:

Please read and follow the posting guidelines for this newsgroup:
http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Please start your scripts with

use strict;
use warnings;

and write your program to work with them. They will catch many errors
for you. Also read the documentation for Archive::Tar:
http://search.cpan.org/dist/Archive-Tar/lib/Archive/Tar.pm

=============================================================
opendir(DIR,"$localpath") || die("cannot open $localpath");

foreach $direntry (readdir(DIR))
{
if ($direntry =~ /tar/)
{
print "direntry tar is: $direntry\n";
if ($tar->read("$localpath/$direntry",1))
{
print "read $direntry successfully\n";
}

if ($tar->extract())
{
print "extract $direntry successfully\n";
}
}

}

I don't have Archive::Tar installed, but it sounds like you can enable
the "extract" option to have it automatically extract files when "read"
is called. Change into the desired directory and invoke "read" on the
$tar object.
=================================================================

Also I try to put the unziped files into a different folder, but the
$tar->extract_file is not working like below code, what is wrong with
that?

$tar->extract_file("$localpath/1.tar","$localpath/extract/test");


thanks a lot!

If the above suggestion works, this should not be needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top