Operating systems have different ways to handle a newline in their text editors. For example Windows uses a specific carriage return (CR) which is depicted as ^M
on Linux, followed by a line feed (LF) to indicate a newline. Linux and UNIX on the other hand use only the line feed to denote the end of a line. This often causes issues when transferring (or even copy and pasting) a file from Windows to Linux. It is hard to spot, and often leaves people scratching their head and wondering why their configuration file is not working.
In this Linux quick tip we will show you how to identify and remove the Window carriage returns on the Linux command line.
Click here to jump to "How to remove the ^M carriage return character".
How to Identify the ^M Character in Linux
Before we go into how to remove the ^M
carriage returns, it is important to know how to find them. The cat command along with most text editors do not show the ^M
carriage returns by default.
[mcherisi@putor ~]$ cat windows-text-file.txt
This file was created on Windows 10 using notepad
to demonstrate how new lines are created
with carriage return then newline.
As you can see above, the file looks perfectly normal. Now let's add the -v
option for cat, which allows it to show non-printing characters.
[mcherisi@putor ~]$ cat -v windows-text-file.txt
This file was created on Windows 10 using notepad^M
to demonstrate how new lines are created^M
with carriage return then newline.^M
Now we can see the ^M
(carat M) carriage returns at the end of each line. If this was a configuration file, the service could fail to start and/or throw syntax errors.
If you want to see the ^M
in the vi editor you can pass the -b
(binary mode) option like so:
vi -b windows-text-file.txt
Or if you have the file already open in vi/vim you can issue the :e ++ff=unix
command.
How to Remove the ^M (CTRL-M) Character in Linux
There are several ways you can remove the ^M
character from files on the Linux command line. The first and easiest way is to use the dos2unix command. The dos2unix command is a brilliant utility that easily converts files to Unix format. This little utility is perfect for this situation and can remove the carriage returns without a lot of hard to remember switches. Simply pass the filename to the utility like so:
[mcherisi@putor ~]$ cat -v windows-text-file.txt
This file was created on Windows 10 using notepad^M
to demonstrate how new lines are created^M
with carriage return then newline.^M
[mcherisi@putor ~]$ dos2unix windows-text-file.txt
dos2unix: converting file windows-text-file.txt to Unix format...
[mcherisi@putor ~]$ cat -v windows-text-file.txt
This file was created on Windows 10 using notepad
to demonstrate how new lines are created
with carriage return then newline.
Another way to do remove them is to use vi or vim. Just like we use the :e ++ff=unix
vi command to see the ^M
carriage returns, we can use the :set ff=unix
command to remove them once the file is loaded into the vi editor.
Here is an example using sed to remove the ^M
carriage returns from a file.
sed -i "s/\r//g" windows-text-file.txt
Conclusion
Dealing with Windows formatted text files is fairly straightforward once you know they exist. This article covered the basics of why the pesky ^M
carriage return is in files created on Windows. We also discussed several ways to remove the ^M
character from your Linux or UNIX files.
If you have any questions or comments we would love to hear from you below. Also, feel free to tell us the way you remove these characters. As with anything in Linux, there are many ways to accomplish the same thing.
Linux and Resources
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
5 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
- Uncategorized (1)
tr -d '\r'
The method shown above for removing the ^M characters also changes a column format to line
format. Here is an example: A data file is
1 12^M
2 13^M
3 14^M
Now, doing the ^M removal results in (an unacceptable)
1 12 2 13 3 14
Which I cannot use. There are thousands of numbers in two column form.
I am unsure exactly what you are trying to say. I tested with your example and it works fine.
[savona@putor tmp]$ cat -v test.txt
1 12^M
2 13^M
3 14^M
[savona@putor tmp]$ dos2unix test.txt
dos2unix: converting file test.txt to Unix format...
[savona@putor tmp]$ cat -v test.txt
1 12
2 13
3 14
Hi thanks for the great information, I think there is a syntactic error in removing from the vi editor.
It should be like:
:set ff=unix
instead of '-'
Thank you for catching that!