LinuxCommandLibrary

dos2unix

Convert DOS line endings to Unix

TLDR

Change the line endings of a file

$ dos2unix [path/to/file]
copy

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

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

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

SYNOPSIS

dos2unix [options] [file ...]

PARAMETERS

-7, --iso
    Convert to 7-bit ASCII (ISO/IEC 646)

-a, --add-carriage-return
    Add CR before every LF

-b, --binary
    Safe binary read (no length check)

-c CONVMODE, --conv=CONVMODE
    Set conversion mode (cr, ccr, apple, crcrlf)

-d, --dos2mac
    Convert DOS to Macintosh format

-e, --add-eol
    Add linefeed to end if missing

-f, --force
    Force processing of all files

-h, --help
    Display help

-i, --info
    Display file info (line endings, encoding)

-k, --keep-date
    Preserve original file timestamp

-L, --license
    Show license information

-n, --newfile
    Create new file (old new syntax)

-N, --info-newline
    Show newline type info

-o, --oldfile
    Overwrite original file

-q, --quiet
    Suppress messages

-r, --reuse
    Reuse DOS file (in-place)

-v, --verbose
    Verbose output

-V, --version
    Show version

DESCRIPTION

dos2unix is a command-line utility that transforms text files from DOS/Microsoft Windows format to Unix/Linux format. The main task is replacing DOS-style line endings (CRLF, or \r\n) with Unix-style line feeds (LF, or \n). This prevents issues like visible ^M characters in editors or execution failures in Unix shells and scripts.

Dos2unix processes files in-place or creates new ones, supports standard input/output for piping, and handles multiple files or directories. It detects file types, skips binary files by default (with length checks), and offers options for character encoding conversions (e.g., ISO-8859-1 to UTF-8), adding/removing carriage returns, or Mac format handling.

Essential for cross-platform workflows, such as editing Windows files on Linux servers, preparing code repositories, or batch-converting logs/emails. Safe options prevent data loss, and verbose/info modes aid diagnostics. Installed via package managers like apt (dos2unix) or yum, it's lightweight and fast even on gigabyte files.

CAVEATS

Binary files may corrupt without -b; test with -n to avoid overwriting originals. Skips non-text files by default unless -f. Not for UTF-16/Unicode without encoding options.

EXAMPLES

dos2unix file.txt
Convert in-place.

dos2unix -n input.txt output.txt
Create new output file.

dos2unix -k *.txt
Convert all .txt files, keep timestamps.

dos2unix < input.txt
Process from stdin.

DEFAULT BEHAVIOR

Removes CR from CRLF; converts ISO-8859-1 to UTF-8. Use -i to check file types before conversion.

HISTORY

Developed by Benjamin Lin in 1992 for Unix systems handling DOS files. Evolved into open-source dos2unix package (GPLv3); major maintainer Pieter van der Eijk since 2009. Latest stable v7.5.3 (2023), bundled in most Linux distros.

SEE ALSO

unix2dos(1), mac2unix(1), recode(1), tr(1), col(1)

Copied to clipboard