[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

dos2unix

Contents Previous Topic Next Topic

Windows and Unix do not use the same formats when storing text files: There are differences in how both ends-of-lines and ends-of-files are represented. For a full discussion on this topic, see Wikipedia. One of the more obvious symptoms is when a Unix text file is opened in Windows XP: the entire file appears in a single line. Alternatively, when a Windows file is opened under Unix, you may (depending on the editor) see extra ^M characters at the end of each line, or worse, you may run into unnecessary compilation errors or warnings.

Users of OS X should not have any problems in copying files between OS X and Unix.

Changing to the Unix Format

Once you copy a file to Windows, for example, by using either the P: drive or by using sftp, you must first convert the file using dos2unix.

For example, suppose you copied Single_list.h and Single_node.h from dos to unix. Once in Unix, you could enter:

{ecelinux:1} ls
Single_list.h  Single_node.h
{ecelinux:2} dos2unix Single_list.h Single_list.h
{ecelinux:3} dos2unix Single_node.h Single_node.h
{ecelinux:4}

Note that the file name is repeated twice. The first is the input file, the second is the output file.

Alternatively, you could try:

{ecelinux:1} tcsh
{ecelinux:2} foreach i (`ls *.h`)
foreach?    dos2unix $i $i
foreach? end
{ecelinux:3}

Moving to Windows

The equivalent command to convert a text file back into the Windows format is unix2dos.

Warning

Do not use:

{ecelinux:1} dos2unix *.h

To visualize what will happen, suppose that we had two files Single_list.h and Single_node.h. In that case, the above statement would be interpreted as

{ecelinux:1} dos2unix Single_list.h Single_node.h

and therefore, it will replace Single_node.h with the fixed version of Single_node.h.

Summary

We have discussed the utilities dos2unix and unix2dos. In a later topic, we will see how we can create our own tools which simplify some of the processes mentioned above.

Contents Previous Topic Next Topic

Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]