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

Hello Monks

I have to compress some files in unix machine. So I am getting an array
of filenames to be compressed and then writing it into
a file and using the system command to tar the list of files.
here is the code for that
use strict; @srcfiles =("/home/Scripts/Perl/abcd.txt /home/Scripts/Perl/Test.pl /h +ome/Scripts/Perl/checkingString.ksh"); $dir_src="/home/Scripts/Perl"; $filename = $dir_src ."/" . "test.txt"; open(FILE, ">>" . $filename) || die "Cannot Create File"; foreach $file (@srcfiles) { print "Files : $file \n"; print FILE "$file\n"; } system("cat $filename"); system("tar -cvf logfiles.tar cat $filename");
somehow the tar command is not working and files are not getting compressed.
any idea why tar is failing
Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: compressing files
by tirwhan (Abbot) on Nov 16, 2006 at 16:35 UTC

    In addition to davorgs help, I'd just like to point out that tar on it's own does not compress anything, it simply writes the given files into a common archive file. If you want compression on this archive the easiest way is to specify the -z(for gzip compression) or -j(for bzip2) command line option, e.g. "tar -cvzf <archive> <files>". This is for GNU tar, other variants may use different switches or may not have these options at all, consult your systems tar manpage on that.


    All dogma is stupid.
Re: compressing files
by davorg (Chancellor) on Nov 16, 2006 at 16:25 UTC

    I think you want to use backticks:

    system("tar -cvf logfiles.tar `cat $filename`");

    But I'm not sure why you aren't using:

    system("tar -cvf logfiles.tar @srcfiles");

    That removes the need for the file.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Both of those are buggy and unsafe. The first even looses tar's error code (and the second can loose tar's error code). Use the list form of system to avoid those problems.

      system('tar', '-cvf', 'logfiles.tar', @srcfiles);

      Archive::Tar is still better. ( Using a module is usually the way to go, but it might not be the case here. See replies. )

Re: compressing files
by marto (Cardinal) on Nov 16, 2006 at 16:32 UTC
    In addition to the advice you have been given, you may want to take a look at the module Archive::Tar. The Archive::Tar->create_archive($file,$compression,@filelist) method looks like it does exactly what you want.

    Hope this helps

    Martin
Re: compressing files
by throop (Chaplain) on Nov 16, 2006 at 16:52 UTC
    Wouldn't
    @srcfiles = qw(/home/Scripts/Perl/abcd.txt /home/Scripts/Perl/Test.pl /home/Scripts/Perl/checkingString.ksh);
    be more in line with what you mean? Right now, in the foreach you'll only see one $file and it will have spaces in it.
Re: compressing files
by wazoox (Prior) on Nov 16, 2006 at 17:02 UTC
    You should have Read The Fine Manual... 'tar' doesn't compress, tar only compact all files in an archive. Then you can compress the resulting .tar archive with pack, gzip, bzip2 ... GNU tar can compress and archive at the same time, with the 'z' (gzip) or 'j' (bzip2) flags :
    tar czf file.tar.gz file1 file2 tar cjf file.tar.bz2 file1 file2
Re: compressing files
by mantra2006 (Hermit) on Nov 16, 2006 at 17:23 UTC
    Hello Monks

    Thanks alot for the replies...I got implemented solutios and it worked...thanks for the help.


    Thanks & Regards
    Sridhar