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

@tarfiles=`ls dirname`; foreach $tarfile(@tarfiles){ `tar -xf $tarfile -C ~/path_to_dir`; }
The tar files doesnot copy to another directory. But from the command line for a single tar file it untars and move the file to another folder Why the tar command doesnot works for a loop

Replies are listed 'Best First'.
Re: tar a files from loop
by GrandFather (Saint) on Mar 05, 2009 at 03:56 UTC

    It may be the trailing new line character? Or maybe you need even more clean up than that to extract the information you need to put on the tar command line? chomp @tarfiles; may help.

    Note: always use strictures (use strict; use warnings;). The missing my in front of @tarfiles=`ls dirname`; implies either you don't, or your scope for @tarfiles is too large.


    True laziness is hard work
      I have used my in front of the @tarfiles=`ls dirname`; and I removed the newline from the array @tarfiles. But still I am not able to extract values to different folder
Re: tar a files from loop
by bichonfrise74 (Vicar) on Mar 05, 2009 at 04:15 UTC
    Did you do a chomp your $tarfile?
    It works just fine for me.
Re: tar a files from loop
by toolic (Bishop) on Mar 05, 2009 at 14:02 UTC