Skip to content

How to Resolve No Carriage Return at End of File on Linux

  • by
A carriage return (CR) is a control character used to reset a line's position to the leftmost of a line of text. It's mostly used along with line feed (LF) to start a new line, which is called CRLF. As you noticed, we said "mostly", not "always", because there could be one exception that CR stands alone, which is at the end of files.

For example, a normal ended file like this:
[root@localhost ~]$ cat -n temp.txt
     1  My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$

There's only one line in this file. Please notice the position of the second Linux prompt. It's normal as we expect.

If I remove all the new line characters "n" (CRLF and CR only), you will see the difference.
[root@localhost ~]$ cat temp.txt | tr -d "n" > temp1.txt
[root@localhost ~]$ cat -n temp1.txt
     1  My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.[root@localhost ~]$

You can see the position of second Linux prompt stick with the end of the file. Which means the CR at the end of the file is removed also.

Mostly, it's a no harm no foul file and won't be a problem. But if you plans to append other files under files like this, it won't start a new line. That's a problem.
[root@localhost ~]$ cat temp.txt >> temp1.txt
[root@localhost ~]$ cat -n temp1.txt
     1  My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$

As you can see, I appended other file to this one, it's supposed to be two lines in the file, but it turned out only one line there. They stick together because of no CR at the end of the target file.

To solve this, we can add a CR to the target file.
[root@localhost ~]$ echo $'r' >> temp1.txt
Actually, we added an empty line and a CR, not only CR to the file, you may refer to this post for more details:
How to Distinguish Carriage Returns CR, Line Feeds LF, and New Lines CRLF on Linux

Let's finish our job to append files.
[root@localhost ~]$ cat temp.txt | tr -d "n" > temp1.txt
[root@localhost ~]$ echo $'r' >> temp1.txt
[root@localhost ~]$ cat -n temp1.txt
     1  My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$ cat temp.txt >> temp1.txt
[root@localhost ~]$ cat -n temp1.txt
     1  My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
     2  My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$

It becomes normal.

In this tutorial, we have learned the following things:
  1. How to remove a new line "n" (CRLF and CR) by tr.
  2. How to know a CR is missing at the end of a file.
  3. How to add a CR "r" to a file in order to append other files.

Leave a Reply

Your email address will not be published.