#!/usr/bin/perl use strict; use Archive::Tar; # begin by creating a test directory to tar up: BEGIN { mkdir "test_dir",0755; for my $i ( 1 .. 3 ) { open( TEST, ">test_dir/test$i.txt" ); print TEST "This is test data file number $i\n"; close TEST; symlink "test$i.txt", "test_dir/test$i.lnk" unless ( $i % 2 ); } } my $tar = Archive::Tar->new(); opendir( DIR, "test_dir" ) or die $!; my @files = grep /\w/, readdir DIR; foreach my $f ( @files ) { next unless ( $f =~ /test2/ ); # just for the sake of being selective # UPDATE: this doesn't actually work as hoped for: # if ( -l "test_dir/$f" ) # { # it's a symlink!! # my $targ_path = readlink "test_dir/$f"; # my %opt_hash = ( linkname => $targ_path ); # $tar->add_data( "test_dir/$f", '', \%opt_hash ) or # die "failed to add $f: $!\n"; # } # else # { # it's just a plain file # # BUT: this actually does what the OP wants, and seems to # disagree with heezy's findings: $tar->add_files( "test_dir/$f" ) or die "failed to add $f: $!\n"; # } } $tar->write( "test_dir.tar", 0 ) or die "failed to write tar file: $!\n"; # let's end with a report of how we did END { my $report = `tar tvf test_dir.tar`; print $report; }