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 | Easy (rss) |
Root privileges | No |
Requirements | Linux, macOS, *BSD or Unix CLI |
Time | 2m |
Removing carriage return in Linux or Unix
The procedure to delete carriage return is as follows:
- Open the terminal app and then type any one of the following command.
- Use the sed: sed 's/\r$//' file.txt > out.txt
- Another option is tr: tr -d '\r' input.txt > out.txt
- MS-Windows (DOS)/Mac to Unix/Linux and vice versa text file format converter: dos2unix infile outfile
- Verify that carriage return removed or converted on Linux/Unix:
od -c output
OR
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
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
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 do sed -i'-backup.php' 's/\r$//' "$f" done
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$//' "{}"
Conclusion
You learned how to remove or delete carriage return (\r) under Linux and Unix-like system using various commands.
🐧 3 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
On Windows, you can use notepad++ edit-> EOL Conversion to switch between Windows and UNIX line ending.
“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.
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.