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

I am trying to move files from dos to unix. I have a script that tars up the files and ftp to unix box. But the files that are ftped are still in dos format. How can I change to unix format before doing the ftp? Here is how my code looks like:
open(FilesToPut, "test.txt") or die "can't open test.txt: $!"; my $tar = Archive::Tar->new(); while (<FilesToPut>) { chomp; $tar->add_files($_); } $tar->write('test.tar'); }
can i convert the file to unix format before tarring it up ? Any other ideas??? Thanks

Replies are listed 'Best First'.
Re: Dos to Unix conversion
by Roy Johnson (Monsignor) on Nov 18, 2005 at 19:53 UTC
    You need to know which are text files and which (if any) are binaries. You can convert only the text files on either end using dos2unix (which is likely to exist on one or the other system) or roll your own.

    Caution: Contents may have been coded under pressure.
Re: Dos to Unix conversion
by planetscape (Chancellor) on Nov 19, 2005 at 06:10 UTC
Re: Dos to Unix conversion
by pileofrogs (Priest) on Nov 18, 2005 at 19:46 UTC

    This might be a terrible idea, but I think tar files are just the data from your files + some tar data all concatinated togeather. That means you should be able to convert the tar file as thought it was a text file with the same results as filtering all the individual text files.

    If you have a mixture of binary and text files though, this absolutely won't work and you'd have to filter the text files individually.

    Update:
    Apparently, according to my downvotes, I suck. I thought this was a genuinely useful comment on the user's question. I'm new to perlmonks, and I'd like any advice that might lead to me sucking less.

    Up-Update:
    Thanks sauoq! Yes! This IS a terrible idea.

    -Pileofrogs
      Apparently, according to my downvotes, I suck. I thought this was a genuinely useful comment on the user's question. I'm new to perlmonks, and I'd like any advice that might lead to me sucking less.

      Your downvotes aren't an indicator that you suck, just that your answer does. Although you are sort of correct (tar files are just some header data and the concatenated files) you missed the fact that tar headers can contain \r or \n, so your suggestion wouldn't work. At least not all of the time... which is, of course, even worse because you really don't want something that only breaks some of the time. ;-)

      -sauoq
      "My two cents aren't worth a dime.";
      
      Your question is ambiguous...dos and unix are not file-types ... they're operating systems (and unix refers to a whole class of operating systems...)

      if you're referring to the naming conventions, then it should be easy to rename the files before tarring them...if you're working with binary files then conversion will need to be done specifically for each file (hopefully you have a conversion utility for whatever type of conversion you have in mind)

      Unless these files are spread across multiple directories and subject to a long list of constraints, this seems to be something that would be better performed with a batch script within the source operating system.

      If you're concerned with simply transporting the files from one system to another, you can probably just ftp the whole tar file from any convenient terminal program...

      Please provide more information as to the nature of your problem.

      -muoyo
        my files are in various directories. I read a input file called test.txt to get the files and tar them up. I then move the tar file to a unix box. Example of my input file: test/test1.txt test2/test.sql test/test2/test3.txt I am able to move the file to unix box but when I untar the files and take a look at it they have ^M and end of each line. I am doing ftp in binary mode. If I use asii I get the error block size error.
        Given your additional info I would agree with the comments later in the thread.. use dos2unix to convert text files (barring that, you could write a reg-exp to strip the ^M at every newline).
        Secondly, try a test case involving tarring only text files to see whether the block size error is intrinsic, or data dependent.

        -muoyo