Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert files from Dos to Unix

Tags:

java

dos2unix

I am having several files which I want to convert from Dos to Unix. Is there any API or method which will help me to do this?

like image 673
Code Hungry Avatar asked Feb 21 '12 09:02

Code Hungry


People also ask

What is DOS to Unix?

unix2dos is a tool to convert line breaks in a text file from Unix format (Line feed) to DOS format (carriage return + Line feed) and vice versa. dos2unix command : converts a DOS text file to UNIX format. Unix2dos command : converts a Unix text file to DOS format.

How do I convert a file in Linux?

Open Handbrake and click on Source. Then, select the file you want to convert; once it's loaded, click on the Enqueue button, and it will add the file to the queue. Click on Source again, select the next file, and add it to the queue. Repeat the process to add all the files that you want to convert (Figure 4).


2 Answers

There are linux tools that can do this (dos2unix for example).

In Java it can be done with String.replaceAll().

DOS uses \r\n for line termination, while UNIX uses a single \n.

String unixText = windowsText.replaceAll("\r\n", "\n"); // DOS2UNIX

So no, no API exists. Yes, it is dead easy.

like image 186
parasietje Avatar answered Oct 02 '22 01:10

parasietje


There is a utility/command in Linux/Unix called dos2unix that will help you convert your files from dos to unix format. To install simply type in console(you may need root privileges)

yum install dos2unix

To do the conversion you have to use command dos2unix followed by filename. For example

[aniket@localhost ~]$ dos2unix sample.txt 
dos2unix: converting file sample.txt to UNIX format ...

For all files in a directory you can simply use

dos2unix *
like image 22
Aniket Thakur Avatar answered Oct 02 '22 01:10

Aniket Thakur