I need to be able to go back and forth between CentOS and Windows for C++ projects that I am developing on both systems. I want to be able to convert all files of a given type such as *.cpp, *.h or *.txt from Unix to Dos.

I have a bash script that converts a single file, but I'd like to be able to type in either just the extension of the file type or *.FILETYPE to convert all the files in a given directory of that type.

Here is the working script, what do I need to do to make it work for more than one file at a time. I've tried using a for loop but I get syntax errors.

#! /bin/sh

# Convert a text based file from Unix Format to Dos Format
# The Output file should be the name of the input file with
# WF inserted before the file extention
# Examples
#    convert Unix format abc.txt to abcWF.txt Windows Format 
#    convert Unix format abc.cpp to abcWF.cpp Windows Format 

echo "In u2w FileToConvert = $1"
    FileBase=$(basename $1)
    commandment="unix2dos -n $1 $OutputFile"
    echo $commandment
    unix2dos -n $1 $OutputFile
    echo "diff -w $1 $OutputFile"
    diff -w "$1" "$OutputFile"


Example run

$  bd2w TestResults.txt
In u2w FileToConvert = TestResults.txt
unix2dos -n TestResults.txt TestResultsWF.txt
unix2dos: converting file TestResults.txt to file TestResultsWF.txt in DOS format ...
diff -w TestResults.txt TestResultsWF.txt
  • @drewbenn I use github for version control, unfortunately this project isn't ready to go into github yet, because I don't have any private repositories and the code isn't RFUBO.
    – pacmaninbw
    Sep 16, 2016 at 22:30
  • I would suggest to make a policy to use exactly one file format. Use editors which can handle both file formats. Slap everyone who violates your policy. Sep 17, 2016 at 8:42

2 Answers 2


Unix to DOS

To convert, for example, all *.h files to DOS format:

sed -i '/\r/! s/$/\r/' *.h

The condition /\r/! tells sed to skip all lines that already have a carriage return. Because of this, it is safe to run this command multiple times on the same file.

The substitution command, s/$/\r/, adds a carriage return to the end of each line.

The option -i tells sed to change the file in-place. If one wanted to keep backups of the originals:

sed -i.bak '/\r/! s/$/\r/' *.h

DOS to Unix

To convert in-place all *.h files from DOS to Unix:

sed -i 's/\r$//' *.h

This command is also safe to run multiple times on the same files.


For BSD(OSX) sed, the argument to the -i option is mandatory, not optional. Thus, use -i.bak to make a backup with extension .bak, or use -i '' to change the files in-place without a backup.


Use the find to search for the files you want to convert and add the -exec (or -execdir) action to run the conversion program. For example, to run bd2w on all .cpp and .h files starting from the current directory, use the following command:

find . -type f -name "*.cpp" -o -name "*.h" -exec bd2w '{}' \;

I must say I share the concerns expressed by the commenters above; aren't you making your life unnecessarily hard by going through hoops like this? Even if you are the only developer, if the code lives on two different computers, I'd suggest you set up git locally. Put the repo on a removable device to/from you can push/pull the sources. This will prevent any mistakes trying to keep the sources up to date on both machines and it will solve your encoding problems.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .