Have a txt file keep it's line breaks after being send over FTP via Curl

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

McB

Posted 2013-07-23T19:59:54.457

Reputation: 221

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 – 2013-07-23T20:09:42.083

Answers

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.

pabouk

Posted 2013-07-23T19:59:54.457

Reputation: 5 358

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.

– pabouk – 2013-07-23T21:22:00.360

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 – 2013-07-23T21:35:27.103

@McB Thank you for feedback. I added other options to the reply. – pabouk – 2013-07-24T02:51:37.177

1Thanks 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 – 2013-07-24T15:52:00.997

-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

omichaud

Posted 2013-07-23T19:59:54.457

Reputation: 1

What question are you answering? – Toto – 2018-01-10T16:12:12.817

This one. If we need to add dos line feeds to the file that quick script will do it. – omichaud – 2018-01-13T15:49:52.853