LinuxCommandLibrary

unix2dos

Convert Unix line endings to DOS format

TLDR

Change the line endings of a file

$ unix2dos [path/to/file]
copy

Create a copy with DOS-style line endings
$ unix2dos [[-n|--newfile]] [path/to/file] [path/to/new_file]
copy

Display file information
$ unix2dos [[-i|--info]] [path/to/file]
copy

Keep/add/remove Byte Order Mark
$ unix2dos --[keep-bom|add-bom|remove-bom] [path/to/file]
copy

SYNOPSIS

unix2dos [options] [file ...]
unix2dos [options] -o <outfile> <infile>
unix2dos [options] < <infile> > <outfile>
The most common usage is unix2dos <filename> for in-place conversion or unix2dos -n <infile> <outfile> for converting to a new file.

PARAMETERS

-b, --add-bom
    Add a Byte Order Mark (BOM) to UTF-8 output files.

-c, --convmode CONVMODE
    Set conversion mode: ascii, 7bit, iso, mac, utf8, utf16le, utf16be, utf32le, or utf32be.

-f, --force
    Force conversion even if the file is not a regular file or appears binary.

-h, --help
    Display help message and exit.

-i, --in-place
    Edit files in place. Converts the original file directly.

-k, --keep-date
    Keep the original modification timestamp of the output file.

-n, --newfile
    Write to a new file. Requires the -o option.

-o, --outfile <outfile> <infile>
    Write converted output to <outfile> from <infile>.

-q, --quiet
    Suppress all warning and error messages.

-s, --suffix <SUFFIX>
    Append a suffix to backup files created during in-place conversion.

--
    Treat subsequent arguments as file names, even if they begin with a hyphen.

DESCRIPTION

The unix2dos command converts text files from the Unix-style line ending format (a single Line Feed, LF) to the DOS/Windows-style line ending format (a Carriage Return followed by a Line Feed, CRLF). This utility is indispensable for ensuring cross-platform compatibility, especially when transferring text files created on Unix-like systems to Windows environments, where applications typically expect CRLF line endings.

It can process multiple files, operate in-place, or write to a new output file. unix2dos intelligently handles various character encodings, including ASCII, UTF-8, and UTF-16, with options to specify the conversion mode. By default, it operates in "ascii" mode, but users can specify more complex conversions for international character sets. Part of the widely used dos2unix package, it provides a straightforward solution to a common interoperability challenge.

CAVEATS

  • Binary Files: Using unix2dos on binary files will likely corrupt them, as it treats all input as text and modifies byte sequences that resemble line endings.
  • In-Place Conversion: While convenient, the -i (in-place) option overwrites the original file. Always ensure you have backups or use the -n / -o options for critical files.
  • Encoding Issues: If the input file's character encoding is not correctly detected or specified (via -c), text may be corrupted, especially for non-ASCII characters.

LINE ENDING DIFFERENCES

Unix systems use '\n' (LF, ASCII 10), while DOS/Windows systems use '\r\n' (CRLF, ASCII 13 followed by ASCII 10). This difference is the core problem unix2dos solves.

STANDARD INPUT/OUTPUT

When no files are specified, unix2dos reads from standard input and writes to standard output, allowing it to be used in shell pipelines (e.g., cat file | unix2dos > newfile).

HISTORY

The unix2dos command is a component of the `dos2unix` package, which emerged to address the fundamental challenge of incompatible line ending conventions across different operating systems. Unix-like systems adopted the Line Feed (LF) character, while DOS and Windows systems standardized on the Carriage Return + Line Feed (CRLF) pair.

This divergence frequently caused issues like files appearing as a single long line or having extra blank lines when moved between environments. The `dos2unix` suite, including unix2dos, was developed to provide a robust and widely available utility for seamless conversion, making cross-platform file exchange much more manageable for developers, system administrators, and users alike. Its design has evolved to handle various encodings and provide flexible conversion options.

SEE ALSO

dos2unix(1), mac2unix(1), tr(1), sed(1)

Copied to clipboard