Search code examples
cronubuntu-14.04carriage-return

Ubuntu 14.04 Cron outputs file names with ^M at the end


When I pipe output to a file in a cron job using the > operator, it always appends a ^M to the end of the file name. This shows up as a ? when I run ls in the directory but reveals itself as ^M when I edit the file in nano and go to save.

For example this command:

locale > locale.txt

Outputs a file named "locale.txt?" (i.e. "locale.txt^M")

I don't know why it does this, but I'm guessing it has something to do with environment variables. When I use > from a terminal it behaves properly. I've searched Google for this problem but apparently it doesn't like all these special characters in the query so I haven't found anything.

I've tried using mv to change the file name back to normal but it doesn't recognize the ? or the ^M character when I type in the file name.

I've seen that perhaps this is the carriage return "\r" character but I don't know why cron would put a Windows newline on the end of my file name. All help is appreciated. Thanks!


Solution

  • The problem is with the cron or script file itself: it has DOS line separators (CRLF) instead of Unix (LF only). You can fix it using dos2unix.