1

I'm using Curl to send a file over FTP. It is just a generated text file from a screen hardcopy command. When I cat the file, it displays correctly, it looks like this:

System up for    14844
 1, 0000, R D S, 0, 0, x, 0.00
 2, 0000, R D S, 0, 0, x, 0.00
 3, 0000, R D S, 0, 0, x, 0.00
 4, 0000, R D S, 0, 0, x, 0.00

However when I look at the transferred file, it is all on 1 line. Any way to fix this? Is there a command line option? This is my curl command:

curl -T ~/hardlog.log ftp://10.0.0.7 --user user:secret

Thanks

1
  • 1
    Are you viewing the file on the same OS? I asked because usually you get these symptoms when a file gets transferred without any changes (you can check this, their MD5sums should be the same). However your client displays the text differently. This can happen if you transfer a file from a unix like OS (which uses linefeed at the end of a line to start at a new line) to a windows or DOS like OS (which requires both LF and CR). http://en.wikipedia.org/wiki/Newline has more information on this.
    – Hennes
    Jul 23, 2013 at 20:09

2 Answers 2

2

As it was already mentioned this is a classical symptom of incompatible line endings. In this case the source system is Unix-like (Linux) and the destination is Windows-like. In Windows the Unix newlines are not recognized so the text file appears as a single long line. In such cases you should do a newline conversion.

Though the FTP protocol has the newline conversion built-in Curl does not support it even if forcing ASCII transfer using -Q '+TYPE A' option.

The most convenient option would be to do the conversion on Linux before the transfer using a script:

#!/bin/sh
SRCFILE=~/hardlog.log
DSTLOGIN=user:secret
DSTURL=ftp://10.0.0.7/              # the URL must end by a slash

unix2dos <"$SRCFILE" | curl -T- "$DSTURL$(basename "$SRCFILE")" -u"$DSTLOGIN"

If the unix2dos utility is not available on your system, you can use sed 's/$/\r/' or awk '{printf("%s\r\n",$0)}' instead. See Alternatives to unix2dos conversion.

Another option is to use a client supporting ASCII transfers like LFTP. Example:

lftp -e "put -a $HOME/hardlog.log ; exit" -u user,secret 10.0.0.7

Note: On Windows there are text editors capable of working with Unix newlines like Wordpad or some third-party ones: Notepad++, Notepad2. The last two editors can be also used for newline conversions.

4
  • Excuse me. From further research it seems that Curl does not support the text file transfers. Please let me know if my example above does not work by chance. I will adjust the reply accordingly. I can also recommend you some standalone newline conversion tools which could be run from a script together with curl. Jul 23, 2013 at 21:22
  • Thanks for this, you're correct (as was Hennes above) that I'm going from Linux to Windows. I tried your code and unfortunately the newlines weren't formatted correctly. I do have Notepad++, but as I'd like to automate the processing of these files, it would be nice to find a way to prep them beforehand.
    – McB
    Jul 23, 2013 at 21:35
  • @McB Thank you for feedback. I added other options to the reply. Jul 24, 2013 at 2:51
  • 1
    Thanks for this! lftp would be handy, but I'm actually working on a BeagleBone and it isn't available. Since I'm trying to automate some of the setup process, downloading and making the file (which is possible) isn't ideal. I don't have unix2dos either, but I tried this code which worked: awk '{printf("%s\r\n",$0)}' hardlog.log > doslog.log
    – McB
    Jul 24, 2013 at 15:52
-1

Here's a crude but effective little loop that adds a newline to the end of each line of text in a file.

    today=`date +%m%d` # file name filter
    for file in *${today}*
    do
      > ${file}.tmp
      while read readline
      do
        echo -e "$readline\n">> ${file}.tmp
      done <${file}
      cp ${file}.tmp ${file}
      rm ${file}.tmp
    done
2
  • What question are you answering?
    – Toto
    Jan 10, 2018 at 16:12
  • This one. If we need to add dos line feeds to the file that quick script will do it.
    – omichaud
    Jan 13, 2018 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .