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

Hi, I am not able to tar the file
@count=`find dir_name -name *.tar`; foreach $cnt(@count){ system("tar -xf dir_name/$cnt"); }

Replies are listed 'Best First'.
Re: Tar a files
by rovf (Priest) on Mar 02, 2009 at 12:09 UTC
    1. Your code doesn't attempt to tar, it attempts to untar.
    2. Since you decided to shell out for 'find' and 'tar', instead of doing it from within Perl, why then don't you simple use find .... -exec?
    3. Shouldn't it be `find ... -name '*.tar'`?
    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Tar a files
by moritz (Cardinal) on Mar 02, 2009 at 10:19 UTC
    So what happens? What's the error you observe?
Re: Tar a files
by Bloodnok (Vicar) on Mar 02, 2009 at 11:18 UTC
    I'm guessing your problem is the use of `find dir_name -name *.tar`; together with tar -xf dir_name/$cnt ... since the find command will prepend all found *.tar files with dir_name, so, once expanded, your tar command is looking for dir_name/dir_name/<something>.tar.

    Try

    foreach $cnt(@count){ system("tar -xf $cnt"); }
    or even
    map { system("tar -xf $cnt") } @count;

    A user level that continues to overstate my experience :-))
Re: Tar a files
by puudeli (Pilgrim) on Mar 02, 2009 at 10:36 UTC

    Please provide more information about the error. For example what kind of file names you have and the directory structure. system may split the argument string to words possibly corrupting argument vector if the file name contains whitespaces. You would be safer using systems LIST argument format. For example,

    @count=`find dir_name -name *.tar`; @args = qw( tar -x -f ); foreach $cnt(@count){ push @args, $cnt; system(@args) == 0 or die "system '@args' failed: $?"; pop @args; }
    Note, untested code.
    --
    seek $her, $from, $everywhere if exists $true{love};
Re: Tar a files
by irah (Pilgrim) on Mar 02, 2009 at 10:34 UTC
    For taking tar, Use Archive::Tar module.