Home Linux Commandline Tools How to Remove (^M) Characters from a File in Linux

How to Remove (^M) Characters from a File in Linux

A common functional uniqueness of different operating system environments is in the way they handle and process data. This functionality is in the way a file’s text data is perceived and processed.

For instance, you can have a normal text file created and populated under a Windows computing environment but once that same file is transferred to a Linux computing platform, it is processed and displayed differently.

There is a logical explanation for these transferred text file’s misbehavior. On a Windows computing environment, the OS acknowledges the presence of a new line within a text file through a specific carriage return.

While the representation of this carriage return (CR) character is hidden from the eyes of these Windows users, transferring this file to a Linux computing environment exposes it as ^M characters.

The ^M Characters Causes and Effects

You are bound to run into these ^M characters issues when you directly copy or download files from a Windows OS environment to a Linux OS environment. Because spotting these nagging characters is not so easy; especially on large files, you might end up thinking your downloaded or transferred configuration text file is damaged or corrupt.

The aftermath of these ^M character’s effect is unexplained syntax errors and system service failures. This article guide seeks to achieve two objectives:

  • Help you identify the ^M characters on files opened or downloaded on your Linux operating system environment.
  • Help you resolve the ^M character issue by removing it from your text files.

Identifying ^M Characters on Linux Files

Before we try and treat the ^M characters symptom on Linux files, identifying these characters is an important diagnostic step. It will help you determine whether your text file is the one with an issue (if you are using it as a configuration file) or the problem lies elsewhere within your Linux system.

Consider the existence of the following Windows OS created a file (windows-downloaded-file.txt) that is now transferred on my Linux operating system environment:

$ cat windows-downloaded-file.txt
View File Content in Linux
View File Content in Linux

The opened file appears to be normal. It is because the Linux cat command; and a selection of other Linux-supported text editors, will by default ignore displaying the ^M characters on files created from Windows OS environments.

It is because ^M is categorized as a non-printing character. To reveal the existence of these characters on a Windows-created file, we will use the Linux cat command together with the -v option.

$ cat -v windows-downloaded-file.txt
Show File Content in Linux
Show File Content in Linux

Now that we have spotted the existence of these nagging ^M characters, it’s time to get rid of them.

Removing ^M Characters on Linux Files

We are going to use the dos2unix utility. Install it on your preferred Linux distribution from either of the following commands:

$ sudo apt-get install dos2unix     [On Debian, Ubuntu and Mint]
$ sudo yum install dos2unix         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/dos2unix  [On Gentoo Linux]
$ sudo pacman -S dos2unix           [On Arch Linux]
$ sudo zypper install dos2unix      [On OpenSUSE]    

To remove the ^M characters from our Windows-downloaded file, we will run the following command:

$ dos2unix windows-downloaded-file.txt

Check for the existence of the ^M characters.

$ cat windows-downloaded-file.txt
Check ^M Characters in File
Check ^M Characters in File

You now understand the implications and resolution of the issue of the ^M character on files transferred from other operating systems like Windows OS. The dos2unix utility effortlessly converts such files into UNIX formats that are supported on all Linux OS environments.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

2 thoughts on “How to Remove (^M) Characters from a File in Linux”

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.