The Ultimate Dos2Unix and Unix2Dos Guide: Converting Files Between Windows and Unix

Last update: 20/05/2025
Author Isaac
  • Understanding the different line break formats in text files between OS helps avoid incompatibilities.
  • dos2unix and unix2dos allow you to securely convert text files between Windows and Linux/Unix, with multiple advanced options to adjust the conversion to any need.
  • The conversion process covers not only line breaks, but also the management of encodings, BOM tags, and symbolic links, all of which are vital in modern environments.
  • Integrate these commands in scripts and automations facilitates mass file management in mixed environments, optimizing the workflow between computers and servers.

unix dos format

Have you ever had to deal with text files that don't look right when you open them in your favorite editor? Linux or Windows? This is usually due to differences in the characters that different operating systems use to mark the end of each line. If you work in mixed environments or collaborate with others who use different systems, you've probably encountered this small headache more than once. Luckily, there are classic commands like dos2unix y unix2dos that allow you to easily convert files between DOS/Windows and Unix/Linux text formats.

In the following article we break down in depth everything related to the dos2unix and unix2dos commandsWe will review from the most basic and theoretical concepts about line breaks and the differences between systems, to a review of the most advanced options of both programs, detailed examples of use, alternatives and Tricks to get you out of any jam. If you've ever wondered what each parameter does exactly or why a file sometimes appears corrupted when it isn't, you'll find the answer here. And we'll do so using relatable language and practical examples, so that anyone looking for a solution will know how to get through the situation, but also for anyone who wants to fully understand everything that happens "under the hood."

Why do dos2unix and unix2dos exist?

When dealing with compatibility between operating systems, one of the most noticeable (and, at the same time, most irritating) differences is the way each represents the end of a line in a text file. En Unix and Linux, the end of the line is indicated by the character LF (Line Feed, \n), while in DOS/Windows the sequence is used CRLF (Carriage Return + Line Feed, \ R \ n). To complicate matters further, the Mac Older (pre-OSX) models used only CR (\r), although today they are aligned with the Unix format.

So what happens? If you edit a file created in Windows in a Linux text editor, you'll often see some annoying ^M at the end of each line, or the file may not be processed correctly by some programs. Conversely, opening a Linux file in classic Windows Notepad may cause the text to appear jumbled, as if it were all on a single line. This is where dos2unix y unix2dos (and other equivalents such as fromdos, all, mac2unix o unix2mac), allowing you to easily change the line ending format from one system to another.

How do dos2unix and unix2dos work?

The heart of both commands is very simple: convert line breaks in text files between DOS/Windows and Unix formats. dos2unix transforms any line ending CRLF (\ R \ n) in just LF (\n), while unix2dos does the reverse. There are also variants for conversions between Mac and Unix formats. Both commands can overwrite the original file or save the conversion to a new file by specifying it with the appropriate option. Let's see the basic usage:

  • Convert from DOS/Windows to Unix/Linux: dos2unix archivo.txt
  • Convert from Unix/Linux to DOS/Windows: unix2dos archivo.txt
  • Save the result to a new file: dos2unix -n origen.txt destino.txt

Line breaks in operating systems: a history of incompatibilities

This problem dates back to the early days of computing, when each operating system adopted different conventions. In Unix/Linux the line break has always been used LF (\n). DOS/Windows decided to use the combination CRLF (\ R \ n), following a convention inherited from typewriters. Classic Mac OS (until before Mac OS X) opted for the CR (\r), although today files in different formats still generate incompatibilities.

  • Unix/Linux: each line ends with \n (LF).
  • DOS/Windows: Each line ends with \ R \ n (CRLF).
  • Classic Mac: Each line ends with \r (CR).
  Complete guide to installing Ubuntu Touch on your device

Today, Mac OS X and its successors have adopted the Unix convention, using LF as an end-of-line character. However, the legacy of mixed-format files remains a source of problems, especially when transferring information between platforms.

What does dos2unix (and unix2dos) actually do with files?

Both commands read the file line by line, detecting line endings based on the source format, and rewrite the file by inserting the appropriate end-of-line character or character sequence for the target system. Depending on the version and options used, they can also handle aspects such as character encoding (e.g., Unicode), handling files with byte order marks (BOMs), preserving dates, or file permissions.

Common options and parameters in dos2unix and unix2dos

Both commands have a wide range of parameters to adapt their operation to specific situations. We list the most common ones and their uses here:

  • -n: Allows you to specify the input and output files. Example: dos2unix -n input.txt output.txt. This way you can keep the original file.
  • -k: Keeps the modification date of the output file the same as that of the input file.
  • -o: Performs the conversion “in place,” that is, overwrites the original file with the result.
  • -f: Forces conversion even on binary files, although this is not generally recommended.
  • -c: Specifies the conversion mode: ascii (default), 7bit, iso o mac. For example, dos2unix -c iso archivo.txt.
  • -b / –keep-bom: Preserves the byte order mark (BOM) in Unicode files.
  • -m / –add-bom: Explicitly adds a BOM to the output file.
  • -l / –newline: Adds an additional blank line after each converted line break.
  • –allow-chown: Allows you to change the file owner on some systems and modes.
  • -h / -help: Displays help and ends the program.
  • -q / –quiet: Activate silent mode to not display messages on the screen.
  • -v / -verbose: Displays detailed information about the conversion.

These are just a few of the options available. Most modern versions of dos2unix/unix2dos They include many more, especially those dealing with coding, symbolic links, and pattern-based selective conversion.

Advanced handling of encodings and Unicode

One of the most problematic aspects when converting text files today has to do with the character encodings. The standard UTF-8 It is the most widespread in Unix/Linux and currently also recommended in Windows, but there are many files in UTF-16, ISO-8859-1, Windows 1252…The current versions of dos2unix They handle most of these cases quite competently, although there are important nuances to keep in mind:

  • Convert UTF-16 to UTF-8: dos2unix will attempt to transform UTF-16 files to UTF-8 (on Windows) or to the local encoding (on Unix/Linux). If the UTF-16 file lacks a BOM, it may be treated as binary and not converted.
  • BOM Management: In Windows, it's common to deal with Unicode files that have a byte order mark. Dos2unix can add or remove it depending on the options. -b y -m.
  • Specific encodings: By means of parameters -iso, -1252, -850, etc., you can force conversion between specific code pages, which is useful if you receive a file from an old environment or a specific location.
  How to fix error code 14 on Disney Plus

It's important to note that the encoding conversion options are not designed to modify binary files, only plain text files. Using dos2unix/unix2dos on binary files can cause data corruption.

Examples of use explained step by step

Let's look at some practical examples to illustrate the most common cases and also some advanced ones:

  • Convert a file and overwrite:
    dos2unix documento.txt
    This command will process the file, removing the CR characters from each line and writing the result to the same file.
  • Generate a new file without modifying the original:
    dos2unix -n entrada.txt salida.txt
    The conversion is saved in “output.txt”.
  • Convert multiple files at once:
    dos2unix archivo1.txt archivo2.txt archivo3.txt
  • Force conversion of a file that begins with a hyphen:
    dos2unix -- -foo.txt
  • Recursively convert files in a directory:
    find . -name '*.txt' -print0 | xargs -0 dos2unix
    This will convert all .txt files in the folder structure from the current directory down.
  • Convert files with specific encoding:
    dos2unix -iso -n origen.txt destino.txt
    Converts between a DOS code page and the ISO-8859-1 standard.
  • Detect and list which files need to be converted:
    dos2unix -ic *.txt
    Displays only files that contain DOS line breaks.

What about binary files?

Default, dos2unix e unix2dos automatically ignore binary files to prevent accidental corruption. If, for any reason, you need to force the conversion, you can use the -f. However, this practice not recommended because it can render files that are not strictly text unusable.

Managing symbolic links and directories

By default, these commands do not modify symbolic links or directories. However, they offer options such as -F (follow symbolic links and convert their destinations), -R (replace symbolic links with the converted files), or -S (ignore symbolic links, which is the default option).

Options for viewing and debugging

If you want to know what format a file has, or how many line breaks of each type it contains, you can use the option -i (o –Info):

  • dos2unix -i archivo.txt displays detailed information about line breaks and file type.

By adding certain flags, you can get custom information (e.g., only files with CRLF, only Unix-type jump count, etc.).

Conversion modes: ascii, iso, 7bit, mac

  • ascii (default): Converts only line endings. This is the safe option for UTF-8 files.
  • iso: Converts between DOS code tables and the ISO-8859-1 standard. Useful for older or multi-language files.
  • 7bit: Eliminates 8-bit characters, useful for very old systems or those requiring extreme compatibility.
  • mac: Converts between classic Mac and Unix formats.

These modes support combination with other options to convert different types of files according to the user's needs.

Working with scripts and alternative commands

If your system doesn't have dos2unix/unix2dos installed or you need to perform the conversion manually (or in scripts), there are several alternatives using classic Unix commands:

  • Using sed: sed 's/\r$//' fichero.txt (the ^M is entered with CTRL+V CTRL+M).
  • In modern GNU sed: sed 's/\x0D$//' fichero.txt.
  • Convert from Unix to DOS with sed: sed 's/$/\r/' fichero.txt.
  • Using tr: tr -d '\r' < input.file > output.file.
  • With Perl: perl -pi -e 's/\r\n/\n/g' input.file.

These methods are useful when you don't have the specific programs installed or need a quick fix on limited systems.

  How to Reset Your PIN on Roblox if You Forgot It

Conversion examples with different encodings

The wealth of options available in dos2unix and unix2dos allows for a wide variety of scenarios. Let's look at some examples to illustrate how to address specific needs:

  • From Windows UTF-16 (with BOM) to Unix UTF-8:
    dos2unix -n in.txt out.txt
  • From Windows UTF-16LE (no BOM) to Unix UTF-8:
    dos2unix -ul -n in.txt out.txt
  • From Unix UTF-8 to Windows UTF-8 (without BOM):
    unix2dos -m -n in.txt out.txt
  • From Unix UTF-8 to Windows UTF-16:
    unix2dos < in.txt | iconv -f UTF-8 -t UTF-16 > out.txt
  • From Windows 1252 to Unix Latin-1:
    dos2unix -1252 -n in.txt out.txt
  • From Unix Latin-1 to DOS 850:
    unix2dos -850 -n in.txt out.txt

In situations where you need to convert between unusual code pages or files with international characters, you can combine dos2unix/unix2dos with other utilities such as iconv to convert between different encodings before or after changing line breaks.

Bulk Conversion and Automation Scripts

One of the strengths of these commands is their integration into scripts and automated environments. If you want to convert all the .txt files in a folder (and subfolders), you can use the usual shell commands:

  • On Unix: find . -name '*.txt' -print0 | xargs -0 dos2unix
  • On windows DCM: for /R %G in (*.txt) do dos2unix "%G"
  • En PowerShell: get-childitem -path . -filter '*.txt' -recurse | foreach-object {dos2unix $_.Fullname}

The use of -print0 y -0 On Unix it allows you to correctly handle file names with spaces, accents or quotes.

Environment variables and regional customization

dos2unix/unix2dos allows you to adjust the language and localization using several environment variables:

  • LANGUAGE: Select your primary language. Example: export LANG=es_ES for Spanish from Spain.
  • LANGUAGE: Allows a list of preferred languages. Example: LANGUAGE=es:en.
  • DOS2UNIX_LOCALEDIR: Defines the path to the program's language files.

These options are especially useful in international or multi-user environments.

Exit, error, and return messages

Upon completion, the program returns 0 if everything went well or 1 if there has been a failure unrelated to the operating system. In silent mode (-q), the return value remains zero unless there is an error in the parameters or incorrect use of options.

Beyond dos2unix and unix2dos: Other utilities and documentation

For special cases related to the conversion of text files you can consult other commands and programs such as fillet, iconv, find o xargsAll of these can be combined to create robust solutions fully tailored to the needs of each project.

If you want to go deeper, some useful resources are:

Installing dos2unix/unix2dos on major systems

In most Linux distributions and Unix environments, these commands can be easily installed:

  • Debian/Ubuntu: sudo apt-get install dos2unix
  • CentOS/Red Hat: sudo yum install dos2unix
  • ArchLinux: sudo pacman -S dos2unix

On systems like Ubuntu, for example, if you search for "dos2unix" or "unix2dos" and you don't have them available, you'll simply need to install the corresponding package (previously called "sysutils"). In Windows environments, portable versions are available, or you can use emulators like Cygwin or WSL.

Leave a comment