| by Arround The Web | No comments

Dos2unix Linux Command

“Are you looking for a way to convert DOS or Mac format to Unix format? Any time you need to share files between Windows and Unix systems, the file format, especially plain-text, comes into play. The main difference is in the line break. For Unix and Linux systems, an end of a line gets represented by a single character, the Line Feed (LF).

In contrast, Windows files use two characters to signify a line break, the carriage return (CR), represented as (\r), and the Line Feed (LF), expressed as (\n).

Unless you have the right way of converting the files, you will have a broken script, code, or formatting, which is annoying. Here’s the good part, this guide will present examples of the dos2unix command usage to get you started. Check it out!”

Converting Windows/DOS Text File to Unix

The dos2unix is a utility that allows converting DOS text files to Unix format without affecting the structure and formatting of the text. You first need to install the utility on Unix systems to use it.

$ sudo apt-get install dos2unix

With the tool installed, the next step is to convert your file.

The basic syntax to use dos2unix is:

$ dos2unix [options] [file-to-convert]

How to Confirm the File Format?

At times, if you are unsure whether the file you wish to convert is a DOS format, you can use the vim editor or command line to confirm the format. In our case, we are using a sample1.txt file located in the Downloads folder as our DOS file.

To use vim to confirm the format, enter the command below on the terminal.

$ vim [filename]

If you don’t have vim editor installed, install it using the command:

$ sudo apt-get install vim

Once the file opens in the vim editor, you can check the format by typing

:set ff?

Hit enter, and you should see that the current file format is set to fileformat=dos like in the case below.

You can exit vim by typing:q, then click enter to get back to the terminal.

Alternatively, you can use the command below to check the file format:

$ od -bc sample1.txt

You should check to ensure the lines end with \r \n to confirm the DOS format in the output.

Using dos2unix to Convert DOS to Unix

Now that you are sure your file needs conversion, the command to do so is pretty simple. However, ensure you are in root to execute it as shown.

To convert without saving its original format:

$ sudo dos2unix sample1.txt

To convert and still save the original file:

$ sudo dos2unix -b sample1.txt

The file will be converted, and a backup of your original DOS file will be created with a .bak extension under the same name.

That’s it! You now have the same file but in Unix format. You can verify the file’s current format as we saw above using either vim or the terminal. It should reflect a Unix format.

To check using vim, the output will be:

To check using the command line:

$ od -bc sample1.txt

The output will be:

You can note that the end line is \n to show its Unix format. Bingo! You’ve done it. The main aim of the dos2unix command is to remove the \r in the \r\n DOS format to leave only the \n to match the Unix format.

Dos2unix Useful Options

You can combine various options with the dos2unix command to achieve more functionality. They include:

1. Get Help

Like any Linux command, you can easily get the help page for dos2unix using -h or –help:

$ dos2unix -h

2. Skip Binary Files

By default, binary files get skipped during conversion. The option used is -s or –safe

3. Force Binary File Conversion

If you want to convert the binary files, add the -f or –force option.

4. Print Version

To check the dos2unix version, use the -V or –version option.

5. Quiet Mode

Warnings and messages can get annoying. Luckily, you can suppress them using the -q or –quiet options.

6. Retain Date Stamp

When converting DOS to Unix, the timestamp of the input file will change unless you restrict it. To retain the same timestamp for the output and input file, use the -k or –keepdate options.

$ dos2unix -k sample1.txt


You can’t keep away from transferring files between Windows, Mac, and Unix. With the dos2unix command, you need not worry about how you will convert the files. You’ve learned how to comfortably use the dos2unix command for an easy and fast conversion.

Share Button

Source: linuxhint.com

Leave a Reply