1. Introduction

Unix systems use the newline character (/n) to signal that the current line has ended and that what will follow will be part of a new line.

On the other hand, Windows and DOS systems use the carriage return plus the newline character (/r/n) to signal that the line has ended. This difference in line endings often causes many errors when people move files across operating systems, preventing files from being properly read or rendered.

In this tutorial, we’ll explore different ways to add a carriage return character before the newline character to make Unix files work properly in Windows.

2. Add a Carriage Return Before Every Newline – Sample File

As stated before, to make the Unix files work in Windows, we need to change the line endings by adding a carriage return before the newline character, so the line endings will change from (/n) to (/r/n). We can use different tools we’ll explore in the next sections.

To test the tools, we first create a file:

$ cat -v file.txt
This is line 1
This is line 2
This is line 3

We use the -v option to show special characters not normally printed. This will help us see the carriage return when added to the line endings.

3. Using vim

We can first change file endings from Unix to DOS using the vim file editor. To change a Unix file to DOS in Linux, we first open the file in vim. Then run the following command:

:set ff=dos

This command will set the file format to DOS, changing the line endings. Let’s check to see if the line endings changed:

$ cat -v file.txt
This is line 1^M
This is line 2^M
This is line 3^M

We can see that an extra character (^M) was added to the end of each line, which represents the carriage return.

4. Using sed

Another tool we can use is the sed command. To change the file from Unix to DOS, we can run this:

$ CR=$(printf '\r')
$ sed -i "s/\$/$CR/" file.txt

The first line creates a variable containing the carriage return character.

The second line uses the sed command to replace every line ending with the carriage return character. We use the -i option to edit the file in place.

We can check the file endings are changed using the cat command as we did before, or we can open the file in vim and see “[dos]” written at the bottom to let us know that the file format is DOS.

5. Using awk

The next tool we’re going to explore is awk, a language used to write small programs that run inside bash in Unix systems. We mainly use it for text extraction and manipulation.

To change a file from Unix to DOS using awk, let’s run:

$ awk -i inplace '{printf "%s\r\n", $0}' file.txt

The part inside the curly braces is the program we want to run.

This program will print the carriage return at the end of each input file line.

In our command, the input file is file.txt.

We added the option “-i inplace” to edit the file in place.

6. Using unix2dos

Finally, we’re going to explore an additional tool that we can use to change our file from Unix to DOS. This tool doesn’t come natively in Linux, but we can easily install it on any distro.

To install on Ubuntu/Debian:

$ sudo apt-get install dos2unix

To install on Fedora/Redhat:

$ sudo yum install dos2unix

After installation, we only need to run a simple command for it to work:

$ unix2dos file.txt

Of course, there are additional options that we can use with this command for more customization or special use cases. However, this is outside the scope of this article.

We can now run the cat command once more to make sure the file now contains carriage return:

$ cat -v file.txt
This is line 1^M
This is line 2^M
This is line 3^M

In the code above, we can see that the file now contains the carriage return character as expected.

Note that we can use this tool to change the files the other way around as well.

7. Conclusion

In this article, we explored the difference between Unix files and DOS files. We also discussed why we might need to change the file from one format to the other. After that, we looked at some of the common ways or tools people use to change the file format as we want.

Comments are closed on this article!