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.
Converting Files on Linux
There are several ways you can transfer files to use the appropriate line endings. Convert the files using the:
sedcommand (see Linux sed command tutorial)
- Vi/Vim text editor (see Vim commands cheat sheet)
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
sudo dnf install dos2unix
If you download a file created in DOS/Windows onto your Linux system, you can convert it using the
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:
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
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
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:
: and type in the following Vi/Vim command (making sure you type
Ctrl+m instead of
:%s/^ m //g
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.
: to prompt the command line.
3. Type in the following command and press Enter:
perl -pi -e 's/\r\n/\n/g' [file_name]
You should see the the changes in the file right away.
Example: Converting a Sample File from DOS to Unix Format
Let’s say you downloaded a file named sample-dos-file.
By opening the file using the Vim/Vi text editor, you would see that after each line ending there is
^M (a carriage return).
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).
Now, to convert the file and remove all carriage return endings, you would run the command:
Open the same file in Vim/Vi. It doesn’t include any
^M symbols signaling the line ending.
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.
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.