tags:

views:

211

answers:

3

How can I tar multiple directories and also append files with some pattern like '.txt' and exclude some directories and exclude some patterns like '.exe' all into a single tar file. The main point is the number of directories are unknown(dynamic), so I need to loop through I guess?

+7  A: 

I'd use Archive::Tar and populate @filelist with Class::Path (specifically Class::Path::Dir's recurse method)

David Dorward
I cannot use Archive::Tar, because i am using system call to create a tar file(its the requirement)$tarExe = "$toolDir/bin/gnu_tar/${binPlatform}/tar.exe";system("${tarExe} -cvf $tarname $location");
superstar
You have a requirement to do it the hard way? There is no good reason to do that on a real project. Is this homework?
David Dorward
i need to use tar.exe from the winx64 folder to create the tar file.This is standard method I was suggested to do. Its a real project to create tar files.
superstar
Using a Perl module is the standard way to do things in Perl, shelling out to other binaries is not.
David Dorward
I understand. But I have to do it this way. thats my problem...
superstar
If it was a suggestion, then you don't have to do it that way. Do it the right way instead.
David Dorward
Is there some requirement that no additional software be installed? If not, just install the module.
Sorpigal
Archive::Tar comes with Perl (since 5.009003, according to corelist).
draegtun
how do i make a system call to exclude some folders while TARring directories. i need to tar my1 and my2 from RandomName and myPacket as basetar.tar and exclude my3 from myPacket.This is what i have and it doesnt work...any suggestion?. tar -cvf basetar.tar input\RandomName\my1 input\RandomName\my2 input\myPacket --exclude=input\myPacket\my3
superstar
If you have requirements that you cannot install modules, perhaps you could state this in the question so that if people spend (waste?) time answering you question ......
justintime
A: 

If for some reason you cannot, or are not permitted to, install additional modules beyond the base system you could use File::Find instead of Class::Path.

It sounds like you already know how to call out to the system tar command so I'll leave it at that.

Sorpigal
+1  A: 

Assuming you have worked out what files you want using File::Find then something like

    my @dir = qw/a b/ ;
    system "tar -cvf mytar @dir" ;

might work. But you might find that the command line is too long.

In which case maybe write the list of files to a file and use the option

   --files-from=NAME

(and please don't tell me you are not allowed to write to files)

justintime
It will break if any of the file names include a character with special meaning for the shell. Provide arguments as the list after the PROGRAM argument.
David Dorward
presumably you are referring to my first suggstion. The files in --file-from would be safe against that.
justintime