Pato Moschcovich
Pato Moschcovich

Reputation: 181

What's the best way to convert Windows/DOS files to Unix in batch?

Basically we need to change the end of line characters for a group of files.

Is there a way to accomplish this with a batch file? Is there a freeware utility?

Upvotes: 4

Views: 2748

Answers (4)

ityker
ityker

Reputation: 21

It could be done with somewhat shorter command.

    find ./ -type f | xargs -I {} dos2unix {}

Upvotes: 2

rickfoosusa
rickfoosusa

Reputation: 1139

Combine find with dos2unix/fromdos to convert a directory of files (excluding binary files).

Just add this to your .bashrc:

DOS2UNIX=$(which fromdos || which dos2unix) \
  || echo "*** Please install fromdos or dos2unix"
function finddos2unix {
# Usage: finddos2unix Directory
find $1 -type f -exec file {} \; | grep " text" | cut -d ':' -f1 | xargs $DOS2UNIX
}

First, DOS2UNIX finds whether you have the utility installed, and picks one to use

Find makes a list of all files, then file appends the ": ASCII text" after each text file.

Finally, grep picks the text files, Cut removes all text after ':', and xargs makes this one big command line for DOS2UNIX.

Upvotes: 0

T.E.D.
T.E.D.

Reputation: 44804

You should be able to use tr in combination with xargs to do this.

On the Unix side at least, this should be the simplest way. However, I tried doing it that way once on a Windows box over a decade ago, but discovered that the Windows version of tr was translating my terminators right back to Windows format for me. :-( However, I think in the interveneing decade the tools have gotten smarter.

Upvotes: 0

Ken
Ken

Reputation: 1301

dos2unix

Upvotes: 8

Related Questions