How to extract .tar files in different directory?

Discussion in 'Perl Misc' started by Dan, Feb 15, 2007.

  1. Dan

    Dan Guest

    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!
     
    Dan, Feb 15, 2007
    #1
    1. Advertising

  2. Dan

    Guest

    On Feb 15, 4:16 pm, "Dan" <> wrote:
    > 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
     
    , Feb 15, 2007
    #2
    1. Advertising

  3. Dan

    gf Guest

    On Feb 15, 2:16 pm, "Dan" <> wrote:
    > 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.
     
    gf, Feb 15, 2007
    #3
  4. Dan

    Guest

    On Feb 15, 4:29 pm, "" <> wrote:
    > On Feb 15, 4:16 pm, "Dan" <> wrote:
    >
    >
    >
    > > 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


    Thanks for all reply, actually, I am running my script in windows XP,
    so I can not use unix cmd.
     
    , Feb 16, 2007
    #4
  5. Dan

    gf Guest

    On Feb 16, 7:42 am, wrote:

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


    Look at 7-Zip, which is an open-source app running on Windows that
    nicely handles tar, plus other formats.

    http://www.7-zip.org/

    Wikipedia has lots to say...

    http://en.wikipedia.org/wiki/Tar_(file_format)
     
    gf, Feb 16, 2007
    #5
  6. Dan

    Mumia W. Guest

    On 02/16/2007 08:42 AM, wrote:
    > On Feb 15, 4:29 pm, "" <> wrote:
    >> [...]
    >> 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.


    --
    Windows Vista and your freedom in conflict:
    http://www.regdeveloper.co.uk/2006/10/29/microsoft_vista_eula_analysis/
     
    Mumia W., Feb 16, 2007
    #6
  7. Dan

    Guest

    On Feb 16, 12:24 pm, "Mumia W." <paduille.4060.mumia.w
    > wrote:
    > On 02/16/2007 08:42 AM, wrote:
    >
    > > On Feb 15, 4:29 pm, "" <> wrote:
    > >> [...]
    > >> 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.
    >
    > --
    > Windows Vista and your freedom in conflict:http://www.regdeveloper.co.uk/2006/10/29/microsoft_vista_eula_analysis/




    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!
     
    , Feb 16, 2007
    #7
  8. Dan

    Mumia W. Guest

    On 02/16/2007 03:40 PM, wrote:
    > On Feb 16, 12:24 pm, "Mumia W." <paduille.4060.mumia.w
    > > wrote:
    >> On 02/16/2007 08:42 AM, wrote:
    >>
    >>> On Feb 15, 4:29 pm, "" <> wrote:
    >>>> [...]
    >>>> 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.
    >>
    >> --
    >> Windows Vista and your freedom in conflict:http://www.regdeveloper.co.uk/2006/10/29/microsoft_vista_eula_analysis/

    >
    >
    >
    > 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.


    --
    Windows Vista and your freedom in conflict:
    http://techdirt.com/articles/20061019/102225.shtml
     
    Mumia W., Feb 17, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claudio Grondi
    Replies:
    4
    Views:
    589
    Claudio Grondi
    Aug 20, 2005
  2. Replies:
    2
    Views:
    454
    Michael Hoffman
    Apr 24, 2007
  3. Ray Van Dolson
    Replies:
    0
    Views:
    347
    Ray Van Dolson
    Sep 23, 2009
  4. Ray Van Dolson
    Replies:
    0
    Views:
    855
    Ray Van Dolson
    Sep 25, 2009
  5. benoit Guyon
    Replies:
    2
    Views:
    237
    benoit Guyon
    Jul 26, 2005
Loading...

Share This Page