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.
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.
[[email protected] ~]$ 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.
[[email protected] ~]$ 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:
[[email protected] ~]$ 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 [[email protected] ~]$ dos2unix windows-text-file.txt dos2unix: converting file windows-text-file.txt to Unix format... [[email protected] ~]$ 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
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
This site uses Akismet to reduce spam. Learn how your comment data is processed.