LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Convert DOS text files to UNIX text files (https://www.linuxquestions.org/questions/linux-software-2/convert-dos-text-files-to-unix-text-files-324616/)

ta0kira 05-18-2005 06:18 AM

Convert DOS text files to UNIX text files
 
I've got a lot of text files (source code) that I created in Windows, but then I moved them to Linux. Some of them retained their DOS text file format, but I would like them all to be in UNIX format. Is there a fast way to convert 50 or so files to UNIX? The way I've done it before is:

1) rename the file
2) create a new file with the old name
3) paste the text into it
4) delete the old file

I would be halfway willing to do that for the rest of them, except they all have at least one hard link, therefore they must be converted in place to retain the same inode. Thanks.
ta0kira

madluther 05-18-2005 06:30 AM

Using tr is what i'd use to convert the files

tr -d '\r' < dosfile > unixfile

Retaining the same inode may require the use of an intermediate file.

HTH

Mad

homey 05-18-2005 06:35 AM

I don't know about the hard link part but you may have success using tr to get rid of the \015 . After that is done, you could just move the new file back to the old file.
For example:
Code:

tr -d '\015' file.txt > file1.txt
mv file1.txt file.txt


syg00 05-18-2005 06:36 AM

Maybe something like ...
Code:

sed -e 's/.$//' dosfile.txt > unixfile.txt
Merely deletes the last char on each line (hopefully CR), so be careful what you feed it.

spaniel 05-18-2005 06:53 AM

dont need the sed or tr
 
There is tiny little program for it (which does basically the same as sed and tr )

dos2unix and of course unix2dos

read the manpages for details.
Programs are usually located in /usr/bin (if you installed it there of course) and present in most distributions.

ta0kira 05-18-2005 07:16 AM

Thanks. I think Slack10 uses 'fromdos' which sounds like it does the same thing. I'll try that and let you know. Hopefully it leaves my already-UNIX files alone.
ta0kira

ta0kira 05-20-2005 02:09 AM

I tried 'fromdos' which worked fine, except one or two already-UNIX were messed up. I had them backed up though. I had to use a temp file to keep the files in the same place.
ta0kira

haveanother 03-15-2011 11:42 AM

I needed to convert an entire directory of files from dos to unix format, so I have used the following combination, inspired by madluther's post.

Code:

find | sed -e "s/\(.*\)/tr -d \'\\\r\' < \1 > __\1/g" | sed -e "s/__\.\//\.\/__/" | /bin/sh
find -iname "__*" | sed -e "s/.\/__\(.*\)$/\/bin\/mv .\/__\1 .\/\1/g" | /bin/sh

Possible issues with this:
-it gives warnings about directories
-it will not work with any files in sub-directories
-probably any files starting with '__' can be badly affected


All times are GMT -5. The time now is 10:51 PM.