Introduction

Files created in DOS/Windows use carriage return (\r) and line feed (\n) for line endings. However, files in Unix/Linux solely use line feed.

Therefore, when transferring a file from one system to another, make sure to convert the files.

In this tutorial, you will learn how to transfer files from DOS to Unix and vice-versa.

Guide on how to transfer DOS to UNIX files.

Converting Files on Linux

There are several ways you can transfer files to use the appropriate line endings. Convert the files using the:

  • dos2unix command
  • unix2dos command
  • sed command
  • tr command
  • Vi/Vim text editor (see Vim commands cheat sheet)
  • perl one-liner command

Option 1: Converting DOS to UNIX with dos2unix Command

The simplest way to convert line breaks in a text file is to use the dos2unix tool.

Install the tool by running the command:

sudo apt install dos2unix

or:

sudo dnf install dos2unix

The command to install dos2unix tool on Linux systems.

If you download a file created in DOS/Windows onto your Linux system, you can convert it using the dos2unix command:

dos2unix [file_name]

The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name. Doing so creates a backup file under the same name and the .bak extension.

dos2unix -b [file_name]

Option 2: Converting UNIX to DOS using the unix2dos Command

To convert a file created on a Linux system into the DOS format, run the command:

unix2dos [file_name]

Alternatively, add the -b attribute to save the original file as backup:

unix2dos -b [file_name]

Option 3: Using the sed Command

You can also use the sed (stream editor) command to remove the carriage return line endings. The command syntax to do so is:

sed 's/^M$//' [original_file_name]>[converted_file_name]

Instead of just typing ^M, press Ctrl+V followed by Ctrl+M to enter the carriage return symbol. When using the sed command, specify the name of the DOS file [original_file_name] and how you want to name the converted file [converted_file_name].

To change the file format from Unix to DOS, use the command:

sed 's/$/^M/' [original_file_name]>[converted_file_name]

Option 4: Using the tr Command

Another way to convert a file into the Unix format is to remove \r line endings with the tr command. The tr command is a command line utility for translating or deleting characters.

Use the command in the following format:

tr -d '\r' < [original_file_name]>[converted_file_name]

Option 5: Using the Vim Text Editor

You can also remove carriage return line endings from files in DOS format using the Vi/Vim text editor.
Open the file in Vi/Vim:

vi [file_name]

Then press : and type in the following Vi/Vim command (making sure you type Ctrl+V then Ctrl+m instead of ^m):

:%s/^ m //g

Remove the carriage return line ending using the vi text editor.

Option 6: Using a Perl One Liner

Lastly, you can use a Perl one-liner command to delete all \r line endings. Pearl on-liners are scripts that fit in a single line of code.

To replace all carriage return and line feed endings with just line feeds:

1. Open the file in the Vi/Vim text editor.

2. Press : to prompt the command line.

3. Type in the following command and press Enter:

perl -pi -e 's/\r\n/\n/g' [file_name]

Delete carriage return endings using a Perl one-liner command.

You should see the the changes in the file right away.


Note: Need help finding files on your Linux system? Take a look at how to use the find command or how to show hidden files in Linux.


Example: Converting a Sample File from DOS to Unix Format

Let’s say you downloaded a file named sample-dos-file.

A sample file created in Windows.

By opening the file using the Vim/Vi text editor, you would see that after each line ending there is ^M (a carriage return).

Opening a DOS file in Vi to see carrier return symbols at line endigns.

Another way to see the file uses both carriage return and line feed for line endings is to view the file in octal values.

To do so, run the command:

od –bc sample-dos-file.txt

The output displays the content of the file with its octal values, as in the image below. You can see that each line end is marked with the octal values 015 (\r) and 012 (\n).

See end of line marks in octal values.

Now, to convert the file and remove all carriage return endings, you would run the command:

dos2unix sample-dos-file

Convert a dos file into the Unix format.

Open the same file in Vim/Vi. It doesn’t include any ^M symbols signaling the line ending.

Open a file in the Vi text editor.

To confirm the file is in Unix format, you can also open view the content in octal values. The output should display just the 012 values for \n.

Display Unix file content in octal values.

Conclusion

This article showed six ways to convert a DOS file into a Unix format and vice versa.

The simplest and recommended way is using the dos2unix command. If you cannot utilize dos2unix, you have five other options to choose from.


Next you should also read