Fact-checked by Grok 2 weeks ago

unix2dos

unix2dos is a command-line utility primarily used on operating systems to convert text files from the Unix line ending convention (a single line feed character, LF or \n) to the /Windows convention (carriage return followed by line feed, CRLF or \r\n). This conversion ensures compatibility when transferring files between Unix/ environments and Windows systems, where differing line endings can cause display or processing issues in editors and scripts. In addition to line ending transformation, unix2dos can convert ISO 8859-1 standard characters to the corresponding extended character set (), facilitating cross-platform character compatibility. The unix2dos tool is included in the broader dos2unix software package, which also provides the reciprocal dos2unix command for converting DOS/Mac files to Unix format, as well as mac2unix and unix2mac for handling classic Mac line endings (CR or \r). Originally developed as a standalone utility, unix2dos was bundled into the dos2unix package starting with version 5.0 in 2010, streamlining distribution and maintenance. The package supports additional features such as line ending conversions for UTF-8 and UTF-16 files, and support for other encodings like GB18030, safe binary file detection to prevent corruption, and options for in-place editing, stdout output, and informational summaries of file differences. The origins of dos2unix and unix2dos trace back to 1989, when the initial versions were authored by John Birchfield for . In 1995, Benjamin Lin rewrote the tools from scratch, enhancing their portability and functionality. Subsequent contributions included Mac format support added by Bernd Johannes Wuebben in 1998 and Unix-to-Mac conversion by current maintainer Erwin Waterlander in 2010. The project, hosted on since 2002, has evolved to support multiple platforms including , Windows (via ), DOS, and , and is licensed under a GPL-compatible license. As of version 7.5.3 (released October 14, 2025), it remains actively maintained, with ongoing updates for modern encoding standards and internationalization.

Text File Line Endings

Unix Format

In Unix-like systems, text files use a single Line Feed (LF) character to mark the end of each line. This LF corresponds to ASCII code 10, represented in hexadecimal as 0x0A. In source code and textual representations, it is typically denoted as \n. For example, a two-line text file containing "Hello" followed by "World" would appear in binary as the bytes for "Hello" (including any trailing spaces if present), succeeded by the byte 0x0A, then the bytes for "World", and another 0x0A at the end. This line-ending convention originated in the early development of Unix at during the 1970s, as described in foundational system documentation where text files are defined as sequences of characters delimited solely by the (LF) character. It drew from teletype terminal standards of the time, which influenced Unix's efficient handling of output devices by relying on LF to advance the print head to the next line. The adoption of a single-character terminator reflected Unix's emphasis on simplicity and minimal resource use in file storage and processing. A key implication of the Unix LF format is its effect on file portability across operating systems. When such files are opened in traditional Windows text editors like —prior to the 2018 update that added LF support—they often display as a single unbroken paragraph, with line breaks unrecognized and rendered incorrectly, potentially causing confusion or errors in cross-platform workflows. In contrast to the and Windows format, the Unix approach uses only LF rather than a two-character sequence.

DOS and Windows Format

In DOS and Windows environments, text files employ a line ending convention consisting of a Carriage Return (CR) followed by a Line Feed (LF), corresponding to ASCII codes 13 (0x0D) and 10 (0x0A). This sequence, known as CRLF, signals the end of a line by first returning the cursor to the beginning of the line (CR) and then advancing to the next line (LF). Unlike the Unix format, which relies solely on LF, the CRLF pair ensures compatibility with legacy hardware behaviors. The rationale for this convention stems from mechanical typewriters, where CR physically slid the carriage to the left margin and LF rotated the platen to feed paper downward. In computing, early systems like in the late 1970s adopted CR+LF to interface with serial terminals and printers mimicking typewriter actions, a standard that carried forward into the for broad hardware support. This approach impacts file storage by appending an additional byte per line ending, thereby increasing overall relative to LF-only formats. Furthermore, when CRLF-terminated files are processed or displayed on Unix systems without , the unhandled often repositions the cursor prematurely, resulting in overprinted text or garbled output in tools like cat. For illustration, a simple line in a DOS-formatted might end with the following binary representation:
Hello, world!\r\n
In , this appears as 48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21 0D 0A.

of Variations

The of line ending conventions traces back to the American Standard Code for Information Interchange (ASCII), standardized in , which defined (CR, ASCII 0D) and line feed (LF, ASCII 0A) as distinct control characters to accommodate mechanical teletypes and printers. On these devices, CR moved the print head to the beginning of the line, while LF advanced the paper to the next line without resetting the position, necessitating their separate use for proper formatting in early computing environments. In the 1970s, Unix developers at opted for LF alone as the delimiter, prioritizing simplicity, space efficiency, and a unified end-of-line sequence over the dual-character approach, which they viewed as inefficient for digital systems without mechanical constraints. This choice, implemented from Unix's inception around 1971, reflected the system's philosophy of minimalism and became the foundation for subsequent standards. By contrast, , released in 1981, adopted the CR+LF sequence to ensure compatibility with and earlier DEC operating systems, which had inherited it from teletype hardware requirements for reliable cursor and paper advancement. Apple's original Macintosh operating system, introduced in 1984, diverged further by using CR-only line endings, aligned with its custom text-handling engine designed for the machine's graphical interface and early hardware limitations. This format persisted through versions until Mac OS X's release in 2001, which, built on a BSD foundation, deprecated CR in favor of LF to align with broader Unix conventions and facilitate cross-platform . These divergent conventions created persistent challenges in cross-platform software development, as mismatched line endings could corrupt file rendering or introduce invisible artifacts in shared codebases. To mitigate this, tools like Git introduced the core.autocrlf configuration option in 2007, enabling automatic normalization of line endings during repository operations to preserve consistency across Windows, Unix, and macOS environments.

Core Functionality

Conversion Process

The unix2dos utility performs line ending conversion by processing the input file on a byte-by-byte basis, replacing each Unix-style line feed (LF, ASCII 10) with the DOS-style followed by line feed (CRLF, ASCII 13 followed by 10). This ensures compatibility with and Windows text file conventions, where line breaks require both characters. The core algorithm involves reading the input stream sequentially. For each byte encountered:
  • If the byte is an LF, the tool outputs a immediately followed by the LF.
  • Otherwise, the byte is output unchanged.
This transformation preserves the original content while standardizing line terminators. The process continues until the end of the input is reached, with the modified stream written to the output destination. In handling edge cases, unix2dos addresses files lacking a terminating line ending on the final line by optionally adding a at the end, preventing incomplete line rendering in environments. For files with mixed line ending formats—such as interspersed LF, , or even Mac-style (ASCII 13)—the tool converts all LF instances to while leaving existing sequences intact, though it may report statistics on the original format distribution if requested. To prevent corruption of non-text files, unix2dos distinguishes between text and modes through initial scanning. files are detected by the presence of non-printable characters (e.g., bytes or bytes below 32 excluding common whitespace like , , and LF), and conversion is skipped by default to avoid unintended modifications such as altering embedded that might mimic line endings. Users can force conversion in mode if needed, but this risks for executables, images, or compressed archives. The following illustrates the fundamental conversion loop, excluding detection and I/O details:
input = read_input_file_or_stdin()
output = empty_buffer()

for each byte in input:
    if byte == LF:
        append [CR](/page/CR) to output
        append LF to output
    else:
        append byte to output

# Optionally add final CRLF if last line lacks ending
if input ends without line terminator:
    append [CR](/page/CR) to output
    append LF to output

write output to file_or_stdout()

Input and Output Handling

The unix2dos utility performs in-place conversion by default, overwriting the original input file after processing it through a to ensure atomic replacement upon successful completion. This approach minimizes the risk of partial writes but requires write permissions on the input file; if the file is read-only or ownership cannot be preserved, the conversion aborts unless the --allow-chown option is specified. Specifying an output file with the -n option enables separate output handling, where the converted content is written to a new file without modifying the input. This mechanism allows for non-destructive processing, particularly useful when dealing with critical data. unix2dos supports input from standard input (stdin) and output to standard output (stdout), facilitating in pipelines, such as cat multiple_files.txt | unix2dos > converted.txt, without loading entire files into . This streaming capability is efficient for large files, as the tool processes data sequentially rather than buffering the full content, though in-place file conversions still rely on temporary files for safety. In terms of error handling, unix2dos checks for file permissions and aborts with a non-zero exit code if it cannot read the input or write the output due to access restrictions, including read-only status. For invalid encodings, such as malformed UTF-16 sequences during optional handling, the tool skips problematic sections or files and reports errors with line numbers where possible, exiting gracefully in quiet . Binary files are detected and skipped by default to prevent corruption, unless forced with the -f option, ensuring text-only operations.

Command-Line Usage

Basic Syntax

The basic syntax of the unix2dos command follows the structure unix2dos [options] [input_file] [output_file], where options modify behavior, input_file specifies the source (or standard input if omitted), and output_file designates the destination (or overwrites input_file if not provided). This allows flexible invocation, either processing files directly or acting as a filter in pipelines. For simple in-place conversion of a Unix-format to format, the command unix2dos file.txt reads the input, appends carriage returns to line feeds as needed, and overwrites the original file. To avoid modifying the source, users can redirect streams, such as unix2dos < input.txt > output.txt, which processes standard input and writes to standard output without altering the original. The unix2dos tool is included in the dos2unix package, which is standard across major Linux distributions like , , and . On macOS, it can be installed via Homebrew with brew install dos2unix. For Windows environments, availability comes through , where the package provides compatibility including unix2dos. This syntax aligns with the dos2unix package version 7.5.3, released on October 14, 2025, incorporating refinements to command handling while maintaining .

Common Options

The unix2dos utility provides several command-line options to customize the conversion of Unix-format text files to DOS/Windows format, allowing users to control output handling, file preservation, and conversion behavior. One of the most frequently used options is -n, which enables new file mode by specifying an input file and an output file; this prevents overwriting the original file during conversion. For instance, unix2dos -n input.txt output.txt converts input.txt and writes the result to output.txt without modifying the source. To preserve file metadata, the -k option maintains the original timestamp (date and time) of the input file on the converted output, which is useful for version control or logging purposes. An example combining these is unix2dos -n -k file.txt newfile.txt, which converts file.txt to format and saves the result as newfile.txt while keeping the original timestamp. For handling character sets and end-of-file markers, -c ascii mode performs a basic conversion focused on line endings (LF to CRLF) and ignoring other formatting differences, making it suitable for standard ASCII/UTF-8 text files. In contrast, -c iso mode enables 8-bit clean conversion between code pages and ISO-8859-1, which is essential for files containing extended characters without data corruption. Output control options include -v for , which logs details such as the number of line breaks converted and any (BOM) presence, aiding in or verification. Conversely, -q activates quiet mode, suppressing all non-error messages to streamline batch operations.

History and Development

Origins in Unix Tools

The unix2dos utility originated in the late as a straightforward designed to address issues in text file sharing between Unix systems and early personal computers running . Developed initially for 4.0 by John Birchfield around 1989, it converted Unix-style line endings (LF) to DOS-style (CRLF), building on existing conversion mechanisms in to support seamless data exchange in an era of growing cross-platform needs. During the 1990s, similar functionality appeared in some BSD variants through the flip utility, which handled line-ending conversions between Unix LF and CRLF formats to aid file interchange on BSD-based systems. This tool, distributed via academic repositories like those at , reflected the broader Unix ecosystem's response to interoperability challenges. A significant milestone came in 1995 when Benjamin Lin rewrote the utility from scratch, releasing a standalone version distributed through SunSITE.unc.edu (now ibiblio.org), which enhanced its portability and accessibility beyond proprietary Unix environments. By the mid-1990s, unix2dos had gained adoption in —the commercial successor to —and various other Unix systems, establishing it as a reliable for format conversions in professional and academic settings.

Integration with dos2unix Package

The unix2dos utility became integrated into the dos2unix project to enable comprehensive bidirectional format conversion within a unified package. In 2010, the previously separate dos2unix and unix2dos packages were bundled together as dos2unix version 5.0, streamlining development, distribution, and user access to both conversion directions. Key enhancements in subsequent releases have focused on broader format support and platform reliability. Version 6.0, released in 2012, introduced handling for UTF-16 encodings, extending the tool's utility to modern internationalized text files. Version 7.5.1, issued on August 29, 2023, resolved issues with conversions across file systems. The dos2unix package, encompassing unix2dos, is distributed under the BSD License (GPL-compatible), ensuring open-source accessibility and community contributions. Its source code resides on , where maintenance continues actively; as of version 7.5.3 (released October 14, 2025), it incorporates fixes for exit codes in binary and aborted conversions, and updated translations. Cross-platform adoption has further solidified the package's integration. Since Android 6.0 in 2015, unix2dos has been included in Android's Toybox implementation, facilitating line-ending conversions in embedded and mobile contexts. It is also readily available within the Windows Subsystem for Linux (WSL) via major distributions like Ubuntu, supporting seamless interoperability in hybrid environments.

Native System Commands

On Unix and Linux systems, the built-in tr command provides a simple method for converting Unix-style line endings (LF) to DOS-style (CRLF) by translating each newline character. The command tr '\n' '\r\n' < file.txt > out.txt replaces every \n with \r\n, effectively adding a carriage return before each line feed. This approach works for basic text files but requires preprocessing to remove any existing \r characters (e.g., via tr -d '\r') to avoid corruption if the input mixes formats. In Windows environments, , introduced in 2006 as a native component starting with , offers a straightforward script for the conversion. The one-liner (Get-Content file.txt -Raw).Replace("n", "rn") | Set-Content out.txt reads the entire file as a raw string, substitutes every [newline](/page/Newline) (\n) with the Windows [newline](/page/Newline) sequence (\r\n`), and writes the output. This method handles the file holistically, preserving content while normalizing endings, though it assumes or ASCII encoding by default. For macOS, which includes as a standard component, a Perl one-liner enables quick conversion without additional installations. The command perl -pe 's/\n/\r\n/' file.txt > out.txt processes the file line by line, substituting each \n with \r\n using a . This leverages Perl's built-in text processing capabilities, suitable for scripting on Unix-derived systems like macOS. These native commands offer lightweight, no-install solutions for ad-hoc conversions but come with limitations compared to dedicated tools like unix2dos, such as no automatic backups, potential issues with files lacking a trailing (resulting in an unterminated last line), and limited error handling for mixed or binary content, reducing their robustness in production workflows.

Third-Party Utilities

Notepad++, a free for Windows first released in , includes built-in end-of-line (EOL) conversion through its Edit > EOL Conversion menu, allowing users to switch between Windows (CRLF), Unix (LF), and Macintosh () formats directly within the editor. This feature has been available since at least version 5.7 in and supports plugin extensions for automated . The Vim text editor, available since 1991, enables unix2dos-like conversion using the command :set ff=dos in normal mode, which adjusts the file format to CRLF before saving; this can be applied to individual buffers or set globally via the fileformats option in the configuration file. Vim's approach integrates seamlessly with its modal editing, supporting detection and conversion of mixed line endings without external tools. Online tools for line ending conversion, such as the LF and CRLF converter at execeratics.com, emerged around and allow web-based processing of text files by uploading content and selecting Unix-to-DOS output. Similar services, like those at toolslick.com, provide no-install options for quick conversions, often handling small files without requiring software downloads. Compared to the standard command-line unix2dos, these third-party utilities frequently incorporate graphical user interfaces for visual editing, for multiple files, and additional features like encoding detection, enhancing usability for non-technical users while maintaining core EOL conversion capabilities.

References

  1. [1]
    dos2unix / unix2dos - Text file format converters - SourceForge
    The first versions were made by John Birchfield in 1989, and in 1995 rewritten from scratch by Benjamin Lin. Mac to Unix conversion was added by Bernd Johannes ...
  2. [2]
    unix2dos(1) — dos2unix — Debian testing — Debian Manpages
    Jan 22, 2024 · The Dos2unix package includes utilities "dos2unix" and "unix2dos" to convert plain text files in DOS or Mac format to Unix format and vice versa.
  3. [3]
    unix2dos - man pages section 1: User Commands
    Jul 27, 2022 · The unix2dos utility converts ISO standard characters to the corresponding characters in the DOS extended character set.
  4. [4]
    None
    Nothing is retrieved...<|control11|><|separator|>
  5. [5]
    ASCII Code 10 - Line Feed
    In the 7-bit ASCII character set, ASCII code 10 is represented by the control character ␊ also known as the line feed. Symbol, ␊. Unicode Symbol, ␊. Decimal, 10.
  6. [6]
    The UNIX time-sharing system - ACM Digital Library
    No particular structuring is cxpected by the system. Files of text consist simply of a string of characters, with lines demarcated by the new-line character.<|control11|><|separator|>
  7. [7]
    Difference Between CR LF, LF, and CR Line Break Types - Baeldung
    Jan 23, 2024 · The LF line break type originated from the Unix operating system which is compatible with the ASCII standard. Also, the ASCII standard ...
  8. [8]
    Introducing extended line endings support in Notepad
    May 8, 2018 · This means that Notepad was unable to correctly display the contents of text files created in Unix, Linux and macOS.
  9. [9]
    ms dos - Why is Windows using CR+LF and Unix just LF when Unix ...
    May 3, 2018 · Windows and MS-DOS use the control characters CR+LF (carriage return ASCII 13 followed by line feed ASCII 10) for new lines, while Unix uses just LF.Missing: standards | Show results with:standards
  10. [10]
    Difference between CR LF, LF and CR line break types
    Oct 12, 2009 · CR and LF are control characters, respectively coded 0x0D (13 decimal) and 0x0A (10 decimal). They are used to mark a line break in a text file.Historical reason behind different line ending at different platformsnewline - Difference between \n and \r? - Stack OverflowMore results from stackoverflow.com
  11. [11]
    The Great Newline Schism - Coding Horror
    Jan 18, 2010 · This Visual Studio dialog presents the following five (!) possible set of line endings for the file: Windows (CR LF); Macintosh (CR); Unix (LF) ...
  12. [12]
    CRLF vs. LF: Normalizing Line Endings in Git
    Apr 18, 2021 · Meanwhile, from its very inception, Unix used LF to denote line endings, ditching CRLF for consistency and simplicity. Apple originally used ...
  13. [13]
    Why does my tool output overwrite itself and how do I fix it?
    Aug 19, 2017 · The problem is that your input file uses DOS line endings of CRLF instead of UNIX line endings of just LF, and you are running a UNIX tool on it.Why can't you use cat to read a file line by line where each line has ...linux - How to find out line-endings in a text file? - Stack OverflowMore results from stackoverflow.com
  14. [14]
    ASCII character set - Lammert Bies
    Originally, the LF character was meant to move the head of a printer one line down. A second control character CR would then be used to move the printing head ...
  15. [15]
    Why does Linux use LF as the newline character?
    Dec 19, 2017 · Unix used a single character, LF, from the beginning to save space and standardize to a canonical end-of-line, using two characters was inefficient and ...Missing: adoption | Show results with:adoption
  16. [16]
    Historical reason behind different line ending at different platforms
    Jan 7, 2009 · The Multics operating system began development in 1964 and used LF alone as its newline. Multics used a device driver to translate this ...newline - Difference between \n and \r? - Stack OverflowWhy does Windows use CR LF? - carriage return - Stack OverflowMore results from stackoverflow.com
  17. [17]
    A Line Break Is a Line Break - Mac OS X Hacks - O'Reilly
    The cause for this is how the line break is actually created. The Mac, by default, uses a single carriage return ( <CR> ), represented as \r . Unix, on the ...<|separator|>
  18. [18]
    Git core.autocrlf line ending default setting - Stack Overflow
    Sep 9, 2016 · autocrlf is set to false by default. (And has been since the property's original introduction on Feb 13, 2007, though it has since been ...Why should I use core.autocrlf=true in Git? - Stack OverflowWhat is the correct core.autocrlf setting I should use? - Stack OverflowMore results from stackoverflow.com
  19. [19]
    dos2unix 7.5.3 - DOS/MAC to UNIX and vice versa text file format converter
    ### Summary of Input and Output Handling for unix2dos Mode
  20. [20]
    dos2unix man | Linux Command Library
    Creates a backup of the original file by appending SUFFIX to its name before conversion. For example, .bak or .orig. -- Marks the end of options, useful ...Tldr · Parameters · Description
  21. [21]
    unix2dos(1) - Linux man page
    This manual page documents unix2dos, the program that converts text files in UNIX format to DOS format. Options. The following options are available: -h --help ...Missing: process | Show results with:process
  22. [22]
    dos2unix - Homebrew Formulae
    Also known as: unix2dos. Convert text between DOS, UNIX, and Mac formats. https://waterlan.home.xs4all.nl/dos2unix.html. License: BSD-2-Clause.
  23. [23]
    Cygwin Package Summary for dos2unix
    The dos2unix package provides conversion utilities to manipulate the line breaks of text files. These tools can convert between Unix, DOS/Windows, and (pre OS-X) ...
  24. [24]
    dos2unix / unix2dos - Text file format converters
    ### Summary of unix2dos Conversion Details from http://waterlan.home.xs4all.nl/dos2unix.html
  25. [25]
    HowTo: UNIX / Linux Convert DOS Newlines CR-LF to ... - nixCraft
    Jun 1, 2021 · To converts text files between DOS and Unix formats you need to use special utility called dos2unix. DOS text files traditionally have carriage ...
  26. [26]
    Package: util/flip/
    It converts lines ending with carriage-return (CR) and linefeed (LF) to lines ending with just linefeed, or vice versa. Executables for MS-DOS and Windows have ...
  27. [27]
    Replace \n with \r\n in Unix file - Stack Overflow
    Oct 5, 2009 · The safe thing to do is first remove all '\r' characters, and then insert (a single) \r before \n.
  28. [28]
    How to convert DOS/Windows newline (CRLF) to Unix newline (LF)
    Apr 10, 2010 · In general, just install dos2unix using your package manager, it really is much simpler and does exist on most platforms.sed - convert dos2unix line endings for all files in a directoryHow to convert files from Dos to Unix - java - Stack OverflowMore results from stackoverflow.com
  29. [29]
    Unix newlines to Windows newlines (on Windows) - Stack Overflow
    Apr 7, 2009 · Is there a way (say PowerShell, or a tool) in Windows that can recurse over a directory and convert any Unix files to Windows files. I'd be ...Windows command to convert Unix line endings? - Stack OverflowStrings and here document have UNIX line endings on WindowsMore results from stackoverflow.com
  30. [30]
    The ultimate guide to Windows and Unix file line ending conversion ...
    Dec 14, 2013 · To convert a file to Unix-style line endings (as used by Linux, BSD, OSX) just open up the terminal and run: perl -pi.bak -e 's/\R/\012/' /path/<|separator|>
  31. [31]
    How to Change File Endings From Unix to Dos | Baeldung on Linux
    Jun 20, 2024 · In this tutorial, we'll explore different ways to add a carriage return character before the newline character to make Unix files work properly in Windows.
  32. [32]
    notepad++ - EOL conversion in notepad ++ - Stack Overflow
    Apr 26, 2013 · That functionality is already built into Notepad++. From the "Edit" menu, select "EOL Conversion" -> "UNIX/OSX Format".How do I change the default EOL settings/encoding of new files in ...eol - Choose newline character in Notepad++ - Stack OverflowMore results from stackoverflow.com
  33. [33]
    EOL in Notepad++ and Notepad - file format - Super User
    May 22, 2010 · In that version, you can go to Edit -> EOL Conversion and change the format of the end of line character between MAC, Windows and Unix. Share.Notepad++ : Convert EOL from Windows to Unix after document has ...Notepad++, How to lock EOL conversion? - Super UserMore results from superuser.com
  34. [34]
    File format - Vim Tips Wiki - Fandom
    Entering :set ff? will probably show that the file was read as unix: the problem is that some lines actually end with CRLF while others end with LF. To fix this ...<|separator|>
  35. [35]
    Convert DOS/Windows line endings to Linux line endings in Vim
    Sep 17, 2008 · dos2unix is a commandline utility that will do this. In Vim, :%s/^M//g will if you use Ctrl - v Ctrl - m to input the ^M (On Windows, use Ctrl - q Ctrl -m ...
  36. [36]
    LF and CRLF converter online - Execeratics
    Welcome to the LF and CRLF converter. I convert the line breaks of your text documents from the LF/UNIX-format to the CRLF/DOS-format and backwards!
  37. [37]
    Dos to Unix Converter - Tool Slick
    Apr 29, 2018 · Dos to Unix Converter is an online tool to convert a text file from the MS-DOS/Microsoft Windows convention (CR + LF, 0x0A + 0x0D or \r + ...