How to remove carriage return in Linux or Unix

How do I remove all the carriage returns \r (^M) from a file in Unix using the command-line options?

A carriage return is nothing but a control character used to reset a device’s position to the beginning of a text line. In other words, whenever you hit the [Enter] key, you generate carriage return. It is a newline concept. Please note that in ASCII and Unicode, the carriage return is defined as 13. So you may see it as control+M (^M). In the C and especially on Linux/macOS or Unix-like system, we will see it as \r. In DOS/Windows text files, a line break is a combination of two characters: a Carriage Return (CR) followed by a Line Feed (LF). In Unix/Linux/macOS text files, a line break is a single character: the Line Feed (LF). This page explains how to remove carriage return and update file easily.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux, macOS, *BSD or Unix CLI
Est. reading time 2m

Removing carriage return in Linux or Unix

The procedure to delete carriage return is as follows:

  1. Open the terminal app and then type any one of the following command.
  2. Use the sed: sed 's/\r$//' file.txt > out.txt
  3. Another option is tr: tr -d '\r' input.txt > out.txt
  4. MS-Windows (DOS)/Mac to Unix/Linux and vice versa text file format converter: dos2unix infile outfile
  5. Verify that carriage return removed or converted on Linux/Unix:
    od -c output
    cat -v filename

Let us see all commands and examples in details.

Viewing carriage return under Linux or Unix

The syntax is as follows:
cat -v input.txt
od -c /path/to/file.php

The cat command prints nonprinting chracters such as ^ and M- notation, except for LFD and TAB. To see LFD and TAB pass the -T:
cat -T filename
We use the od command to dump files in octal and other formats on screen. Pass the -c to see printable characters or backslash escapes on screen:
od -c input
cat file | od -c

Modern text editors such as vim can show carriage return too:
vim filename
How to view carriage return on Linux Unix macOS using cat and od

Deleting carriage return using the sed on Linux/Unix/macOS

The syntax is:

sed 's/\r$//' file.txt > out.txt
sed "s/$(printf '\r')\$//" file.php > out.file.php && mv out.file.php file.php

Sed command has an option to do an in-place file update for both GNU/sed and BSD/sed on macOS. Hence, the syntax is:

sed -i sed 's/\r$//' input
cat -v input
# macos/bsd need file ext # 
sed -i'.BAKUP' input.file
cat -v input.file

DOS/Mac to Unix and vice versa text file format converter

First install dos2unix text file format converter using the apt command/apt/get command on your Ubuntu/Debian box:
sudo apt install dos2unix
RHEL/CentOS/Fedora Linux users try:
sudo yum install dos2unix # <-- RHEL/CentOS 7.x
sudo dnf install dos2unix # <-- Fedora latest/RHEL/CentOS 8.x

Now use it as follows:
dos2unix input output
cat -v output
od -c output

Using the tr command under Unix or Linux

We can convert line breaks in a text file from Unix format (Line feed) to DOS format (carriage return + Line feed) and vice versa:

cat input | tr -d '\r' > output
tr -d '\r' < input > output
cat -v output
od -c output

A note about perl

Perl is yet another method to remove all the carriage returns \r from a file in Unix or Linux systems. The syntax is:

perl -i -p -e 's/\r//g' my_file
cat -v my_file
od -v my_file

col command

The col command filters out reverse (and half reverse) line feeds so that the output is in the correct order with only forward and half-forward line feeds and replaces white-space characters with tabs where possible. We can pass the -b and tell the col not output any backspaces, printing only the last character written to each column position as follows:

col -b < input_file > output_file
cat -v output_file
od -c output_file
How to delete and remove carriage return in Linux or Unix

Deleting carriage return in Linux or Unix using the CLI options

Converting files in bulk

In the current directory try sed as follows with help of bash for loop:

for f in *.php
  sed -i'-backup.php' 's/\r$//' "$f"

In this final example, search for all PHP files in /app/ directory, including sub-folders, and run the sed command on it as follows:

find /app/ -type f -iname "*.php" -print0 \
| xargs -I {} -0 sed -i'-backup.php' 's/\r$//' "{}"


You learned how to remove or delete carriage return (\r) under Linux and Unix-like system using various commands.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Mostafa Kassem Feb 2, 2021 @ 14:22

    On Windows, you can use notepad++ edit-> EOL Conversion to switch between Windows and UNIX line ending.

  • Sam Stephens Feb 3, 2021 @ 16:34

    tr -d '\r' input.txt > out.txt” will not work because tr only accepts standard input and not file input. “tr -d '\r' < input.txt > out.txt” or “cat input.txt | tr -d '\r' > out.txt” should be used instead.

  • Samuel Stephens Feb 3, 2021 @ 16:39

    The last line should read “tr -d '\r' < input.txt > out.txt” or “cat input.txt | tr -d '\r' > out.txt” should be used instead.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum