Windows line endings consist of the two-character sequence CR, LF. CR is the carriage return character, sometimes represented as \r
, \015
, ^M
, etc. A Unix line ending is just the LF character.
A way to convert Windows line endings to Unix line endings using only standard utilities present on all Unix variants is to use the tr
utility.
tr -d '\r' <thefile >thefile.new && mv thefile.new thefile
If the file already has Unix line endings, its content won't be changed.
If you have many files to transform in the current directory, you can use a loop. Assuming that you don't have any files whose name ends with .new
:
for x in *; do
tr -d '\r' <"$x" >"$x.new" && mv "$x.new" "$x"
done
Under Linux (excluding some embedded Linux systems) or Cygwin, you can use sed
. The -i
option to edit a file in place is specific to these systems. The notation \r
for a CR character is more widespread but not universal.
sed -i -e 's/\r//g' thefile
ftp(1)
program this is activated with theascii
command (run it before transferring the file, then runbinary
to get back). With other clients there might be other ways to achieve that, f.i. withlftp(1)
you have to runget -a file
. – lcd047 May 13 '15 at 14:56^M
were there all along. It's part of how Windows ends lines, so on Windows you just see a newline. You can askftp
to remove them during the transfer by activatingascii
mode. – alexis May 13 '15 at 22:23