dos2unix: fix CRLF line endings in scripts and text files

Convert Windows line endings to Unix format (and back with unix2dos) on Linux.

Linuxcrlftextline-endingsLow risk
Run dos2unix FILE to strip CR characters and convert CRLF to LF; use unix2dos for the reverse direction.

01Command

Command
$ dos2unix script.sh && unix2dos notes.txt

02What it does

Files edited on Windows carry \r\n line endings, which Linux tools render as stray ^M characters and — worse — as a broken shebang that makes a script fail with '/bin/bash^M: bad interpreter'. dos2unix removes the CR bytes in place; unix2dos re-adds them when a Windows tool requires CRLF. Without the package installed, equivalents are tr -d '\r' or sed -i 's/\r$//', but dos2unix handles BOM and Mac-classic modes correctly too.

03Risk

RiskLow risk
Requirementsdos2unix package
!Warning: dos2unix rewrites the file in place; use -n IN OUT to convert into a new file, and never run it on binary files.

04Undo

Undo
$ unix2dos FILE (converts back to CRLF)

05Sources

  • dos2unix(1) — Ubuntu manpage
    Canonical · technical · 2026-09-17
    Evidence: dos2unix converts DOS/Mac line breaks to Unix; unix2dos does the reverse; -n writes to a new file, -c sets conversion mode
  • dos2unix(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    Evidence: dos2unix converts line breaks from DOS/Mac to Unix; unix2dos does the reverse; -n writes to a new file, -c sets conversion mode

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions