Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 05-18-2005, 07:18 AM   #1
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.
Old 05-18-2005, 07:30 AM   #2
Registered: Aug 2004
Distribution: LFS
Posts: 350

Rep: Reputation: 30
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.


Old 05-18-2005, 07:35 AM   #3
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 58
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:
tr -d '\015' file.txt > file1.txt
mv file1.txt file.txt

Last edited by homey; 05-18-2005 at 07:36 AM.
Old 05-18-2005, 07:36 AM   #4
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,634

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
Maybe something like ...
sed -e 's/.$//' dosfile.txt > unixfile.txt
Merely deletes the last char on each line (hopefully CR), so be careful what you feed it.
Old 05-18-2005, 07:53 AM   #5
Registered: Mar 2005
Location: Netherlands
Distribution: Fedora Core 5 x86_64
Posts: 48

Rep: Reputation: 15
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.
Old 05-18-2005, 08:16 AM   #6
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
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.
Old 05-20-2005, 03:09 AM   #7
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
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.
Old 03-15-2011, 12:42 PM   #8
LQ Newbie
Registered: Jan 2011
Posts: 13

Rep: Reputation: 0
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.

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert text files to html files lothario Linux - Software 3 09-27-2005 09:48 PM
editing text files killertofu Linux - Newbie 2 10-15-2004 06:55 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 02:54 AM
how to convert windows formatted text files to *nix formatting? zero79 Linux - General 8 06-11-2004 04:00 AM
Unix to DOS text Wynd Programming 5 04-18-2003 01:14 PM

All times are GMT -5. The time now is 02:09 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration