There might be a need to transfer files to two systems, Unix & Windows. There is a line break in DOCS and Windows files, also known as a new line. It is a fusion of two characters. The 1st character is Carriage Return (CR), whereas the 2nd character is Line Feed (\n) (LF). At the same time, the end of the line is denoted in Linux/Unix distributions by using only one character, the Line Feed (LF).
Due to this difference, issues like scripts not working correctly, code not getting compiled, and text formatting looking off are of high chances. Here, you can use the dos2unix command in Linux to use these docs per the requirements. In this guide, we will explain everything about the dos2unix command.
How to Use Dos2unix Command in Linux
Let’s install the dos2unix command in Linux. First, execute the following command in the terminal:
Note: Please use dnf instead of apt to install the dos2unix package in Fedora.
Once you install the dos2unix package in the system, run the following command to open the help option for the dos2unix command:
Now, let’s convert a Windows text file to Linux by using the basic dos2unix command. For example, we want to convert the “Linuxhint_description.txt” file which is created in Windows, so here is the basic command:
dos2unix <filename>.txt
cat -v <filename>.txt
In the previous command, the cat command is used to display the text files’ data. After converting the file successfully, we again used the cat command to ensure that the system converted the file.
Bonus Tips
In case you don’t want to use the dos2unix command, you can use the following commands to convert the Windows Doc files to Linux/Unix supported files:
Tr Command
Tr command is another option to convert a file into the Unix format by eliminating the \r line. This command-line utility is used for interpreting or eliminating the characters.
tr -d '\r' <filename>
Perl One-Liner
You can also use a Perl One-Liner command to eliminate all the \r line endings. The system can fit the Pearl one-liners scripts in a single line of code. Follow the following steps to replace all the carriage returns and line feed that ends with just line feeds:
First, open the file in the Vi/Vim text editor.
Now, you can remove all the extra characters to make changes according to the Linux/Unix requirements.
Conclusion
Dos2unix is a valuable command used most of the time for converting the DOS file to Unix. By following the exact steps of this guide, you can use the dos2unix command easily. These are some simple but essential steps, so use them carefully to achieve the goal.
We also provided some extra tips to convert the file if you cannot use the dos2unix command. However, dos2unix is a universal command that you can use on any Linux operating system. So make sure you use this command correctly to eliminate the chances of error.