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

Hi everyone

I am trying to archive my cvs repository to a .tar file using Archive::Tar

The problem is, I cant seem to use this module to tar a whole directory tree. I can only tar specified files like this

Archive::Tar->create_archive ("my.tar.gz", 9, "/this/file", "/that/fil +e");
Does anybody know how to tar a whole directory structure from a perl script?

Thanks in advance

Fergus

Replies are listed 'Best First'.
Re: Archive::Tar- how do I tar a whole directory tree
by derby (Abbot) on Jan 08, 2002 at 19:23 UTC
    Fergus,

    I don't see a way with just Archive::Tar but how about combining it with File::Find:

    #!/usr/local/bin/perl -w use Archive::Tar; use File::Find; $arc = Archive::Tar->new(); find( \&archiveit, "/whatever/dir" ); $arc->write( "my.tar.gz", 9 ); sub archiveit { $arc->add_files( $File::Find::name ); }

    -derby

Re: Archive::Tar- how do I tar a whole directory tree
by Masem (Monsignor) on Jan 08, 2002 at 19:21 UTC
    The last argument of create_archive is a list. You can generate a list of files that would be in a directory using glob. So you can call this as :
    Archive::Tar->create_archive( "my.tar.gz", 9, glob "/this/*", glob "/t +hat/*" );

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      TIMTOWTDI

      A simple way is the command line code:

      #!/usr/bin/perl use strict; my $destination = "Archive_filename_to_be_created"; my $source = "/home/users/billyjo/stuff_to_backup/"; my $cmd = `tar -cpf $destination $source`; ####### The line above uses the backtick operation, not single quotes! + ##########

      As far as using the Archive::Tar module, (RTFM) :-)

      - f o o g o d

Re: Archive::Tar- how do I tar a whole directory tree
by wileykt (Acolyte) on Jan 08, 2002 at 20:21 UTC
    It's also real easy to do this using the unix command tar.
    tar -cvf filename.TAR ./
    c = create new archive
    v = verbose messaging
    f = use the next arg as the name of the archive
    I probably shouldn't say this, but I find the unix easier than perl in this case.

    Keith