How to Use dos2unix Command on Linux

The dos2unix command is essentially used for file conversion in Linux. There are certain differences between files that are created in Windows and Linux. Linux administrators often have to convert files between different OS. Hence, they use the dos2unix command.

In this article, you will learn how to use the dos2unix command on Linux. This article will also serve as a guide to various dos2unix conversion examples. So, let’s get started!

Syntax

The syntax for the command looks something like this:

dos2unix [options] [FILE ...] [-n INFILE OUTFILE ...]

The command takes two arguments: options and a filename.

Use dos2unix Command on Linux

The files created in Dos/Windows-based operating systems have different formats compared to Linux-based files. For instance, DOS files use \r and \n at the end of each line. On the other hand, Linux/Unix files use only \n escape sequences. Hence, it is essential to convert files when transferring them.

Let’s look at the guide on how to use this command on Linux. 

1. Update the System

Firstly, update the system before installing the command. Open the terminal using the shortcut key “Ctrl + Alt + T”. Next, use the update command. 

For example:

sudo apt update

The output should look something like this:

use dos2unix command on Linux

2. Install the dos2unix Command

Secondly, install the dos2unix command. For this step, use the install command as shown below:

sudo apt install dos2unix

You should get a similar output:

use dos2unix command on Linux

Wait for the installation to complete. 

3. Convert Files Using dos2unix Command

Now that the command is successfully installed, you can use this to convert the DOS files. For example, to convert a windows-based file without saving its original format, type the command given below:

dos2unix [file_name]

However, if you want to retain the original file with the same name, use the -b option with the dos2unix command. Specifically, type:

dos2unix -b myFile.txt

Output:

use dos2unix command on Linux

4. Conversion Modes in dos2unix Command

There are various conversion modes concerning encoding types. For ASCII encoding, dos2unix has “ascii” mode, which is the default conversion mode. This converts UTF-8 files into ASCII mode. 

However, special characters in windows-based files that do not have an equivalent character in ISO-8859-1 get converted into dots. Some examples of conversion modes are: 

# Convert DOS-based files to Unix Latin-1:
dos2unix -iso -n myFile.txt outFile.txt

# Convert from DOS-based CP850 to Unix Latin-1:
dos2unix -850 -n myFile.txt outFile.txt

On the other hand, if you are unsure of the file mode, you can check it. For this step, you will use the vim editor. Type the command given below to open the file with vim editor:

vim myFile.txt

If you do not have it installed, you can simply install it here. Next, in command mode, type the following command:

:set ff?

Press the “Enter” key, and you will see a similar output as this:

find file type for dos2unix command

Since this is a UNIX-based file, it is showing fileformat=unix. After that, exit the vim editor using the :q command. Another way to check the file format is to use the od command. Specifically, type:

od -bc sample1.txt

4. Recursive Conversion of Multiple Files

In the dos2unix command, you can also perform recursive conversion of several files. In this case, the dos2unix command is used in combination with the find and xargs command. For this step, type: 

find . -name *.txt | xargs dos2unix

5. Dos2Unix Options

You can use the dos2unix command with a variety of options. These options provide flexibility in modifying the output as needed. Let’s look at some of the options in detail.

To view help related to the dos2unix command, use the help option. Specifically, type:

dos2unix -h

The output should look something like this:

dos2unix options

Alternatively, you can use the –help flag as well.

In addition, you can also check the version of the dos2unix command. Execute the command with the -v or –version flag. 

Input:

dos2unix -v

Output:

dos2unix version

When performing a recursive conversion, you can skip binary files in the process. For this step, use the -s flag. For example:

dos2unix -s /home/Documents

On the other hand, you can force convert binary files as well. This will require you to use the -f option. For instance, type: 

dos2unix -f /home/Documents

To avoid the warnings generated by the dos2unix command, suppress them using the quiet option. Execute the command given below:

dos2unix -q myFile.txt 

You can also save the timestamps of your converted files. Since the conversion changes the creation date of the converted files, you can use the dos2unix command to retain the original creation date. Use the -k option with the dos2unix command. For example:

dos2unix -q myFile.txt 

6. Convert Current File and Save in New File

Using the dos2unix command, you can save the converted file in a new file. For instance, you can convert myFile.txt and save the converted file in newFile.txt. You will use the -n option for this step. Type:

dos2unix -n myFile.txt newFile.txt

7. Keep the Timestamp of the Converted File

You can also save the converted file and keep the timestamp of the old file. This can be achieved by combining two options: -k and -n. For example:

dos2unix -k -n myFile.txt newFile.txt

And that’s a wrap! By following this guide, you can now comfortably convert files between DOS and UNIX operating systems. What we have covered in the article is just the top of the surface. To learn more about this command, visit their official website. You can also convert files from UNIX to DOS by using its sibling command unix2dos

We hope you found this article to be helpful. If you have any handy examples on dos2unix, please let us know in the comments section, and we’ll try to answer them in the next articles. 

If this guide helped you, please share it.

Leave a Reply
Related Posts