Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl - gtar? 1

Status
Not open for further replies.

vietboy505

Technical User
Feb 20, 2006
56
US
Windows side doesn't have tar or gtar right?

I am trying to write the script to copy directory or file to a destination. I think it's on my Windows side is not working.

Code:
$TAR = "/usr/local/bin/gtar"; #gtar is there on the unix side
$opt = "";
#where $source & destdir is define earlier
$c = "cd $source; $TAR -" . $opt . "Scvf - . | (cd $destdir; $TAR -SxBpf - )";
        printf($c);
        system($c);
Everything before the code is right, I think it's the tar part it not working? how can I fix it to work on the Windows side or both for UNIX & Windows?
 
Using system() generally isn't a good way of achieving things like this that need to be cross-platform. If you need it to work on Windows too, use something like Archive::Tar.

Incidentally, don't use printf() to print things, use simple print() instead.
 
I am reading the tar now..

How would I redefine my code for copy files or directories to a new destination using TAR?
 
Code:
use Archive::Tar;
my $tar = Archive::Tar->new;

#where $source & destdir is define earlier
$tar->add_files("$source", "$destdir");

Nothing happen.
 
You need to call the write() method to write your tar file.

You're talking about copying files and directories so I'm curious as to why you're using tar in the first place.
 
ishnid, the first thing came to my mind is tar in UNIX beside 'cp'. I don't know why. :)

Should I use File::Copy to make life easier?
[link]http://www.tizag.com/perlT/perlcopyfiles.php[/url]
Does that work the same for directory?
 
I think tar can compress file the down and that's why I choose tar.
 
I try other method..

Code:
use General::FileSystem;
copyFile($source, $destdir) or die "File cannot be copied.";

Can't locate General/FileSystem.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at line 75.
BEGIN failed--compilation aborted at line 75.
 
The tar command doesn't do any compression by itself. It merely sticks (hence the name) files together into a single file. It's often used in conjunction with gzip or bzip to compress those files (in fact you can get tar to call those programs by giving it the -z or -j flags when creating the tar archive).

I'd only bother using tar (with compression) if I was transferring files between machines, to cut down on the transfer time over the network.

I don't believe File::Copy handles directories. File::Copy::Recursive should do it for you.
 
ishnid,

I will stick to Archive::Tar, and you said to put the write(), but it only take an original file but not a destination.
How would I do that for copy of orginal files to new destination, copy directory to new directory?

Thanks for the help.

Code:
use Archive::Tar;
my $tar = Archive::Tar->new;

foreach $source (@ARGV) {
    if (-d $source) {
        printf("Copying directory $source\n");
       	#$c = "cd $source; $TAR -" . $opt . "Scvf - . | (cd $destdir; $TAR -SxBpf - )";
        #system($c);
        #HOW WOULD I USE ARCHIVE::TAR TO COPY DIRECTORY?
    }
    elsif (-e $source) {
        printf("Copying file $source\n");
	#$c = "cd $pwd; $TAR -" . $opt . "Scf - $source | (cd $destdir; $TAR -SxBpf - )";
	#system($c);			
        #HOW WOULD I USE ARCHIVE::TAR TO COPY FILE?
    }
    else {
        print(STDERR "Can't find '$source'\n");
    }
}
 
I believe Archive::Tar extracts files to your present working directory. You could use perl's chdir function to change your pwd, then use the extract() method to extract your files there. It doesn't look like you really need to create a tar archive at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top